This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Übung 25
|
||||
const quotedCSV = '"very big, soft computer mouse","the cutest peripheral ever","10","39.90"';
|
||||
|
||||
// Übung 26
|
||||
// In der Praxis finden sich auch CSV-Datensätze, in denen manche Felder mit Anführungszeichen umgeben sind und manche nicht.
|
||||
|
||||
// Schreibe ein Programm, das auch mit diesen Fällen zurechtkommt. Folgender Datensatz soll korrekt zerlegt werden:
|
||||
|
||||
const mixedCSV = '"very big, soft computer mouse","the cutest peripheral ever",10,39.90';
|
||||
|
||||
/*
|
||||
Übung 25: CSV-Datensätze mit Anführungszeichen
|
||||
Bisher haben wir die Felder in einem CSV-Datensatz einfach mit der Anweisung:
|
||||
|
||||
|
||||
record.split(',')
|
||||
|
||||
|
||||
in einzelne Felder zerlegt. In der Praxis kann es aber vorkommen, dass ein einzelnes Feld ein Komma enthält. Das Problem wird üblicherweise gelöst, indem die einzelnen Felder noch mit Anführungszeichen umgeben werden. Hier ist ein Beispiel:
|
||||
|
||||
'very big, soft computer mouse', 'the cutest peripheral ever', '10', '39.90'
|
||||
|
||||
|
||||
Das erste Feld enthält ein Komma (very big, soft computer mouse), trotzdem ist die Unterteilung eindeutig. Schreibe ein kurzes Programm, das diesen Datensatz korrekt zerlegt und die einzelnen Felder ausgibt:
|
||||
|
||||
|
||||
const quotedCSV =
|
||||
'"very big, soft computer mouse","the cutest peripheral ever","10","39.90"'
|
||||
*/
|
||||
|
||||
/*
|
||||
Übung 26: CSV-Datensätze mit Anführungszeichen — Teil 2
|
||||
In der Praxis finden sich auch CSV-Datensätze, in denen manche Felder mit Anführungszeichen umgeben sind und manche nicht.
|
||||
|
||||
Schreibe ein Programm, das auch mit diesen Fällen zurechtkommt. Folgender Datensatz soll korrekt zerlegt werden:
|
||||
|
||||
const mixedCSV =
|
||||
'"very big, soft computer mouse","the cutest peripheral ever",10,39.90'
|
||||
|
||||
*/
|
||||
@@ -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]
|
||||
```
|
||||
@@ -0,0 +1 @@
|
||||
"very big, soft computer mouse","the cutest peripheral ever",10,39.90
|
||||
|
@@ -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
|
||||
|
||||
|
||||
|
@@ -0,0 +1,13 @@
|
||||
import fs from 'node:fs';
|
||||
import zlib from 'node:zlib';
|
||||
import color from 'chalk';
|
||||
|
||||
const data = fs.readFileSync('data/products.csv', 'utf-8');
|
||||
|
||||
const convertCsvToJson = (products, ar) => {};
|
||||
|
||||
/*
|
||||
Übung 27: Ohne Umwege
|
||||
|
||||
Die Datei products.csv soll in eine products.json umgewandelt werden und in products.json.gz komprimiert werden.
|
||||
*/
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"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": "^6.0.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// const crypto = require('node:crypto');
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
const PASSWORD_LENGTH = 10;
|
||||
const s = '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!.,;#$%/+*';
|
||||
|
||||
const buf = crypto.randomBytes(PASSWORD_LENGTH);
|
||||
const password = Array.from(buf)
|
||||
.map((byte) => s.charAt(byte % s.length))
|
||||
.join('');
|
||||
|
||||
console.log(password);
|
||||
|
||||
// Übung 28: Passwortgenerator
|
||||
|
||||
// Was macht der folgende Code?
|
||||
|
||||
// OK: Der Titel verrät es schon. Versuche dennoch, das Programm zu verstehen! Benutze die Dokumentation der Standardbibliothek, um nachzuschlagen, was crypto.randomBytes(…) genau macht!
|
||||
|
||||
// Bonusfrage: Warum sind in den Zeichen «1», «i» und «l» nicht enthalten, genau so wenig wie «0» und «o»?
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "u28_password-generator",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u28_password-generator",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"lodash": "^4.18.1"
|
||||
}
|
||||
},
|
||||
"node_modules/lodash": {
|
||||
"version": "4.18.1",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz",
|
||||
"integrity": "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "u28_password-generator",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"lodash": "^4.18.1"
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
// const fs = require('node:fs');
|
||||
// const zlib = require('node:zlib');
|
||||
import fs from 'node:fs';
|
||||
import zlib from 'node:zlib';
|
||||
|
||||
const gzipCompressor = zlib.createGzip();
|
||||
|
||||
const inputStream = fs.createReadStream('data/products.html');
|
||||
const outputStream = fs.createWriteStream('data/products.html.gz');
|
||||
|
||||
inputStream.on('data', (data) => {
|
||||
console.log('Pakete (data chunks) werden geladen: ', data.length);
|
||||
outputStream.write(zlib.gzipSync(data));
|
||||
});
|
||||
|
||||
inputStream.pipe(gzipCompressor).pipe(outputStream);
|
||||
|
||||
// Übung 29: Datenströme
|
||||
// Schreibe Codebeispiel 239 um, ohne die Funktion pipe(…) zu verwenden!
|
||||
|
||||
// TIPP: Um eine Datei päckchenweise lesen- und schreiben zu können, gibt es für das fs Modul die Methoden createReadStream und createWriteStream.
|
||||
|
||||
// ÜBUNG BEREITS GELÖST!!!
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "03_stream",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "module"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Übung 32: Information zum Betriebssystem und der Anzahl der vorhandenen CPUs
|
||||
|
||||
// Im Kontext einer Monitoring-Lösung benötigt ein Kunde eine kleine CLI (Command Line Interface)-Anwendung, die es erlaubt, das aktuelle Betriebssystem und die Anzahl der CPUs auf dem jeweiligen Rechner zu ermitteln.
|
||||
|
||||
// Vorgehensweise
|
||||
|
||||
// 1.Schau dir die Online-Dokumentation zum os-Modul der Standardbibliothek an.
|
||||
// 2.Erstelle dann ein kurzes Node.js-Programm, das ausgibt, auf welcher Plattform es gerade läuft (also etwa »linux«, »darwin« (OS X) oder »win32«) und wie viele Kerne der Rechner hat.
|
||||
|
||||
// Hier eine mögliche Beispielausgabe:
|
||||
|
||||
// I am running on a machine with linux and 2 cores.
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "u32_os-info",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"type": "module"
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user