CSV playground

node v16.18.0
version: master
endpointsharetweet
const fs = require('fs') fs.writeFileSync('1.mjs', `// ES Module import csv from 'csv' const csvParse = (data, options) => new Promise((resolve, reject) => { csv.parse(data, options, (err, output) => err ? reject(err) : resolve(output)) }) // Usage: async function main () { const csvTextArr = [ 'name,email,names.1,names.2', 'Elon,em@x.com,daddy elon,"doge, father"', 'Yusaku,ys@zozo.jp', 'Branson,rb@v.gal,,' ] csvTextArr[0] = csvTextArr[0].split(',').map(s => s.split('.')[0]).join(',') // remove dots const result = await csvParse(csvTextArr.join('\\n'), { columns: true, columns_duplicates_to_array: true, relax_column_count_less: true }) console.log(result) console.log(process.argv[1] + '\\n') } main() `) // <ESM-POLYFILL> Supports TS, MJS, CJS const { execSync } = require('child_process') const _ = require('lodash') // Side effect only const MODULES_PATH = (require.resolve('lodash')).replace(/\/lodash\/lodash\.js$/, '') execSync('ln -s '+MODULES_PATH+' node_modules') const tsConfig = { compilerOptions: { "target": "esnext", "module": "ESNext", "strict": true, "allowJs": true, "checkJs": false, "resolveJsonModule": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true }, "include": [ "src/**/*.ts" ], "exclude": [ "node_modules" ] } fs.writeFileSync('tsconfig.json', JSON.stringify(tsConfig, null, 2)) const rawStdout = execSync( // Run any TS, MJS or CJS files laying around 'find \\( -name \'*.?js\' -o -name \'*.ts\' \\) -maxdepth 2 -print0 | sort -z | xargs -0 -L1 node --loader ts-node/esm/transpile-only', { encoding: 'utf-8' } ) console.log(rawStdout) // </ESM-POLYFILL>
/* execSync('cat package.json', { encoding: 'utf-8' }) console.log(require.main) console.log(...execSync('set', { encoding: 'utf-8' }).split('\n')) console.log(...execSync(`cat /app/node_modules/fs-extra/package.json`, { encoding: 'utf-8' }).split('\n')) console.log(...execSync(`cat ${MODULES_PATH}/fs-extra/package.json`, { encoding: 'utf-8' }).split('\n')) console.log(...execSync('ls -laht "/app/"', { encoding: 'utf-8' }).split('\n')) console.log(process.argv) console.log(__filename) console.log(process.env) execSync('npm install csv', { encoding: 'utf-8' }) */
Loading…

no comments

    sign in to comment