Skip to content

Commit

Permalink
fix: Rename ld-json to ndjson (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 30, 2018
1 parent 01ca93e commit 24a7893
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $ npm install json2csv --save
-V, --version output the version number
-i, --input <input> Path and name of the incoming json file. If not provided, will read from stdin.
-o, --output [output] Path and name of the resulting csv file. Defaults to stdout.
-L, --ldjson Treat the input as Line-Delimited JSON.
-n, --ndjson Treat the input as NewLine-Delimited JSON.
-s, --no-streamming Process the whole JSON array in memory instead of doing it line by line.
-f, --fields <fields> Specify the fields to convert.
-l, --field-list [list] Specify a file with a list of fields to include. One field per line.
Expand Down Expand Up @@ -153,7 +153,7 @@ $ json2csv -i test.json -f name,version --no-header >> test.csv
The programatic APIs take a configuration object very equivalent to the CLI options.

- `fields` - Array of Objects/Strings. Defaults to toplevel JSON attributes. See example below.
- `ldjson` - Only effective on the streaming API. Indicates that data coming through the stream is ld-json.
- `ndjson` - Only effective on the streaming API. Indicates that data coming through the stream is NDJSON.
- `unwind` - Array of Strings, creates multiple rows from a single JSON document similar to MongoDB's $unwind
- `flatten` - Boolean, flattens nested JSON using [flat]. Defaults to `false`.
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)
Expand Down
16 changes: 8 additions & 8 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Table = require('cli-table');
const program = require('commander');
const debug = require('debug')('json2csv:cli');
const json2csv = require('../lib/json2csv');
const parseLdJson = require('../lib/parse-ldjson');
const parseNdJson = require('../lib/parse-ndjson');
const pkg = require('../package');

const JSON2CSVParser = json2csv.Parser;
Expand All @@ -19,7 +19,7 @@ program
.version(pkg.version)
.option('-i, --input <input>', 'Path and name of the incoming json file. If not provided, will read from stdin.')
.option('-o, --output [output]', 'Path and name of the resulting csv file. Defaults to stdout.')
.option('-L, --ldjson', 'Treat the input as Line-Delimited JSON.')
.option('-n, --ndjson', 'Treat the input as NewLine-Delimited JSON.')
.option('-s, --no-streamming', 'Process the whole JSON array in memory instead of doing it line by line.')
.option('-f, --fields <fields>', 'Specify the fields to convert.')
.option('-l, --field-list [list]', 'Specify a file with a list of fields to include. One field per line.')
Expand Down Expand Up @@ -72,17 +72,17 @@ function getFields(fieldList, fields) {
: undefined);
}

function getInput(input, ldjson) {
function getInput(input, ndjson) {
if (inputPath) {
if (ldjson) {
if (ndjson) {
return new Promise((resolve, reject) => {
fs.readFile(inputPath, 'utf8', (err, data) => {
if (err) {
reject(err);
return;
}

resolve(parseLdJson(data));
resolve(parseNdJson(data));
});
});
}
Expand All @@ -97,8 +97,8 @@ function getInput(input, ldjson) {
process.stdin.on('data', chunk => (inputData += chunk));
process.stdin.on('error', err => debug('Could not read from stdin', err));
process.stdin.on('end', () => {
const rows = ldjson
? parseLdJson(inputData)
const rows = ndjson
? parseNdJson(inputData)
: JSON.parse(inputData);

return Promise.resolve(rows);
Expand Down Expand Up @@ -155,7 +155,7 @@ getFields(program.fieldList, program.fields)
};

if (program.streamming === false) {
return getInput(program.input, program.ldjson)
return getInput(program.input, program.ndjson)
.then(input => new JSON2CSVParser(opts).parse(input))
.then(processOutput);
}
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare namespace json2csv {
}

interface Options<T> {
ldjson?: boolean;
ndjson?: boolean;
fields?: (string | Field | CallbackField<T>)[];
unwind?: string | string[];
flatten?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions lib/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class JSON2CSVTransform extends Transform {
this._data = '';
this._hasWritten = false;

if (this.params.ldjson) {
this.initLDJSONParse();
if (this.params.ndjson) {
this.initNDJSONParse();
} else {
this.initJSONParser();
}
Expand All @@ -30,11 +30,11 @@ class JSON2CSVTransform extends Transform {
}

/**
* Init the transform with a parser to process LD-JSON data.
* Init the transform with a parser to process NDJSON data.
* It maintains a buffer of received data, parses each line
* as JSON and send it to `pushLine for processing.
*/
initLDJSONParse() {
initNDJSONParse() {
const transform = this;

this.parser = {
Expand Down
4 changes: 2 additions & 2 deletions lib/parse-ldjson.js → lib/parse-ndjson.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

function parseLdJson(input) {
function parseNdJson(input) {
return input
.split('\n')
.map(line => line.trim())
.filter(line => line !== '')
.map(line=> JSON.parse(line));
}

module.exports = parseLdJson;
module.exports = parseNdJson;
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const test = require('tape');
const json2csv = require('../lib/json2csv');
const Json2csvParser = json2csv.Parser;
const Json2csvTransform = json2csv.Transform;
const parseLdJson = require('../lib/parse-ldjson');
const parsendjson = require('../lib/parse-ndjson');
const loadFixtures = require('./helpers/load-fixtures');

Promise.all([
Expand Down Expand Up @@ -632,33 +632,33 @@ Promise.all([
// Tests for Streaming API
// =======================================================

test('should handle ld-json', (t) => {
test('should handle ndjson', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission'],
ldjson: true
ndjson: true
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixturesStreams.ldjson().pipe(transform);
const processor = jsonFixturesStreams.ndjson().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.ldjson);
t.equal(csv, csvFixtures.ndjson);
t.end();
})
.on('error', err => t.notOk(err));
});

test('should error on invalid ld-json input data', (t) => {
test('should error on invalid ndjson input data', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission'],
ldjson: true
ndjson: true
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixturesStreams.ldjsonInvalid().pipe(transform);
const processor = jsonFixturesStreams.ndjsonInvalid().pipe(transform);

processor.on('finish', () => {
t.notOk(true);
Expand Down Expand Up @@ -1538,7 +1538,7 @@ Promise.all([
});

// =======================================================
// Test for parseLdJson
// Test for parsendjson
// =======================================================

test('should output a string', (t) => {
Expand All @@ -1560,7 +1560,7 @@ Promise.all([
test('should parse line-delimited JSON', (t) => {
const input = '{"foo":"bar"}\n{"foo":"qux"}';

const parsed = parseLdJson(input);
const parsed = parsendjson(input);

t.equal(parsed.length, 2, 'parsed input has correct length');
t.end();
Expand Down

0 comments on commit 24a7893

Please sign in to comment.