feat: added 03_dom
This commit is contained in:
1
02_advanced/uebungen/u26_csv-quotes2/data/csvDatei.csv
Normal file
1
02_advanced/uebungen/u26_csv-quotes2/data/csvDatei.csv
Normal file
@@ -0,0 +1 @@
|
||||
"very big, soft computer mouse","the cutest peripheral ever",10,39.90
|
||||
|
11
02_advanced/uebungen/u26_csv-quotes2/data/products.csv
Normal file
11
02_advanced/uebungen/u26_csv-quotes2/data/products.csv
Normal 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
|
||||
|
||||
|
||||
|
58
02_advanced/uebungen/u26_csv-quotes2/index-andreas.js
Normal file
58
02_advanced/uebungen/u26_csv-quotes2/index-andreas.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import fs from 'node:fs';
|
||||
import color from 'chalk';
|
||||
|
||||
const data = fs.readFileSync('data/products.csv', 'utf-8');
|
||||
const productAr = data.split('\n').map((row) => row.replace('\n', ''));
|
||||
const headerAr = productAr.shift().split(/\s*,\s*/);
|
||||
|
||||
console.log(productAr);
|
||||
|
||||
// console.log(headerAr);
|
||||
// console.log('=======');
|
||||
// console.log(productAr);
|
||||
|
||||
const convertCsvToJson = (products, headerAr) => {
|
||||
let jsonStr = '[\n';
|
||||
|
||||
products.forEach((row, idxRow) => {
|
||||
jsonStr += ' {\n';
|
||||
row
|
||||
.trim()
|
||||
.split(',')
|
||||
.forEach((elem, idxElem, ar) => {
|
||||
if (ar.length === idxElem + 1) {
|
||||
if (isNaN(elem)) {
|
||||
jsonStr += ` "${headerAr[idxElem]}": "${elem}"\n`;
|
||||
} else {
|
||||
jsonStr += ` "${headerAr[idxElem]}": ${Number(elem)}\n`;
|
||||
}
|
||||
} else {
|
||||
if (isNaN(elem)) {
|
||||
jsonStr += ` "${headerAr[idxElem]}": "${elem}",\n`;
|
||||
} else {
|
||||
jsonStr += ` "${headerAr[idxElem]}": ${Number(elem)},\n`;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (products.length === idxRow + 1) {
|
||||
jsonStr += ` }\n`;
|
||||
} else {
|
||||
jsonStr += ` },\n`;
|
||||
}
|
||||
});
|
||||
|
||||
jsonStr += ']';
|
||||
|
||||
console.log(color.yellow(`${jsonStr}`));
|
||||
|
||||
fs.writeFile('products.json', jsonStr, 'utf-8', (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log(color.green('file created!'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
convertCsvToJson(productAr, headerAr);
|
||||
21
02_advanced/uebungen/u26_csv-quotes2/index-ersin.js
Normal file
21
02_advanced/uebungen/u26_csv-quotes2/index-ersin.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import fs from 'node:fs';
|
||||
import papa from 'papaparse';
|
||||
|
||||
//https://www.npmjs.com/package/papaparse
|
||||
|
||||
const data = fs.readFileSync('data/csvDatei.csv', 'utf-8').trim();
|
||||
|
||||
const parsed = papa.parse(data, {
|
||||
dynamicTyping: true,
|
||||
});
|
||||
|
||||
const dataRow = parsed.data[0];
|
||||
|
||||
const result = {
|
||||
name: dataRow[0],
|
||||
description: dataRow[1],
|
||||
quantity: dataRow[2],
|
||||
price: dataRow[3],
|
||||
};
|
||||
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
62
02_advanced/uebungen/u26_csv-quotes2/index-phil.js
Normal file
62
02_advanced/uebungen/u26_csv-quotes2/index-phil.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import fs from 'node:fs';
|
||||
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', 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);
|
||||
35
02_advanced/uebungen/u26_csv-quotes2/package-lock.json
generated
Normal file
35
02_advanced/uebungen/u26_csv-quotes2/package-lock.json
generated
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "u26_csv-quotes2",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u26_csv-quotes2",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chalk": "^5.6.2",
|
||||
"papaparse": "^5.5.4"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "5.6.2",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
|
||||
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": "^12.17.0 || ^14.13 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/papaparse": {
|
||||
"version": "5.5.4",
|
||||
"resolved": "https://registry.npmjs.org/papaparse/-/papaparse-5.5.4.tgz",
|
||||
"integrity": "sha512-SwzWD9gl/ElwYLCI0nUja1mFJzjq2D8ziShfNBa7zCHzkOozeOGDwHWQ+tvCzEZcewecWZ5U7kUopDnG+DFYEQ==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
17
02_advanced/uebungen/u26_csv-quotes2/package.json
Normal file
17
02_advanced/uebungen/u26_csv-quotes2/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "u26_csv-quotes2",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
58
02_advanced/uebungen/u26_csv-quotes2/products.json
Normal file
58
02_advanced/uebungen/u26_csv-quotes2/products.json
Normal file
@@ -0,0 +1,58 @@
|
||||
[
|
||||
{
|
||||
"code": "MUG0007",
|
||||
"shortDescription": "coffee mug with LCD level indicator",
|
||||
"tagline": "never again reach for the empty mug",
|
||||
"quantity": 20,
|
||||
"price": 49.9
|
||||
},
|
||||
{
|
||||
"code": "MUG0013",
|
||||
"shortDescription": "coffee mug with bluetooth-connected coffee grounds scanner for automatized fortune telling",
|
||||
"tagline": "put the smart into coffee reading",
|
||||
"quantity": 20,
|
||||
"price": 99.9
|
||||
},
|
||||
{
|
||||
"code": "OFF3145",
|
||||
"shortDescription": "pen with pre-warmed ink (tested on the South Pole)",
|
||||
"tagline": "the pen is mightier than the cold",
|
||||
"quantity": 50,
|
||||
"price": 29.9
|
||||
},
|
||||
{
|
||||
"code": "COM1001",
|
||||
"shortDescription": "ambidextrous computer mouse",
|
||||
"tagline": "end the dictate of left and right",
|
||||
"quantity": 10,
|
||||
"price": 19.9
|
||||
},
|
||||
{
|
||||
"code": "COM0404",
|
||||
"shortDescription": "Easter egg themed webcam for your monitor",
|
||||
"tagline": "put an Easter egg on hardware too",
|
||||
"quantity": 20,
|
||||
"price": 149.9
|
||||
},
|
||||
{
|
||||
"code": "COM0001",
|
||||
"shortDescription": "Vulcan language vi cheatsheet",
|
||||
"tagline": "learn vi and Vulcan at the same time",
|
||||
"quantity": 3,
|
||||
"price": 9.9
|
||||
},
|
||||
{
|
||||
"code": "COM1536",
|
||||
"shortDescription": "Klingon language emacs cheatsheet",
|
||||
"tagline": "learn emacs and Klingon at the same time",
|
||||
"quantity": 50,
|
||||
"price": 9.9
|
||||
},
|
||||
{
|
||||
"code": "MOB0555",
|
||||
"shortDescription": "smartphone case with built-in screen",
|
||||
"tagline": "never miss a message",
|
||||
"quantity": 20,
|
||||
"price": 39.9
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user