Skip to content

Commit

Permalink
fix: use correct line endings on windows when using prettyprint, ndjs…
Browse files Browse the repository at this point in the history
…on or transforms
  • Loading branch information
juanjoDiaz committed Apr 23, 2022
1 parent 60dcefc commit 3c27a69
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 27 deletions.
8 changes: 4 additions & 4 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ function getOutputStream(outputPath, config) {
return process.stdout;
}

async function getInput(inputPath, ndjson) {
async function getInput(inputPath, ndjson, eol) {
if (!inputPath) return getInputFromStdin();
if (ndjson) return parseNdJson(await readFile(inputPath, 'utf8'));
if (ndjson) return parseNdJson(await readFile(inputPath, 'utf8'), eol);
return require(inputPath);
}

Expand All @@ -104,15 +104,15 @@ async function getInputFromStdin() {
async function processOutput(outputPath, csv, config) {
if (!outputPath) {
// eslint-disable-next-line no-console
config.pretty ? (new TablePrinter(config)).printCSV(csv) : console.log(csv);
config.pretty ? (new TablePrinter(config)).printCSV(csv) : process.stdout.write(csv);
return;
}

await writeFile(outputPath, csv);
}

async function processInMemory(config, opts) {
const input = await getInput(program.input, config.ndjson);
const input = await getInput(program.input, config.ndjson, config.eol);
const output = new JSON2CSVParser(opts).parse(input);
await processOutput(program.output, output, config);
}
Expand Down
13 changes: 7 additions & 6 deletions bin/utils/TablePrinter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const os = require('os');
const { Writable } = require('stream');

const MIN_CELL_WIDTH = 15;
Expand Down Expand Up @@ -44,14 +45,14 @@ class TablePrinter {
}

print(top, lines, bottom) {
const table = `${top}\n`
const table = `${top}${os.EOL}`
+ lines
.map(row => this.formatRow(row))
.join(`\n${this.middleLine}\n`)
+ (bottom ? `\n${bottom}` : '');
.join(`${os.EOL}${this.middleLine}${os.EOL}`)
+ os.EOL
+ (bottom ? bottom : '');

// eslint-disable-next-line no-console
console.log(table);
process.stdout.write(table);
}

formatRow(row) {
Expand All @@ -66,7 +67,7 @@ class TablePrinter {

return Array(height).fill('')
.map((_, i) => `│${processedCells.map(cell => cell[i]).join('│')}│`)
.join('\n');
.join(os.EOL);
}

formatCell(content, heigth, width) {
Expand Down
4 changes: 2 additions & 2 deletions bin/utils/parseNdjson.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

function parseNdJson(input) {
function parseNdJson(input, eol) {
return input
.split('\n')
.split(eol)
.map(line => line.trim())
.filter(line => line !== '')
.map(line=> JSON.parse(line));
Expand Down
4 changes: 2 additions & 2 deletions lib/JSON2CSVStreamParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class JSON2CSVStreamParser extends JSON2CSVBase {
}

getNdJsonTokenizer(asyncOpts) {
const tokenizer = new Tokenizer({ ...asyncOpts, separator: '\n' });
this.tokenParser = new TokenParser({ paths: ['$'], keepStack: false, separator: '\n' });
const tokenizer = new Tokenizer({ ...asyncOpts, separator: this.opts.eol });
this.tokenParser = new TokenParser({ paths: ['$'], keepStack: false, separator: this.opts.eol });
this.configureCallbacks(tokenizer, this.tokenParser);
return tokenizer;
}
Expand Down
9 changes: 5 additions & 4 deletions test/CLI.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const os = require('os');
const { mkdir, rm, readFile } = require('fs').promises;
const { join: joinPath } = require('path');
const { exec } = require('child_process');
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

t.fail('Exception expected.');
} catch (err) {
t.ok(err.message.includes('Unexpected SEPARATOR ("\\n") in state COMMA'));
t.ok(err.message.includes(`Unexpected SEPARATOR ("${os.EOL.replace('\r', '\\r').replace('\n', '\\n')}") in state COMMA`));
}
});

Expand All @@ -62,7 +63,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

const { stdout: csv } = await execAsync(`${cli} -i "${getFixturePath('/json/ndjson.json')}" ${opts}`);

t.equal(csv, csvFixtures.ndjson + '\n'); // console.log append the new line
t.equal(csv, csvFixtures.ndjson);
});

testRunner.add('should error on invalid input file path', async (t) => {
Expand Down Expand Up @@ -116,7 +117,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

const { stdout: csv } = await execAsync(`${cli} -i "${getFixturePath('/json/deepJSON.json')}" ${opts}`);

t.equal(csv, csvFixtures.deepJSON + '\n'); // console.log append the new line
t.equal(csv, csvFixtures.deepJSON);
});

testRunner.add('should parse json to csv and infer the fields automatically ', async (t) => {
Expand Down Expand Up @@ -354,7 +355,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

const { stdout: csv } = await execution;

t.equal(csv, csvFixtures.default + '\n'); // console.log append the new line
t.equal(csv, csvFixtures.default);
});

testRunner.add('should error if stdin data is not valid with -s flag', async (t) => {
Expand Down
6 changes: 4 additions & 2 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const os = require('os');

const {
AsyncParser: Parser,
transforms: { flatten, unwind },
Expand Down Expand Up @@ -73,7 +75,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

t.fail('Exception expected');
} catch(err) {
t.equal(err.message, 'Unexpected SEPARATOR ("\\n") in state COMMA');
t.equal(err.message, `Unexpected SEPARATOR ("${os.EOL.replace('\r', '\\r').replace('\n', '\\n')}") in state COMMA`);
}
});

Expand All @@ -89,7 +91,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

t.fail('Exception expected');
} catch (err) {
t.equal(err.message, 'Unexpected SEPARATOR ("\\n") in state COMMA');
t.ok(err.message.includes(`Unexpected SEPARATOR ("${os.EOL.replace('\r', '\\r').replace('\n', '\\n')}") in state COMMA`));
}
});

Expand Down
4 changes: 3 additions & 1 deletion test/JSON2CSVStreamParser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const os = require('os');

const {
StreamParser: Parser,
transforms: { flatten, unwind },
Expand Down Expand Up @@ -74,7 +76,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

t.fail('Exception expected');
} catch (err) {
t.equal(err.message, 'Unexpected SEPARATOR ("\\n") in state COMMA');
t.ok(err.message.includes(`Unexpected SEPARATOR ("${os.EOL.replace('\r', '\\r').replace('\n', '\\n')}") in state COMMA`));
}
});

Expand Down
6 changes: 4 additions & 2 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const os = require('os');

const {
Transform: Parser,
transforms: { flatten, unwind },
Expand Down Expand Up @@ -71,7 +73,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

t.fail('Exception expected');
} catch(err) {
t.equal(err.message, 'Unexpected SEPARATOR ("\\n") in state COMMA');
t.equal(err.message, `Unexpected SEPARATOR ("${os.EOL.replace('\r', '\\r').replace('\n', '\\n')}") in state COMMA`);
}
});

Expand All @@ -87,7 +89,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {

t.fail('Exception expected');
} catch (err) {
t.equal(err.message, 'Unexpected SEPARATOR ("\\n") in state COMMA');
t.ok(err.message.includes(`Unexpected SEPARATOR ("${os.EOL.replace('\r', '\\r').replace('\n', '\\n')}") in state COMMA`));
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/csv/prettyprint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
│ "Mercedes" │ 20000 │ "yellow" │
├────────────────────┼───────────────┼───────────────┤
│ "Porsche" │ 30000 │ "green" │
└────────────────────┴───────────────┴───────────────┘
└────────────────────┴───────────────┴───────────────┘
2 changes: 1 addition & 1 deletion test/fixtures/csv/prettyprintWithoutHeader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
│ "Mercedes" │ 20000 │ "yellow" │
├───────────────┼───────────────┼───────────────┤
│ "Porsche" │ 30000 │ "green" │
└───────────────┴───────────────┴───────────────┘
└───────────────┴───────────────┴───────────────┘
2 changes: 1 addition & 1 deletion test/fixtures/csv/prettyprintWithoutRows.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
┌────────────────┬────────────────┬────────────────┐
│ "fieldA" │ "fieldB" │ "fieldC" │
└────────────────┴────────────────┴────────────────┘
└────────────────┴────────────────┴────────────────┘
3 changes: 2 additions & 1 deletion test/parseNdjson.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const os = require('os');
const parsendjson = require('../bin/utils/parseNdjson');

module.exports = (testRunner, jsonFixtures) => {
testRunner.add('should parse line-delimited JSON', (t) => {
const parsed = parsendjson(jsonFixtures.ndjson());
const parsed = parsendjson(jsonFixtures.ndjson(), os.EOL);

t.equal(parsed.length, 4, 'parsed input has correct length');
});
Expand Down

0 comments on commit 3c27a69

Please sign in to comment.