Skip to content

Commit

Permalink
chore: code cleanup, new docs, API cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yocontra committed Aug 15, 2020
1 parent 5540123 commit dbc6ce2
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
package-lock.json
package-lock.json
yarn.lock
31 changes: 18 additions & 13 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
#!/usr/bin/env node

var fs = require('fs')
var ndjson = require('./index.js')
var minimist = require('minimist')
const fs = require('fs')
const { pipeline } = require('readable-stream')
const minimist = require('minimist')
const ndjson = require('./index.js')

var args = minimist(process.argv.slice(2))
const args = minimist(process.argv.slice(2))
const first = args._[0]

var inputStream

var first = args._[0]
if (!first) {
console.error('Usage: ndjson [input] <options>')
process.exit(1)
}

if (first === '-') inputStream = process.stdin
else inputStream = fs.createReadStream(first)
const inputStream = first === '-'
? process.stdin
: fs.createReadStream(first)

var parse = ndjson.parse(args)
var serializer = ndjson.serialize(args)

inputStream.pipe(parse).pipe(serializer).pipe(process.stdout)
pipeline(
inputStream,
ndjson.parse(args),
ndjson.stringify(args),
process.stdout,
(err) => {
err ? process.exit(1) : process.exit(0)
}
)
7 changes: 0 additions & 7 deletions collaborators.md

This file was deleted.

25 changes: 10 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var through = require('through2')
var split = require('split2')
var EOL = require('os').EOL
var stringify = require('json-stringify-safe')
const through = require('through2')
const split = require('split2')
const { EOL } = require('os')
const stringify = require('json-stringify-safe')

module.exports = parse
module.exports.serialize = module.exports.stringify = serialize
module.exports.parse = parse
module.exports.stringify = (opts) =>
through.obj(opts, (obj, _, cb) => {
cb(null, stringify(obj) + EOL)
})

function parse (opts) {
module.exports.parse = (opts) => {
opts = opts || {}
opts.strict = opts.strict !== false

Expand All @@ -22,10 +23,4 @@ function parse (opts) {
}

return split(parseRow, opts)
}

function serialize (opts) {
return through.obj(opts, function(obj, enc, cb) {
cb(null, stringify(obj) + EOL)
})
}
}
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "ndjson-next",
"version": "1.5.1",
"description": "streaming newline delimited json parser + serializer",
"name": "ndjson",
"version": "2.0.0",
"description": "Streaming newline delimited json parser + serializer",
"main": "index.js",
"scripts": {
"test": "tape test.js"
"test": "tape test/index.js"
},
"bin": {
"ndjson": "cli.js"
},
"author": "max ogden",
"license": "BSD-3-Clause",
"engines": {
"node": ">=10"
},
"dependencies": {
"json-stringify-safe": "^5.0.1",
"minimist": "^1.2.5",
"readable-stream": "^3.6.0",
"split2": "^3.0.0",
"through2": "^4.0.0"
},
Expand All @@ -23,12 +27,12 @@
},
"repository": {
"type": "git",
"url": "git://github.com/contra/ndjson.git"
"url": "git://github.com/ndjson/ndjson.js.git"
},
"bugs": {
"url": "https://github.com/contra/ndjson/issues"
"url": "https://github.com/ndjson/ndjson.js/issues"
},
"homepage": "https://github.com/contra/ndjson",
"homepage": "https://github.com/ndjson/ndjson.js",
"keywords": [
"ndjson",
"ldjson"
Expand Down
45 changes: 22 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
# Fork Notes
# ndjson

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

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

# ndjson-next

streaming [newline delimited json](https://en.wikipedia.org/wiki/Line_Delimited_JSON) parser + serializer. Available as a JS API or a command line tool

[![NPM](https://nodei.co/npm/ndjson-next.png)](https://nodei.co/npm/ndjson-next/)

## usage
## Usage

```
var ndjson = require('ndjson-next')
const ndjson = require('ndjson')
```

#### ndjson.parse(opts)
#### ndjson.parse([opts])

returns a transform stream that accepts newline delimited json and emits objects
Returns a transform stream that accepts newline delimited json buffers and emits objects of parsed data.

example newline delimited json:

`data.txt`:
Example file:

```
{"foo": "bar"}
{"hello": "world"}
```

If you want to discard non-valid JSON messages, you can call `ndjson.parse({strict: false})`

usage:
Parsing it:

```js
fs.createReadStream('data.txt')
Expand All @@ -42,9 +31,15 @@ fs.createReadStream('data.txt')
})
```

#### ndjson.serialize() / ndjson.stringify()

returns a transform stream that accepts json objects and emits newline delimited json
##### Options

- `strict` can be set to false to discard non-valid JSON messages
- All other options are passed through to the stream class.

#### ndjson.stringify([opts])

Returns a transform stream that accepts JSON objects and emits newline delimited json buffers.

example usage:

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

### license
##### Options

Options are passed through to the stream class.

### LICENSE

BSD-3-Clause
2 changes: 2 additions & 0 deletions test/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"foo": "bar"}
{"hello": "world"}
28 changes: 14 additions & 14 deletions test.js → test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var test = require('tape')
var ndj = require('./')
var os = require('os')
var concat = require('concat-stream')
const test = require('tape')
const os = require('os')
const concat = require('concat-stream')
const ndj = require('../')

test('.parse', function(t) {
var parser = ndj.parse()
const parser = ndj.parse()
parser.on('data', function(obj) {
t.equal(obj.hello, 'world')
t.end()
Expand All @@ -14,7 +14,7 @@ test('.parse', function(t) {
})

test('.parse twice', function(t) {
var parser = ndj.parse()
const parser = ndj.parse()
parser.once('data', function(obj) {
t.equal(obj.hello, 'world')
parser.once('data', function(obj) {
Expand All @@ -27,7 +27,7 @@ test('.parse twice', function(t) {
})

test('.parse - strict:true error', function (t) {
var parser = ndj.parse({strict: true})
const parser = ndj.parse({strict: true})
try {
parser.write('{"no":"json"\n')
} catch(e) {
Expand All @@ -37,7 +37,7 @@ test('.parse - strict:true error', function (t) {
})

test('.parse - strict:true error event', function (t) {
var parser = ndj.parse({strict: true})
const parser = ndj.parse({strict: true})
parser.on('error', function (err) {
t.pass('error event called')
t.end()
Expand All @@ -50,7 +50,7 @@ test('.parse - strict:true error event', function (t) {
})

test('.parse - strict:false error', function (t) {
var parser = ndj.parse({strict: false})
const parser = ndj.parse({strict: false})
parser.once('data', function (data) {
t.ok(data.json, 'parse second one')
t.end()
Expand All @@ -62,8 +62,8 @@ test('.parse - strict:false error', function (t) {
}
})

test('.serialize', function(t) {
var serializer = ndj.serialize()
test('.stringify', function(t) {
const serializer = ndj.stringify()
serializer.pipe(concat(function(data) {
t.equal(data, '{"hello":"world"}' + os.EOL)
t.end()
Expand All @@ -72,13 +72,13 @@ test('.serialize', function(t) {
serializer.end()
})

test('.serialize circular', function(t) {
var serializer = ndj.serialize()
test('.stringify circular', function(t) {
const serializer = ndj.stringify()
serializer.pipe(concat(function(data) {
t.equal(data, '{"obj":"[Circular ~]"}' + os.EOL)
t.end()
}))
var obj = {}
const obj = {}
obj.obj = obj
serializer.write(obj)
serializer.end()
Expand Down

0 comments on commit dbc6ce2

Please sign in to comment.