Skip to content

Commit d396445

Browse files
fix(non-blocking-json): start batching after index 0
1 parent 8ef221d commit d396445

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

packages/node_modules/non-blocking-json/src/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ test("should handle more scenarios", async () => {
4848
const obj = JSON.parse(string)
4949
const result = await stringify(obj)
5050
expect(result).toBe(string)
51+
})
52+
53+
test("should handle crazy scenario", async () => {
54+
const string = '{"foo":"\\"hey {}\\""}'
55+
const obj = JSON.parse(string)
56+
const result = await parse(string)
57+
expect(result).toEqual(obj)
5158
})

packages/node_modules/non-blocking-json/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function stringify(value, replacer?) {
7979
}
8080

8181
for (let [key, value] of paths) {
82-
if (x % ASYNC_BATCH_COUNT === 0) {
82+
if (x && x % ASYNC_BATCH_COUNT === 0) {
8383
if (TRACK_PERF) {
8484
const end = performance.now()
8585
console.log("PERF - ", end - start)
@@ -162,15 +162,16 @@ export async function parse(value) {
162162
}
163163

164164
for (let charIndex = 0; charIndex < value.length; charIndex++) {
165-
if (charIndex % ASYNC_BATCH_COUNT === 0) {
165+
if (charIndex && charIndex % ASYNC_BATCH_COUNT === 0) {
166166
if (TRACK_PERF) {
167167
const end = performance.now()
168168
console.log("PERF - ", end - start)
169169
start = end
170170
}
171171
await new Promise(resolve => next(resolve));
172172
}
173-
if (value[charIndex] === '"') {
173+
174+
if (value[charIndex] === '"' && value[charIndex - 1] !== '\\') {
174175
isInString = !isInString;
175176
continue;
176177
}
@@ -216,7 +217,7 @@ export async function parse(value) {
216217

217218
const keys = Object.keys(parsedValue);
218219

219-
if (i % ASYNC_BATCH_COUNT === 0) {
220+
if (i && i % ASYNC_BATCH_COUNT === 0) {
220221
await new Promise(resolve => next(resolve));
221222
}
222223

0 commit comments

Comments
 (0)