From 4f884f2ca8ff5b59453f3bdd9d90899cb7219764 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 12:39:45 +0200 Subject: [PATCH 1/5] perf: avoid addComma on empty array Just a small performance improvements when an array has only one item Signed-off-by: francesco --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 1bcb115d..3695b3f7 100644 --- a/index.js +++ b/index.js @@ -369,7 +369,7 @@ function buildInnerObject (context, location) { code += 'let json = JSON_STR_BEGIN_OBJECT\n' let addComma = '' - if (!hasRequiredProperties) { + if (!hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties))) { code += 'let addComma = false\n' addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)' } From 5fde9b621661b471dfc631f6039f6dfe5cf4eb5c Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 16:01:37 +0200 Subject: [PATCH 2/5] perf: avoid other addComma Signed-off-by: francesco --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3695b3f7..13185240 100644 --- a/index.js +++ b/index.js @@ -369,7 +369,8 @@ function buildInnerObject (context, location) { code += 'let json = JSON_STR_BEGIN_OBJECT\n' let addComma = '' - if (!hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties))) { + const hasComma = !hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties)) + if (hasComma) { code += 'let addComma = false\n' addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)' } @@ -387,14 +388,14 @@ function buildInnerObject (context, location) { code += ` value = obj[${sanitizedKey}] if (value !== undefined) { - ${addComma} + ${i > 0 ? addComma : (hasComma ? 'addComma = true' : '')} json += ${JSON.stringify(sanitizedKey + ':')} ${buildValue(context, propertyLocation, 'value')} }` if (defaultValue !== undefined) { code += ` else { - ${addComma} + ${i > 0 ? addComma : (hasComma ? 'addComma = true' : '')} json += ${JSON.stringify(sanitizedKey + ':' + JSON.stringify(defaultValue))} } ` From 6932828f57aa623b8203de3a39cec933e211b468 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 16:34:34 +0200 Subject: [PATCH 3/5] fix: lint Signed-off-by: francesco --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 13185240..285e2a8d 100644 --- a/index.js +++ b/index.js @@ -375,7 +375,8 @@ function buildInnerObject (context, location) { addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)' } - for (const key of propertiesKeys) { + for (let i = 0; i < propertiesKeys.length; i++) { + const key = propertiesKeys[i] let propertyLocation = propertiesLocation.getPropertyLocation(key) if (propertyLocation.schema.$ref) { propertyLocation = resolveRef(context, propertyLocation) From a1fade334c05a666ef77ded5816535bc9196fe14 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 16:49:51 +0200 Subject: [PATCH 4/5] fix: compliant with json schema Signed-off-by: francesco --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 285e2a8d..b32e3d05 100644 --- a/index.js +++ b/index.js @@ -369,7 +369,7 @@ function buildInnerObject (context, location) { code += 'let json = JSON_STR_BEGIN_OBJECT\n' let addComma = '' - const hasComma = !hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties)) + const hasComma = !hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties !== false)) if (hasComma) { code += 'let addComma = false\n' addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)' From c8a0a47c14dc7cab6aec6543ed6cb7eb0124bdcf Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 17:33:14 +0200 Subject: [PATCH 5/5] feat: add test Signed-off-by: francesco --- test/json-add-comma.test.js | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 test/json-add-comma.test.js diff --git a/test/json-add-comma.test.js b/test/json-add-comma.test.js new file mode 100644 index 00000000..a90cc1ec --- /dev/null +++ b/test/json-add-comma.test.js @@ -0,0 +1,57 @@ +'use strict' + +const { test } = require('node:test') +const build = require('..') + +test('additionalProperties: false', (t) => { + t.plan(1) + const stringify = build({ + title: 'additionalProperties', + type: 'object', + properties: { + foo: { + type: 'string' + } + }, + additionalProperties: false + }) + + const obj = { foo: 'a', bar: 'b', baz: 'c' } + t.assert.equal(stringify(obj), '{"foo":"a"}') +}) + +test('additionalProperties: {}', (t) => { + t.plan(1) + const stringify = build({ + title: 'additionalProperties', + type: 'object', + properties: { + foo: { + type: 'string' + } + }, + additionalProperties: {} + }) + + const obj = { foo: 'a', bar: 'b', baz: 'c' } + t.assert.equal(stringify(obj), '{"foo":"a","bar":"b","baz":"c"}') +}) + +test('additionalProperties: {type: string}', (t) => { + t.plan(1) + const stringify = build({ + title: 'additionalProperties', + type: 'object', + properties: { + foo: { + type: 'string' + } + }, + additionalProperties: { + type: 'string' + } + }) + + const obj = { foo: 'a', bar: 'b', baz: 'c' } + t.assert.equal(stringify(obj), '{"foo":"a","bar":"b","baz":"c"}') +})