Skip to content

Commit 24a7893

Browse files
juanjoDiazknownasilya
authored andcommitted
fix: Rename ld-json to ndjson (#240)
1 parent 01ca93e commit 24a7893

File tree

9 files changed

+27
-27
lines changed

9 files changed

+27
-27
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $ npm install json2csv --save
4848
-V, --version output the version number
4949
-i, --input <input> Path and name of the incoming json file. If not provided, will read from stdin.
5050
-o, --output [output] Path and name of the resulting csv file. Defaults to stdout.
51-
-L, --ldjson Treat the input as Line-Delimited JSON.
51+
-n, --ndjson Treat the input as NewLine-Delimited JSON.
5252
-s, --no-streamming Process the whole JSON array in memory instead of doing it line by line.
5353
-f, --fields <fields> Specify the fields to convert.
5454
-l, --field-list [list] Specify a file with a list of fields to include. One field per line.
@@ -153,7 +153,7 @@ $ json2csv -i test.json -f name,version --no-header >> test.csv
153153
The programatic APIs take a configuration object very equivalent to the CLI options.
154154

155155
- `fields` - Array of Objects/Strings. Defaults to toplevel JSON attributes. See example below.
156-
- `ldjson` - Only effective on the streaming API. Indicates that data coming through the stream is ld-json.
156+
- `ndjson` - Only effective on the streaming API. Indicates that data coming through the stream is NDJSON.
157157
- `unwind` - Array of Strings, creates multiple rows from a single JSON document similar to MongoDB's $unwind
158158
- `flatten` - Boolean, flattens nested JSON using [flat]. Defaults to `false`.
159159
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)

bin/json2csv.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Table = require('cli-table');
99
const program = require('commander');
1010
const debug = require('debug')('json2csv:cli');
1111
const json2csv = require('../lib/json2csv');
12-
const parseLdJson = require('../lib/parse-ldjson');
12+
const parseNdJson = require('../lib/parse-ndjson');
1313
const pkg = require('../package');
1414

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

75-
function getInput(input, ldjson) {
75+
function getInput(input, ndjson) {
7676
if (inputPath) {
77-
if (ldjson) {
77+
if (ndjson) {
7878
return new Promise((resolve, reject) => {
7979
fs.readFile(inputPath, 'utf8', (err, data) => {
8080
if (err) {
8181
reject(err);
8282
return;
8383
}
8484

85-
resolve(parseLdJson(data));
85+
resolve(parseNdJson(data));
8686
});
8787
});
8888
}
@@ -97,8 +97,8 @@ function getInput(input, ldjson) {
9797
process.stdin.on('data', chunk => (inputData += chunk));
9898
process.stdin.on('error', err => debug('Could not read from stdin', err));
9999
process.stdin.on('end', () => {
100-
const rows = ldjson
101-
? parseLdJson(inputData)
100+
const rows = ndjson
101+
? parseNdJson(inputData)
102102
: JSON.parse(inputData);
103103

104104
return Promise.resolve(rows);
@@ -155,7 +155,7 @@ getFields(program.fieldList, program.fields)
155155
};
156156

157157
if (program.streamming === false) {
158-
return getInput(program.input, program.ldjson)
158+
return getInput(program.input, program.ndjson)
159159
.then(input => new JSON2CSVParser(opts).parse(input))
160160
.then(processOutput);
161161
}

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare namespace json2csv {
1717
}
1818

1919
interface Options<T> {
20-
ldjson?: boolean;
20+
ndjson?: boolean;
2121
fields?: (string | Field | CallbackField<T>)[];
2222
unwind?: string | string[];
2323
flatten?: boolean;

lib/JSON2CSVTransform.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class JSON2CSVTransform extends Transform {
1717
this._data = '';
1818
this._hasWritten = false;
1919

20-
if (this.params.ldjson) {
21-
this.initLDJSONParse();
20+
if (this.params.ndjson) {
21+
this.initNDJSONParse();
2222
} else {
2323
this.initJSONParser();
2424
}
@@ -30,11 +30,11 @@ class JSON2CSVTransform extends Transform {
3030
}
3131

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

4040
this.parser = {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
function parseLdJson(input) {
3+
function parseNdJson(input) {
44
return input
55
.split('\n')
66
.map(line => line.trim())
77
.filter(line => line !== '')
88
.map(line=> JSON.parse(line));
99
}
1010

11-
module.exports = parseLdJson;
11+
module.exports = parseNdJson;
File renamed without changes.
File renamed without changes.

test/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const test = require('tape');
55
const json2csv = require('../lib/json2csv');
66
const Json2csvParser = json2csv.Parser;
77
const Json2csvTransform = json2csv.Transform;
8-
const parseLdJson = require('../lib/parse-ldjson');
8+
const parsendjson = require('../lib/parse-ndjson');
99
const loadFixtures = require('./helpers/load-fixtures');
1010

1111
Promise.all([
@@ -632,33 +632,33 @@ Promise.all([
632632
// Tests for Streaming API
633633
// =======================================================
634634

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

641641
const transform = new Json2csvTransform(opts);
642-
const processor = jsonFixturesStreams.ldjson().pipe(transform);
642+
const processor = jsonFixturesStreams.ndjson().pipe(transform);
643643

644644
let csv = '';
645645
processor
646646
.on('data', chunk => (csv += chunk.toString()))
647647
.on('end', () => {
648-
t.equal(csv, csvFixtures.ldjson);
648+
t.equal(csv, csvFixtures.ndjson);
649649
t.end();
650650
})
651651
.on('error', err => t.notOk(err));
652652
});
653653

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

660660
const transform = new Json2csvTransform(opts);
661-
const processor = jsonFixturesStreams.ldjsonInvalid().pipe(transform);
661+
const processor = jsonFixturesStreams.ndjsonInvalid().pipe(transform);
662662

663663
processor.on('finish', () => {
664664
t.notOk(true);
@@ -1538,7 +1538,7 @@ Promise.all([
15381538
});
15391539

15401540
// =======================================================
1541-
// Test for parseLdJson
1541+
// Test for parsendjson
15421542
// =======================================================
15431543

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

1563-
const parsed = parseLdJson(input);
1563+
const parsed = parsendjson(input);
15641564

15651565
t.equal(parsed.length, 2, 'parsed input has correct length');
15661566
t.end();

0 commit comments

Comments
 (0)