This commit is contained in:
Binary file not shown.
@@ -3,8 +3,52 @@ import zlib from 'node:zlib';
|
||||
import color from 'chalk';
|
||||
|
||||
const data = fs.readFileSync('data/products.csv', 'utf-8');
|
||||
const products = data.split('\n');
|
||||
const header = products.shift().split(',');
|
||||
// console.log('header', header); // => Code,Short Description,Tagline,Quantity,Price
|
||||
|
||||
const convertCsvToJson = (products, ar) => {};
|
||||
const convertCsvToJson = (productRow) => {
|
||||
// console.log('products', products);
|
||||
// console.log('ar', ar);
|
||||
|
||||
const cols = productRow.split(/\s*,\s*/);
|
||||
// console.log(product);
|
||||
// let obj = {};
|
||||
|
||||
// for (let i = 0; i < header.length; i++) {
|
||||
// obj[header[i]] = cols[i];
|
||||
// }
|
||||
|
||||
const product = header.reduce((obj, str, idx) => {
|
||||
obj[str] = cols[idx];
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
return product;
|
||||
};
|
||||
|
||||
const entries = products
|
||||
.filter((row) => row !== '') //
|
||||
.map(convertCsvToJson);
|
||||
console.log(entries);
|
||||
|
||||
// fs.writeFile('a.json', JSON.stringify(entries), 'UTF-8', (err) => {
|
||||
// console.warn(err);
|
||||
// });
|
||||
|
||||
const compressedData = zlib.gzipSync(JSON.stringify(entries));
|
||||
try {
|
||||
fs.writeFileSync('data/products.json.gz', compressedData);
|
||||
console.log('compressed file');
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
/*
|
||||
Übung 27: Ohne Umwege
|
||||
|
||||
Die Datei products.csv soll in eine products.json umgewandelt werden und in products.json.gz komprimiert werden.
|
||||
*/
|
||||
|
||||
/*
|
||||
Übung 27: Ohne Umwege
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "u27_zlib",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "u27_zlib",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chalk": "^6.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "6.0.1",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.1.tgz",
|
||||
"integrity": "sha512-/Ce6KNm3vIbWdMlNna6RVIZ/ICQxnJxCicet5LBKK9ZffBkqzDw0xh9EiKSljdRtiIQ1S1z4YgcscUUGzNCWrA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=22"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user