react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / module-deps / node_modules / JSONStream / index.js
80736 views#! /usr/bin/env node12var Parser = require('jsonparse')3, through = require('through')45/*67the value of this.stack that creationix's jsonparse has is weird.89it makes this code ugly, but his problem is way harder that mine,10so i'll forgive him.1112*/1314exports.parse = function (path, map) {1516var parser = new Parser()17var stream = through(function (chunk) {18if('string' === typeof chunk)19chunk = new Buffer(chunk)20parser.write(chunk)21},22function (data) {23if(data)24stream.write(data)25stream.queue(null)26})2728if('string' === typeof path)29path = path.split('.').map(function (e) {30if (e === '*')31return true32else if (e === '') // '..'.split('.') returns an empty string33return {recurse: true}34else35return e36})373839var count = 0, _key40if(!path || !path.length)41path = null4243parser.onValue = function (value) {44if (!this.root)45stream.root = value4647if(! path) return4849var i = 0 // iterates on path50var j = 0 // iterates on stack51while (i < path.length) {52var key = path[i]53var c54j++5556if (key && !key.recurse) {57c = (j === this.stack.length) ? this : this.stack[j]58if (!c) return59if (! check(key, c.key)) return60i++61} else {62i++63var nextKey = path[i]64if (! nextKey) return65while (true) {66c = (j === this.stack.length) ? this : this.stack[j]67if (!c) return68if (check(nextKey, c.key)) {69i++;70this.stack[j].value = null71break72}73j++74}75}7677}78if (j !== this.stack.length) return7980count ++81var actualPath = this.stack.slice(1).map(function(element) { return element.key }).concat([this.key])82var data = this.value[this.key]83if(null != data)84if(null != (data = map ? map(data, actualPath) : data))85stream.queue(data)86delete this.value[this.key]87for(var k in this.stack)88this.stack[k].value = null89}90parser._onToken = parser.onToken;9192parser.onToken = function (token, value) {93parser._onToken(token, value);94if (this.stack.length === 0) {95if (stream.root) {96if(!path)97stream.queue(stream.root)98count = 0;99stream.root = null;100}101}102}103104parser.onError = function (err) {105if(err.message.indexOf("at position") > -1)106err.message = "Invalid JSON (" + err.message + ")";107stream.emit('error', err)108}109110111return stream112}113114function check (x, y) {115if ('string' === typeof x)116return y == x117else if (x && 'function' === typeof x.exec)118return x.exec(y)119else if ('boolean' === typeof x)120return x121else if ('function' === typeof x)122return x(y)123return false124}125126exports.stringify = function (op, sep, cl, indent) {127indent = indent || 0128if (op === false){129op = ''130sep = '\n'131cl = ''132} else if (op == null) {133134op = '[\n'135sep = '\n,\n'136cl = '\n]\n'137138}139140//else, what ever you like141142var stream143, first = true144, anyData = false145stream = through(function (data) {146anyData = true147var json = JSON.stringify(data, null, indent)148if(first) { first = false ; stream.queue(op + json)}149else stream.queue(sep + json)150},151function (data) {152if(!anyData)153stream.queue(op)154stream.queue(cl)155stream.queue(null)156})157158return stream159}160161exports.stringifyObject = function (op, sep, cl, indent) {162indent = indent || 0163if (op === false){164op = ''165sep = '\n'166cl = ''167} else if (op == null) {168169op = '{\n'170sep = '\n,\n'171cl = '\n}\n'172173}174175//else, what ever you like176177var first = true178, anyData = false179stream = through(function (data) {180anyData = true181var json = JSON.stringify(data[0]) + ':' + JSON.stringify(data[1], null, indent)182if(first) { first = false ; this.queue(op + json)}183else this.queue(sep + json)184},185function (data) {186if(!anyData) this.queue(op)187this.queue(cl)188189this.queue(null)190})191192return stream193}194195if(!module.parent && process.title !== 'browser') {196process.stdin197.pipe(exports.parse(process.argv[2]))198.pipe(exports.stringify('[', ',\n', ']\n', 2))199.pipe(process.stdout)200}201202203