react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / JSONStream / index.js
80713 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 () {44if (!this.root && this.stack.length == 1)45stream.root = this.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)) { i++; break}69j++70}71}72}73if (j !== this.stack.length) return7475count ++76var data = this.value[this.key]77if(null != data)78if(null != (data = map ? map(data) : data))79stream.queue(data)80delete this.value[this.key]81}82parser._onToken = parser.onToken;8384parser.onToken = function (token, value) {85parser._onToken(token, value);86if (this.stack.length === 0) {87if (stream.root) {88if(!path)89stream.queue(stream.root)90stream.emit('root', stream.root, count)91count = 0;92stream.root = null;93}94}95}9697parser.onError = function (err) {98stream.emit('error', err)99}100101102return stream103}104105function check (x, y) {106if ('string' === typeof x)107return y == x108else if (x && 'function' === typeof x.exec)109return x.exec(y)110else if ('boolean' === typeof x)111return x112else if ('function' === typeof x)113return x(y)114return false115}116117exports.stringify = function (op, sep, cl, indent) {118indent = indent || 0119if (op === false){120op = ''121sep = '\n'122cl = ''123} else if (op == null) {124125op = '[\n'126sep = '\n,\n'127cl = '\n]\n'128129}130131//else, what ever you like132133var stream134, first = true135, anyData = false136stream = through(function (data) {137anyData = true138var json = JSON.stringify(data, null, indent)139if(first) { first = false ; stream.queue(op + json)}140else stream.queue(sep + json)141},142function (data) {143if(!anyData)144stream.queue(op)145stream.queue(cl)146stream.queue(null)147})148149return stream150}151152exports.stringifyObject = function (op, sep, cl, indent) {153indent = indent || 0154if (op === false){155op = ''156sep = '\n'157cl = ''158} else if (op == null) {159160op = '{\n'161sep = '\n,\n'162cl = '\n}\n'163164}165166//else, what ever you like167168var first = true169, anyData = false170stream = through(function (data) {171anyData = true172var json = JSON.stringify(data[0]) + ':' + JSON.stringify(data[1], null, indent)173if(first) { first = false ; this.queue(op + json)}174else this.queue(sep + json)175},176function (data) {177if(!anyData) this.queue(op)178this.queue(cl)179180this.queue(null)181})182183return stream184}185186if(!module.parent && process.title !== 'browser') {187process.stdin188.pipe(exports.parse(process.argv[2]))189.pipe(exports.stringify('[', ',\n', ']\n', 2))190.pipe(process.stdout)191}192193194