feat: added 03_dom

This commit is contained in:
Philippe Torrel
2026-07-13 10:38:22 +02:00
parent 6292cabfee
commit e0bd3930d9
134 changed files with 21947 additions and 26 deletions

View File

@@ -0,0 +1,51 @@
# Regex Beispiel
Am besten löst man das mit einem regulären Ausdruck, der entweder nach **Inhalten in Anführungszeichen** sucht _oder_ nach **Inhalten ohne Kommas**.
Hier ist der passende Reguläre Ausdruck:
`/"([^"\\]*(?:\\.[^"\\]*)*)"|([^,]+)/g`
---
### Wie der Regex funktioniert
Der Ausdruck nutzt das **ODER-Prinzip (`|`)**, um zwei Szenarien zu matchen:
1. **`"([^"\\]*(?:\\.[^"\\]*)*)"` (Gruppe 1):** Matcht alles, was in doppelten Anführungszeichen steht. Er erlaubt sogar escaped Anführungszeichen (`\"`) im Text.
2. **`([^,]+)` (Gruppe 2):** Wenn keine Anführungszeichen da sind, matcht er alles bis zum nächsten Komma.
---
### Anwendung in JavaScript (Beispiel)
Da du das Ergebnis sicherlich in einem Array von einzelnen Werten brauchst, kannst du den Regex wie folgt anwenden. Hierbei nutzen wir `matchAll`, um die Treffer sauber zu trennen und die Anführungszeichen direkt zu entfernen:
```javascript
const input = '"very big, soft computer mouse","the cutest peripheral ever",10,39.90';
const regex = /"([^"\\]*(?:\\.[^"\\]*)*)"|([^,]+)/g;
const matches = [...input.matchAll(regex)];
const result = matches.map((match) => {
// Wenn Gruppe 1 existiert, war es ein String in Anführungszeichen
// Wenn Gruppe 2 existiert, war es ein unberührter Wert (Zahl)
const value = match[1] !== undefined ? match[1] : match[2].trim();
// Optional: Datentypen konvertieren
if (!isNaN(value) && value !== '') {
return Number(value);
}
return value;
});
console.log(result);
```
### Das Ergebnis (Output)
Nach dem Parsen erhältst du ein sauberes Array, bei dem die Kommas innerhalb der Strings ignoriert wurden und die Zahlen einsatzbereit sind:
```json
["very big, soft computer mouse", "the cutest peripheral ever", 10, 39.9]
```

View File

@@ -0,0 +1 @@
"very big, soft computer mouse","the cutest peripheral ever",10,39.90
1 very big, soft computer mouse the cutest peripheral ever 10 39.90

View File

@@ -0,0 +1,11 @@
Code,Short Description,Tagline,Quantity,Price
MUG0007,coffee mug with LCD level indicator,never again reach for the empty mug,20,49.90
MUG0013,coffee mug with bluetooth-connected coffee grounds scanner for automatized fortune telling,put the smart into coffee reading,20,99.90
OFF3145,pen with pre-warmed ink (tested on the South Pole),the pen is mightier than the cold,50,29.90
COM1001,ambidextrous computer mouse,end the dictate of left and right,10,19.90
COM0404,Easter egg themed webcam for your monitor,put an Easter egg on hardware too,20,149.90
COM0001,Vulcan language vi cheatsheet,learn vi and Vulcan at the same time,3,9.90
COM1536,Klingon language emacs cheatsheet,learn emacs and Klingon at the same time,50,9.90
MOB0555,smartphone case with built-in screen,never miss a message,20,39.90
1 Code Short Description Tagline Quantity Price
2 MUG0007 coffee mug with LCD level indicator never again reach for the empty mug 20 49.90
3 MUG0013 coffee mug with bluetooth-connected coffee grounds scanner for automatized fortune telling put the smart into coffee reading 20 99.90
4 OFF3145 pen with pre-warmed ink (tested on the South Pole) the pen is mightier than the cold 50 29.90
5 COM1001 ambidextrous computer mouse end the dictate of left and right 10 19.90
6 COM0404 Easter egg themed webcam for your monitor put an Easter egg on hardware too 20 149.90
7 COM0001 Vulcan language vi cheatsheet learn vi and Vulcan at the same time 3 9.90
8 COM1536 Klingon language emacs cheatsheet learn emacs and Klingon at the same time 50 9.90
9 MOB0555 smartphone case with built-in screen never miss a message 20 39.90

View File

@@ -0,0 +1,63 @@
import fs from 'node:fs';
import zlib from 'node:zlib';
import color from 'chalk';
const data = fs.readFileSync('data/products.csv', 'utf-8');
const toCamelCase = (str) => {
return str
.trim()
.toLowerCase()
.replace(/[^a-zA-Z0-9]+(.)/g, (match, chr) => chr.toUpperCase());
};
const convertCsvToJson = (products, ar) => {
const resultAr = products.map((str) => {
const fields = str.split(/\s*,\s*/);
const labels = ar.map((label, idx) => {
return [label, isNaN(fields[idx]) ? fields[idx] : Number(fields[idx])];
});
const obj = Object.fromEntries(labels);
// console.log(labels); // Entries Array
//console.log(obj);
return obj;
// return {
// [ar[0]]: fields[0],
// [ar[1]]: fields[1],
// [ar[2]]: fields[2],
// [ar[3]]: Number(fields[3]),
// [ar[4]]: Number(fields[4]),
// };
});
// console.log(resultAr);
const jsonStr = JSON.stringify(resultAr, '', 2);
fs.writeFile('products.json.gz', zlib.gzipSync(jsonStr), 'utf-8', (err) => {
if (err) {
console.log(err);
} else {
console.log(color.green('file created!'));
}
});
};
const productAr = data
.split('\n')
.map((row) => row.replace('\n', ''))
.filter((row) => row !== '');
const headerAr = productAr
.shift()
.split(/\s*,\s*/)
.map(toCamelCase);
// console.log('=======');
// console.log(productAr);
convertCsvToJson(productAr, headerAr);

View File

@@ -0,0 +1,17 @@
{
"name": "u27_zlib",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"chalk": "^5.6.2",
"papaparse": "^5.5.4"
}
}

Binary file not shown.