From b8583d4db48aa2195667ae161973c54b81160f5d Mon Sep 17 00:00:00 2001 From: Romain Hamel Date: Fri, 8 Mar 2024 08:08:47 +0100 Subject: [PATCH] fix: stringify key identifier starting with number literal (#86) --- src/utils.ts | 2 +- test/index.test.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 19f0977..5f63cd7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,7 +15,7 @@ export function wrapInDelimiters( ); } -const VALID_IDENTIFIER_RE = /^[$_]?\w*$/; +const VALID_IDENTIFIER_RE = /^[$_]?([A-Z_a-z]\w*|\d)$/; export function genObjectKey(key: string) { return VALID_IDENTIFIER_RE.test(key) ? key : genString(key); diff --git a/test/index.test.ts b/test/index.test.ts index 65b00c2..0903f09 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -118,6 +118,8 @@ const genObjectFromRawTests = [ 1: "undefined", 2: true, 3: "true", + "2xs": true, + xs2: true, "obj 1": '{ literal: () => "test" }', "obj 2": { nested: { foo: '"bar"' } }, arr: ["1", "2", "3"], @@ -130,6 +132,8 @@ const genObjectFromRawTests = [ " a: null,", " b: null,", " c: undefined,", + ' "2xs": true,', + " xs2: true,", ' "obj 1": { literal: () => "test" },', ' "obj 2": {', " nested: {",