react / wstein / node_modules / jest-cli / node_modules / node-worker-pool / lib / JSONStreamParser.js
80665 views"use strict";12function JSONStreamParser() {3this._currentlyWithinQuotedString = false;4this._depth = 0;5this._buffer = '';6}78JSONStreamParser.prototype.parse=function(streamText) {9var cursor = this._buffer.length;10this._buffer += streamText;11var currChar;12var responses = [];13while (cursor < this._buffer.length) {14currChar = this._buffer.charAt(cursor);15if (this._currentlyWithinQuotedString && currChar === '\\') {16// If the current character is escaped, move forward17cursor++;18} else if (currChar === '"') {19// Are we inside a quoted string?20this._currentlyWithinQuotedString = !this._currentlyWithinQuotedString;21} else if (!this._currentlyWithinQuotedString) {22if (currChar === '{') {23this._depth++;24} else if (currChar === '}') {25this._depth--;26if (this._depth === 0) {27responses.push(JSON.parse(this._buffer.substring(0, cursor + 1)));28this._buffer = this._buffer.substring(cursor + 1);29cursor = 0;30continue;31}32}33}34cursor++;35}36return responses;37};3839JSONStreamParser.prototype.getBuffer=function() {40return this._buffer;41}4243module.exports = JSONStreamParser;444546