58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
import fs from 'node:fs';
|
|
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 = (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
|
|
|
|
Die Datei products.csv soll in eine products.json umgewandelt werden und in products.json.gz komprimiert werden.
|
|
*/
|