Skip to content
Browse files

take the pos from charError

  • Loading branch information...
1 parent 6d9df4c commit 60f3c448bfd08d2fac9425e361e67fe63fdb9b75 @substack committed Aug 14, 2012
Showing with 19 additions and 19 deletions.
  1. +16 −17 index.js
  2. +2 −1 test/messy.js
  3. +1 −1 test/messy.txt
View
33 index.js
@@ -4,39 +4,39 @@ var Parser = require('jsonparse');
module.exports = function (opts) {
if (!opts) opts = {};
- var parser, value, error;
+ var parser, value, pos;
function createParser () {
parser = new Parser;
parser.onValue = function () {
if (this.value !== undefined) {
+ if (value !== undefined && value !== this.value) {
+ //stream.emit('data', value);
+ }
value = this.value;
}
};
+ parser.onError = function () {};
- parser.onError = function (err) {
- if (!error) {
- error = err;
+ parser.charError = function (buf, i) {
+ if (pos === undefined) {
+ pos = i;
if (value !== undefined) {
stream.emit('data', value);
+ value = undefined;
}
}
- value = undefined;
};
}
function write (buf) {
if (parser) {
parser.write(buf);
- if (error) {
- var m = String(error).match(/position (\d+)/);
+ if (pos !== undefined) {
parser = undefined;
- error = undefined;
-
- if (m) {
- pos = parseInt(m[1], 10);
- write(buf.slice(pos, buf.length));
- }
+ var pos_ = pos;
+ pos = undefined;
+ write(buf.slice(pos_, buf.length));
}
return;
}
@@ -46,11 +46,10 @@ module.exports = function (opts) {
if (s === '[' || s === '{') {
createParser();
parser.write(buf.slice(i, buf.length));
- if (!error) break;
- var m = String(error).match(/position (\d+)/);
- if (m) i += parseInt(m[1], 10);
+ if (pos === undefined) break;
+ i += pos;
- error = undefined;
+ pos = undefined;
parser = undefined;
}
}
View
3 test/messy.js
@@ -3,7 +3,7 @@ var createScraper = require('../');
var fs = require('fs');
var chunky = require('chunky');
-var TIMES = 1;
+var TIMES = 100;
test('messy errorful noise', function (t) {
t.plan(TIMES);
@@ -18,6 +18,7 @@ test('messy errorful noise', function (t) {
t.same(objects, [
{ beep : 'boop' },
[ 7, 8, 9 ],
+ [ 4, 'a' ],
{ x : 10 },
]);
});
View
2 test/messy.txt
@@ -6,4 +6,4 @@ libnibblist { "beep" : "boop" } flim-flammery [
quipper-glubbility [7,8,9]
-["a","b",]{"x":10}
+[4,"a",qux]{"x":10}

0 comments on commit 60f3c44

Please sign in to comment.
Something went wrong with that request. Please try again.