Comparing version 0.1.0 to 0.1.1
var Parser = require('./lib/parser') | ||
var Stringifier = require('./lib/stringifier') | ||
exports.parse = function parse () { | ||
return new Parser() | ||
exports.parse = function parse (options) { | ||
return new Parser(options) | ||
} | ||
@@ -7,0 +7,0 @@ |
var Transform = require('stream').Transform | ||
function Parser () { | ||
function Parser (options) { | ||
if (!(this instanceof Parser)) { | ||
@@ -8,4 +8,7 @@ throw new TypeError('Cannot call a class as a function') | ||
options = options || {} | ||
Transform.call(this, { objectMode: true }) | ||
this._memory = '' | ||
this._emitInvalidLines = (options.emitInvalidLines || false) | ||
} | ||
@@ -19,11 +22,20 @@ | ||
var json | ||
var err = null | ||
var json = null | ||
try { | ||
json = JSON.parse(lines[i]) | ||
} catch (err) { | ||
err.source = lines[i] | ||
return cb(err) | ||
} catch (_err) { | ||
_err.source = lines[i] | ||
err = _err | ||
} | ||
this.push(json) | ||
if (err) { | ||
if (this._emitInvalidLines) { | ||
this.emit('invalid-line', err) | ||
} else { | ||
return cb(err) | ||
} | ||
} else { | ||
this.push(json) | ||
} | ||
} | ||
@@ -30,0 +42,0 @@ |
{ | ||
"name": "jsonlines", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Linus Unnebäck <linus@folkdatorn.se>", |
@@ -43,3 +43,3 @@ # JSONLines for Node.js | ||
### `.parse()` | ||
### `.parse([options])` | ||
@@ -49,2 +49,4 @@ Returns a transform stream that turns newline separated json into a stream of | ||
`options` is an optional object with the keys documented below. | ||
### `.stringify()` | ||
@@ -54,1 +56,35 @@ | ||
separated json. | ||
## Options | ||
### `emitInvalidLine` | ||
If true, instead of emitting an error and cancelling the stream when an invalid line is proccessed, an `invalid-line` event is emitted with the same error. This is very useful when processing text that have mixed plain text and json data. | ||
Example: | ||
```js | ||
var jsonlines = require('jsonlines') | ||
var parser = jsonlines.parse({ emitInvalidLines: true }) | ||
parser.on('data', function (data) { | ||
console.log('Got json:', data) | ||
}) | ||
parser.on('invalid-line', function (err) { | ||
console.log('Got text:', err.source) | ||
}) | ||
parser.write('{ "test": "This is a test!" }\n') | ||
parser.write('This is some plain text\n') | ||
parser.write('{ "jsonlines": "is awesome" }') | ||
parser.end() | ||
``` | ||
Output: | ||
```text | ||
Got json: { test: 'This is a test!' } | ||
Got text: This is some plain text | ||
Got json: { jsonlines: 'is awesome' } | ||
``` |
@@ -95,2 +95,22 @@ /* eslint-env mocha */ | ||
}) | ||
it('should emit invalid lines', function (done) { | ||
var parser = jsonlines.parse({ emitInvalidLines: true }) | ||
var data = '"works"\nbroken\n"ok"' | ||
parser.once('data', function (data) { | ||
assert.equal(data, 'works') | ||
parser.once('invalid-line', function (err) { | ||
assert.equal(err.source, 'broken') | ||
parser.once('data', function (data) { | ||
assert.equal(data, 'ok') | ||
done() | ||
}) | ||
}) | ||
}) | ||
parser.end(data) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8428
211
88