Skip to content

Commit

Permalink
feat: deepMergeObject improvements; NumericLiteral and StringLiteral …
Browse files Browse the repository at this point in the history
…keys (#63)
  • Loading branch information
Tahul authored May 31, 2023
1 parent 9edc7f5 commit 8568f76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/proxy/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export function proxifyObject<T extends object>(
}
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;
Expand All @@ -38,8 +40,13 @@ export function proxifyObject<T extends object>(
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`, {
Expand Down
11 changes: 10 additions & 1 deletion test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export default {
`
export default {
foo: {
}
},
100: 10,
true: 10
}
`.trim()
);
Expand All @@ -107,6 +109,10 @@ export default {
value: "a",
},
},

100: 20,

true: 20,
};

// Recursively merge existing object with `obj`
Expand All @@ -118,6 +124,9 @@ export default {
value: 1,
},
100: 20,
true: 20,
bar: {
testValue: {
value: \\"a\\",
Expand Down

0 comments on commit 8568f76

Please sign in to comment.