jsonstream2
Advanced tools
Comparing version 1.1.0 to 1.1.1
#! /usr/bin/env node | ||
var jsonstream = require('..'); | ||
var jsonstream = require('..') | ||
process.stdin | ||
.pipe(jsonstream.parse(process.argv[2])) | ||
.pipe(jsonstream.stringify('[', ',\n', ']\n', 2)) | ||
.pipe(process.stdout); | ||
.pipe(jsonstream.parse(process.argv[2])) | ||
.pipe(jsonstream.stringify('[', ',\n', ']\n', 2)) | ||
.pipe(process.stdout) |
{ | ||
"name": "jsonstream2", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Dominic Tarr's JSONStream using Rodd Vaggs' through2", | ||
@@ -43,4 +43,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"event-stream": "^3.3.2", | ||
"it-is": "^1.0.3", | ||
"prova": "^1.14.0" | ||
} | ||
} |
@@ -1,41 +0,40 @@ | ||
var type = require('type-component'); | ||
var through = require('through2'); | ||
var Parser = require('jsonparse'); | ||
var type = require('type-component') | ||
var through = require('through2') | ||
var Parser = require('jsonparse') | ||
var check = function(x, y) { | ||
if (type(x) === 'string') { | ||
return y === x; | ||
return y === x | ||
} | ||
if (x && type(x.exec) === 'function') { | ||
return x.exec(y); | ||
return x.exec(y) | ||
} | ||
if (type(x) === 'boolean') { | ||
return x; | ||
return x | ||
} | ||
if (type(x) === 'function') { | ||
return x(y); | ||
return x(y) | ||
} | ||
return false; | ||
}; | ||
return false | ||
} | ||
module.exports.parse = function(path, map) { | ||
var parser = new Parser(); | ||
var parser = new Parser() | ||
var stream = through.obj(function(chunk, enc, fn) { | ||
if (type(chunk) === 'string') { | ||
chunk = new Buffer(chunk); | ||
chunk = new Buffer(chunk) | ||
} | ||
parser.write(chunk); | ||
fn(); | ||
}); | ||
parser.write(chunk) | ||
fn() | ||
}) | ||
if (type(path) === 'string') path = path.split('.').map(function(e) { | ||
if (e === '*') { | ||
return true; | ||
return true | ||
} | ||
@@ -46,10 +45,10 @@ | ||
recurse: true | ||
}; | ||
} | ||
} | ||
return e; | ||
}); | ||
return e | ||
}) | ||
if (!Array.isArray(path) || !path.length) { | ||
path = null; | ||
path = null | ||
} | ||
@@ -59,38 +58,38 @@ | ||
if (!this.root && this.stack.length === 1) { | ||
stream.root = this.value; | ||
stream.root = this.value | ||
} | ||
if (!path) { | ||
return; | ||
return | ||
} | ||
var i = 0; // iterates on path | ||
var j = 0; // iterates on stack | ||
var i = 0 // iterates on path | ||
var j = 0 // iterates on stack | ||
while (i < path.length) { | ||
var key = path[i]; | ||
var c; | ||
var key = path[i] | ||
var c | ||
j += 1; | ||
i += 1; | ||
j += 1 | ||
i += 1 | ||
if (key && !key.recurse) { | ||
c = (this.stack.length === j) ? this : this.stack[j]; | ||
c = (this.stack.length === j) ? this : this.stack[j] | ||
if (!c) { | ||
return; | ||
return | ||
} | ||
if (!check(key, c.key)) { | ||
return; | ||
return | ||
} | ||
} else { | ||
var next = path[i]; | ||
var next = path[i] | ||
if (!next) { | ||
return; | ||
return | ||
} | ||
while (true) { | ||
c = (this.stack.length === j) ? this : this.stack[j]; | ||
c = (this.stack.length === j) ? this : this.stack[j] | ||
@@ -102,8 +101,8 @@ if (!c) { | ||
if (check(next, c.key)) { | ||
this.stack[j].value = null; | ||
i += 1; | ||
break; | ||
this.stack[j].value = null | ||
i += 1 | ||
break | ||
} | ||
j += 1; | ||
j += 1 | ||
} | ||
@@ -114,29 +113,29 @@ } | ||
if (this.stack.length !== j) { | ||
return; | ||
return | ||
} | ||
var data = this.value[this.key]; | ||
var data = this.value[this.key] | ||
var actualPath = this.stack.slice(1).map(function(element) { | ||
return element.key; | ||
}).concat([this.key]); | ||
return element.key | ||
}).concat([this.key]) | ||
if (data !== null) { | ||
if ((data = map ? map(data, actualPath) : data) !== null) { | ||
stream.push(data); | ||
stream.push(data) | ||
} | ||
} | ||
delete this.value[this.key]; | ||
delete this.value[this.key] | ||
for (var k in this.stack) { | ||
this.stack[k].value = null; | ||
this.stack[k].value = null | ||
} | ||
}; | ||
} | ||
parser.onError = function(err) { | ||
stream.emit('error', err); | ||
}; | ||
stream.emit('error', err) | ||
} | ||
parser._onToken = parser.onToken; | ||
parser._onToken = parser.onToken | ||
parser.onToken = function(token, value) { | ||
parser._onToken(token, value); | ||
parser._onToken(token, value) | ||
if (this.stack.length === 0) { | ||
@@ -148,77 +147,83 @@ if (stream.root) { | ||
stream.emit('root', stream.root); | ||
stream.root = null; | ||
stream.emit('root', stream.root) | ||
stream.root = null | ||
} | ||
} | ||
}; | ||
} | ||
return stream; | ||
}; | ||
return stream | ||
} | ||
module.exports.stringify = function(open, sep, close, indent) { | ||
indent = indent || 0; | ||
indent = indent || 0 | ||
if (open === false) { | ||
open = ''; | ||
sep = '\n'; | ||
close = ''; | ||
open = '' | ||
sep = '\n' | ||
close = '' | ||
} else if (['null', 'undefined'].indexOf(type(open)) >= 0) { | ||
open = '[\n'; | ||
sep = '\n,\n'; | ||
close = '\n]\n'; | ||
open = '[\n' | ||
sep = '\n,\n' | ||
close = '\n]\n' | ||
} | ||
var anyData = false; | ||
var first = true; | ||
var first = true | ||
return through.obj(function(chunk, enc, fn) { | ||
var json = JSON.stringify(chunk, null, indent); | ||
anyData = true; | ||
var json = JSON.stringify(chunk, null, indent) | ||
if (!first) { | ||
this.push(sep + json); | ||
this.push(sep + json) | ||
} else { | ||
first = false; | ||
this.push(open + json); | ||
first = false | ||
this.push(open + json) | ||
} | ||
fn(); | ||
fn() | ||
}, function(fn) { | ||
this.push(close); | ||
fn(); | ||
}); | ||
}; | ||
if (first) { | ||
this.push(open) | ||
} | ||
exports.stringifyObject = function (open, sep, close, indent) { | ||
indent = indent || 0; | ||
first = false | ||
this.push(close) | ||
fn() | ||
}) | ||
} | ||
exports.stringifyObject = function(open, sep, close, indent) { | ||
indent = indent || 0 | ||
if (open === false) { | ||
open = ''; | ||
sep = '\n'; | ||
close = ''; | ||
open = '' | ||
sep = '\n' | ||
close = '' | ||
} else if (['null', 'undefined'].indexOf(type(open)) >= 0) { | ||
open = '{\n'; | ||
sep = '\n,\n'; | ||
close = '\n}\n'; | ||
open = '{\n' | ||
sep = '\n,\n' | ||
close = '\n}\n' | ||
} | ||
var anyData = false; | ||
var first = true; | ||
var first = true | ||
return through.obj(function(chunk, enc, fn) { | ||
var json = JSON.stringify(chunk[0]) + ':' + JSON.stringify(chunk[1], null, indent); | ||
anyData = true; | ||
var json = JSON.stringify(chunk[0]) + ':' + JSON.stringify(chunk[1], null, indent) | ||
if (!first) { | ||
this.push(sep + json); | ||
this.push(sep + json) | ||
} else { | ||
first = false; | ||
this.push(open + json); | ||
first = false | ||
this.push(open + json) | ||
} | ||
fn(); | ||
fn() | ||
}, function(fn) { | ||
this.push(close); | ||
fn(); | ||
}); | ||
}; | ||
if (first) { | ||
this.push(open) | ||
} | ||
first = false | ||
this.push(close) | ||
fn() | ||
}) | ||
} |
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
384788
4669
3