From 8568f7616a0977cf4c9ba4c831af4b696b10c0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Guilloux?= Date: Wed, 31 May 2023 15:13:09 +0200 Subject: [PATCH] feat: deepMergeObject improvements; NumericLiteral and StringLiteral keys (#63) --- src/proxy/object.ts | 13 ++++++++++--- test/object.test.ts | 11 ++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/proxy/object.ts b/src/proxy/object.ts index 11fbb4e..62aec08 100644 --- a/src/proxy/object.ts +++ b/src/proxy/object.ts @@ -23,7 +23,9 @@ export function proxifyObject( } if ( prop.type === "ObjectProperty" && - prop.key.type === "StringLiteral" && + (prop.key.type === "StringLiteral" || + prop.key.type === "NumericLiteral" || + prop.key.type === "BooleanLiteral") && prop.key.value === key ) { return (prop.value as any).value; @@ -38,8 +40,13 @@ export function proxifyObject( if ("key" in prop && "name" in prop.key) { return prop.key.name; } - if (prop.type === "ObjectProperty" && prop.key.type === "StringLiteral") { - return prop.key.value; + if ( + prop.type === "ObjectProperty" && + (prop.key.type === "StringLiteral" || + prop.key.type === "NumericLiteral" || + prop.key.type === "BooleanLiteral") + ) { + return prop.key.value.toString(); } if (throwError) { throw new MagicastError(`Casting "${prop.type}" is not supported`, { diff --git a/test/object.test.ts b/test/object.test.ts index f552044..915843b 100644 --- a/test/object.test.ts +++ b/test/object.test.ts @@ -92,7 +92,9 @@ export default { ` export default { foo: { - } + }, + 100: 10, + true: 10 } `.trim() ); @@ -107,6 +109,10 @@ export default { value: "a", }, }, + + 100: 20, + + true: 20, }; // Recursively merge existing object with `obj` @@ -118,6 +124,9 @@ export default { value: 1, }, + 100: 20, + true: 20, + bar: { testValue: { value: \\"a\\",