Skip to content

Commit

Permalink
fix: stringify key identifier starting with number literal (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
romhml authored Mar 8, 2024
1 parent 0e0cc7a commit b8583d4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -130,6 +132,8 @@ const genObjectFromRawTests = [
" a: null,",
" b: null,",
" c: undefined,",
' "2xs": true,',
" xs2: true,",
' "obj 1": { literal: () => "test" },',
' "obj 2": {',
" nested: {",
Expand Down

0 comments on commit b8583d4

Please sign in to comment.