From b219e608dcdc8d8b69405a0bb98002c9e5dd3206 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 24 Jan 2026 14:28:38 +0100 Subject: [PATCH 1/6] perf: removed condition last-1 Signed-off-by: Nigro Simone --- lib/serializer.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index d8ff4b87..979cc2d4 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -102,7 +102,7 @@ module.exports = class Serializer { // 34 and 92 happens all the time, so we // have a fast case for them let result = '' - let last = -1 + let last = 0 let point = 255 for (let i = 0; i < len; i++) { point = str.charCodeAt(i) @@ -110,7 +110,6 @@ module.exports = class Serializer { point === 0x22 || // '"' point === 0x5c // '\' ) { - last === -1 && (last = 0) result += str.slice(last, i) + '\\' last = i } else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) { From be2fdc7ec5c72b126138f999c704330aa25d4bae Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 24 Jan 2026 14:43:30 +0100 Subject: [PATCH 2/6] feat: add test --- test/string.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/string.test.js b/test/string.test.js index 518513da..0d9eef04 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -82,3 +82,20 @@ test('unsafe unescaped string', (t) => { JSON.parse(output) }) }) + +test('multiple quote', (t) => { + t.plan(2) + + const schema = { + type: 'string' + } + + const input = '"ab\\cd"ef' + const stringify = build(schema) + const output = stringify(input) + + t.assert.equal(output, `"${input}"`) + t.assert.throws(function () { + JSON.parse(output) + }) +}) From 4f7f65737ddd7ddb9086d8066378998373e84003 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 24 Jan 2026 14:45:39 +0100 Subject: [PATCH 3/6] Revert "perf: removed condition last-1" This reverts commit b219e608dcdc8d8b69405a0bb98002c9e5dd3206. --- lib/serializer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/serializer.js b/lib/serializer.js index 979cc2d4..d8ff4b87 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -102,7 +102,7 @@ module.exports = class Serializer { // 34 and 92 happens all the time, so we // have a fast case for them let result = '' - let last = 0 + let last = -1 let point = 255 for (let i = 0; i < len; i++) { point = str.charCodeAt(i) @@ -110,6 +110,7 @@ module.exports = class Serializer { point === 0x22 || // '"' point === 0x5c // '\' ) { + last === -1 && (last = 0) result += str.slice(last, i) + '\\' last = i } else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) { From 6a28f27b858f811f93fef936745b2308f8a0c5c7 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 24 Jan 2026 14:51:18 +0100 Subject: [PATCH 4/6] fix: test --- test/string.test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/string.test.js b/test/string.test.js index 0d9eef04..58a44ce4 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -83,7 +83,7 @@ test('unsafe unescaped string', (t) => { }) }) -test('multiple quote', (t) => { +test('multiple sequential escape', (t) => { t.plan(2) const schema = { @@ -95,7 +95,4 @@ test('multiple quote', (t) => { const output = stringify(input) t.assert.equal(output, `"${input}"`) - t.assert.throws(function () { - JSON.parse(output) - }) }) From 458c463620067df43629aa7124239f3f83cde59b Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 24 Jan 2026 14:52:46 +0100 Subject: [PATCH 5/6] fix: plan number --- test/string.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/string.test.js b/test/string.test.js index 58a44ce4..c1394062 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -84,7 +84,7 @@ test('unsafe unescaped string', (t) => { }) test('multiple sequential escape', (t) => { - t.plan(2) + t.plan(1) const schema = { type: 'string' From a3c30f664546721d0eff63c7a4e5c210326bf49a Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sat, 24 Jan 2026 14:53:26 +0100 Subject: [PATCH 6/6] feat: add parse --- test/string.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/string.test.js b/test/string.test.js index c1394062..5772d77f 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -84,7 +84,7 @@ test('unsafe unescaped string', (t) => { }) test('multiple sequential escape', (t) => { - t.plan(1) + t.plan(2) const schema = { type: 'string' @@ -95,4 +95,5 @@ test('multiple sequential escape', (t) => { const output = stringify(input) t.assert.equal(output, `"${input}"`) + t.assert.equal(JSON.parse(output), input) })