Skip to content

Commit dbc6ce2

Browse files
committed
chore: code cleanup, new docs, API cleanup
1 parent 5540123 commit dbc6ce2

File tree

8 files changed

+79
-80
lines changed

8 files changed

+79
-80
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
package-lock.json
2+
package-lock.json
3+
yarn.lock

cli.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
#!/usr/bin/env node
22

3-
var fs = require('fs')
4-
var ndjson = require('./index.js')
5-
var minimist = require('minimist')
3+
const fs = require('fs')
4+
const { pipeline } = require('readable-stream')
5+
const minimist = require('minimist')
6+
const ndjson = require('./index.js')
67

7-
var args = minimist(process.argv.slice(2))
8+
const args = minimist(process.argv.slice(2))
9+
const first = args._[0]
810

9-
var inputStream
10-
11-
var first = args._[0]
1211
if (!first) {
1312
console.error('Usage: ndjson [input] <options>')
1413
process.exit(1)
1514
}
1615

17-
if (first === '-') inputStream = process.stdin
18-
else inputStream = fs.createReadStream(first)
16+
const inputStream = first === '-'
17+
? process.stdin
18+
: fs.createReadStream(first)
1919

20-
var parse = ndjson.parse(args)
21-
var serializer = ndjson.serialize(args)
22-
23-
inputStream.pipe(parse).pipe(serializer).pipe(process.stdout)
20+
pipeline(
21+
inputStream,
22+
ndjson.parse(args),
23+
ndjson.stringify(args),
24+
process.stdout,
25+
(err) => {
26+
err ? process.exit(1) : process.exit(0)
27+
}
28+
)

collaborators.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var through = require('through2')
2-
var split = require('split2')
3-
var EOL = require('os').EOL
4-
var stringify = require('json-stringify-safe')
1+
const through = require('through2')
2+
const split = require('split2')
3+
const { EOL } = require('os')
4+
const stringify = require('json-stringify-safe')
55

6-
module.exports = parse
7-
module.exports.serialize = module.exports.stringify = serialize
8-
module.exports.parse = parse
6+
module.exports.stringify = (opts) =>
7+
through.obj(opts, (obj, _, cb) => {
8+
cb(null, stringify(obj) + EOL)
9+
})
910

10-
function parse (opts) {
11+
module.exports.parse = (opts) => {
1112
opts = opts || {}
1213
opts.strict = opts.strict !== false
1314

@@ -22,10 +23,4 @@ function parse (opts) {
2223
}
2324

2425
return split(parseRow, opts)
25-
}
26-
27-
function serialize (opts) {
28-
return through.obj(opts, function(obj, enc, cb) {
29-
cb(null, stringify(obj) + EOL)
30-
})
31-
}
26+
}

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{
2-
"name": "ndjson-next",
3-
"version": "1.5.1",
4-
"description": "streaming newline delimited json parser + serializer",
2+
"name": "ndjson",
3+
"version": "2.0.0",
4+
"description": "Streaming newline delimited json parser + serializer",
55
"main": "index.js",
66
"scripts": {
7-
"test": "tape test.js"
7+
"test": "tape test/index.js"
88
},
99
"bin": {
1010
"ndjson": "cli.js"
1111
},
1212
"author": "max ogden",
1313
"license": "BSD-3-Clause",
14+
"engines": {
15+
"node": ">=10"
16+
},
1417
"dependencies": {
1518
"json-stringify-safe": "^5.0.1",
1619
"minimist": "^1.2.5",
20+
"readable-stream": "^3.6.0",
1721
"split2": "^3.0.0",
1822
"through2": "^4.0.0"
1923
},
@@ -23,12 +27,12 @@
2327
},
2428
"repository": {
2529
"type": "git",
26-
"url": "git://github.com/contra/ndjson.git"
30+
"url": "git://github.com/ndjson/ndjson.js.git"
2731
},
2832
"bugs": {
29-
"url": "https://github.com/contra/ndjson/issues"
33+
"url": "https://github.com/ndjson/ndjson.js/issues"
3034
},
31-
"homepage": "https://github.com/contra/ndjson",
35+
"homepage": "https://github.com/ndjson/ndjson.js",
3236
"keywords": [
3337
"ndjson",
3438
"ldjson"

readme.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
1-
# Fork Notes
1+
# ndjson
22

3-
- Updates all dependencies
4-
- Promises to stay maintained and responsive to PRs
3+
Streaming [newline delimited json](https://en.wikipedia.org/wiki/Line_Delimited_JSON) parser + serializer. Available as a JS API and a CLI.
54

6-
All thanks to the original maintainer Max Ogden.
5+
[![NPM](https://nodei.co/npm/ndjson.png)](https://nodei.co/npm/ndjson/)
76

8-
# ndjson-next
9-
10-
streaming [newline delimited json](https://en.wikipedia.org/wiki/Line_Delimited_JSON) parser + serializer. Available as a JS API or a command line tool
11-
12-
[![NPM](https://nodei.co/npm/ndjson-next.png)](https://nodei.co/npm/ndjson-next/)
13-
14-
## usage
7+
## Usage
158

169
```
17-
var ndjson = require('ndjson-next')
10+
const ndjson = require('ndjson')
1811
```
1912

20-
#### ndjson.parse(opts)
13+
#### ndjson.parse([opts])
2114

22-
returns a transform stream that accepts newline delimited json and emits objects
15+
Returns a transform stream that accepts newline delimited json buffers and emits objects of parsed data.
2316

24-
example newline delimited json:
25-
26-
`data.txt`:
17+
Example file:
2718

2819
```
2920
{"foo": "bar"}
3021
{"hello": "world"}
3122
```
3223

33-
If you want to discard non-valid JSON messages, you can call `ndjson.parse({strict: false})`
34-
35-
usage:
24+
Parsing it:
3625

3726
```js
3827
fs.createReadStream('data.txt')
@@ -42,9 +31,15 @@ fs.createReadStream('data.txt')
4231
})
4332
```
4433

45-
#### ndjson.serialize() / ndjson.stringify()
4634

47-
returns a transform stream that accepts json objects and emits newline delimited json
35+
##### Options
36+
37+
- `strict` can be set to false to discard non-valid JSON messages
38+
- All other options are passed through to the stream class.
39+
40+
#### ndjson.stringify([opts])
41+
42+
Returns a transform stream that accepts JSON objects and emits newline delimited json buffers.
4843

4944
example usage:
5045

@@ -57,6 +52,10 @@ serialize.write({"foo": "bar"})
5752
serialize.end()
5853
```
5954

60-
### license
55+
##### Options
56+
57+
Options are passed through to the stream class.
58+
59+
### LICENSE
6160

6261
BSD-3-Clause

test/data.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"foo": "bar"}
2+
{"hello": "world"}

test.js renamed to test/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var test = require('tape')
2-
var ndj = require('./')
3-
var os = require('os')
4-
var concat = require('concat-stream')
1+
const test = require('tape')
2+
const os = require('os')
3+
const concat = require('concat-stream')
4+
const ndj = require('../')
55

66
test('.parse', function(t) {
7-
var parser = ndj.parse()
7+
const parser = ndj.parse()
88
parser.on('data', function(obj) {
99
t.equal(obj.hello, 'world')
1010
t.end()
@@ -14,7 +14,7 @@ test('.parse', function(t) {
1414
})
1515

1616
test('.parse twice', function(t) {
17-
var parser = ndj.parse()
17+
const parser = ndj.parse()
1818
parser.once('data', function(obj) {
1919
t.equal(obj.hello, 'world')
2020
parser.once('data', function(obj) {
@@ -27,7 +27,7 @@ test('.parse twice', function(t) {
2727
})
2828

2929
test('.parse - strict:true error', function (t) {
30-
var parser = ndj.parse({strict: true})
30+
const parser = ndj.parse({strict: true})
3131
try {
3232
parser.write('{"no":"json"\n')
3333
} catch(e) {
@@ -37,7 +37,7 @@ test('.parse - strict:true error', function (t) {
3737
})
3838

3939
test('.parse - strict:true error event', function (t) {
40-
var parser = ndj.parse({strict: true})
40+
const parser = ndj.parse({strict: true})
4141
parser.on('error', function (err) {
4242
t.pass('error event called')
4343
t.end()
@@ -50,7 +50,7 @@ test('.parse - strict:true error event', function (t) {
5050
})
5151

5252
test('.parse - strict:false error', function (t) {
53-
var parser = ndj.parse({strict: false})
53+
const parser = ndj.parse({strict: false})
5454
parser.once('data', function (data) {
5555
t.ok(data.json, 'parse second one')
5656
t.end()
@@ -62,8 +62,8 @@ test('.parse - strict:false error', function (t) {
6262
}
6363
})
6464

65-
test('.serialize', function(t) {
66-
var serializer = ndj.serialize()
65+
test('.stringify', function(t) {
66+
const serializer = ndj.stringify()
6767
serializer.pipe(concat(function(data) {
6868
t.equal(data, '{"hello":"world"}' + os.EOL)
6969
t.end()
@@ -72,13 +72,13 @@ test('.serialize', function(t) {
7272
serializer.end()
7373
})
7474

75-
test('.serialize circular', function(t) {
76-
var serializer = ndj.serialize()
75+
test('.stringify circular', function(t) {
76+
const serializer = ndj.stringify()
7777
serializer.pipe(concat(function(data) {
7878
t.equal(data, '{"obj":"[Circular ~]"}' + os.EOL)
7979
t.end()
8080
}))
81-
var obj = {}
81+
const obj = {}
8282
obj.obj = obj
8383
serializer.write(obj)
8484
serializer.end()

0 commit comments

Comments
 (0)