Socket
Socket
Sign inDemoInstall

ndjson

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ndjson - npm Package Compare versions

Comparing version 1.4.0 to 1.4.1

22

index.js

@@ -27,23 +27,5 @@ var through = require('through2')

function serialize (opts) {
if (!opts) opts = {}
if (!opts.separator) opts.separator = EOL
if (!opts.after) opts.after = EOL
var firstWrite = true
var serializer = through.obj(function write (obj, enc, cb) {
var out = ""
if (firstWrite) {
if (typeof opts.before !== 'undefined') out += opts.before
out += JSON.stringify(obj)
firstWrite = false
} else {
out += opts.separator
out += JSON.stringify(obj)
}
cb(null, out)
}, function end (cb) {
if (typeof opts.after !== 'undefined') serializer.push(opts.after)
cb()
return through.obj(function(obj, enc, cb) {
cb(null, JSON.stringify(obj) + EOL)
})
return serializer
}

2

package.json
{
"name": "ndjson",
"version": "1.4.0",
"version": "1.4.1",
"description": "streaming newline delimited json parser + serializer",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -38,3 +38,3 @@ # ndjson

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

@@ -54,33 +54,4 @@ returns a transform stream that accepts json objects and emits newline delimited json

##### Options
- `before` - a string to put before the stream
- `after` - a string to put after the stream, defaults to `os.EOL` (e.g. `\n`)
- `separator` - a string to put between items in the stream, defaults to `os.EOL` (e.g. `\n`)
### CLI usage
You can use the `ndjson` CLI tool to validate ndjson input, and serialize ndjson into a custom JSON output
```
Usage: ndjson [input] <options>
```
Install it globally:
```
npm i ndjson -g
```
Pass `-` to use STDIN, otherwise pass a file as input
Example:
```
echo '{"foo": "bar"}' | ndjson --before=CATS --after=DOGS -
CATS{"foo":"bar"}DOGS
```
### license
BSD-3-Clause

@@ -74,22 +74,1 @@ var test = require('tape')

})
test('.serialize custom before after', function(t) {
var serializer = ndj.serialize({before: 'CATS', after: 'DOGS'})
serializer.pipe(concat(function(data) {
t.equal(data, 'CATS{"hello":"world"}DOGS')
t.end()
}))
serializer.write({hello: 'world'})
serializer.end()
})
test('.serialize custom separator', function(t) {
var serializer = ndj.serialize({separator: 'CATS'})
serializer.pipe(concat(function(data) {
t.equal(data, '{"hello":"world"}CATS{"hej":"verden"}\n')
t.end()
}))
serializer.write({hello: 'world'})
serializer.write({hej: 'verden'})
serializer.end()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc