From af348d723586bc06f93a510d30154bd484052165 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Tue, 13 Jun 2023 12:06:57 +0200 Subject: [PATCH] fix: #93 repair `undefined` values by replacing them with `null` --- src/jsonrepair.test.ts | 6 ++++++ src/jsonrepair.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/jsonrepair.test.ts b/src/jsonrepair.test.ts index 6d87b83..ecc04fd 100644 --- a/src/jsonrepair.test.ts +++ b/src/jsonrepair.test.ts @@ -146,6 +146,12 @@ describe('jsonRepair', () => { strictEqual(jsonrepair('{"a":'), '{"a":null}') }) + it('should repair undefined values', () => { + strictEqual(jsonrepair('{"a":undefined}'), '{"a":null}') + strictEqual(jsonrepair('[undefined]'), '[null]') + strictEqual(jsonrepair('undefined'), 'null') + }) + it('should escape unescaped control characters', () => { strictEqual(jsonrepair('"hello\bworld"'), '"hello\\bworld"') strictEqual(jsonrepair('"hello\fworld"'), '"hello\\fworld"') diff --git a/src/jsonrepair.ts b/src/jsonrepair.ts index b0864d9..cd7f78d 100644 --- a/src/jsonrepair.ts +++ b/src/jsonrepair.ts @@ -596,7 +596,7 @@ export function jsonrepair(text: string): string { } const symbol = text.slice(start, i) - output += JSON.stringify(symbol) + output += symbol === 'undefined' ? 'null' : JSON.stringify(symbol) return true }