Skip to content

Commit

Permalink
Pass keys options recursively (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quicksaver authored Feb 14, 2024
1 parent 4343da0 commit 15ed560
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/change-case/src/keys.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,24 @@ const TEST_CASES: [unknown, number | undefined, unknown][] = [
],
[{ TEST: true }, 0, { TEST: true }],
[null, 1, null],
[
{
outer_property_1_2: "outer",
an_array: [{ inner_property_3_4: true }],
},
Infinity,
{
outerProperty12: "outer",
anArray: [{ innerProperty34: true }],
},
{ mergeAmbiguousCharacters: true },
],
];

describe("change keys", () => {
for (const [input, depth, result] of TEST_CASES) {
for (const [input, depth, result, options] of TEST_CASES) {
it(`${input} -> ${result}`, () => {
expect(camelCase(input, depth)).toEqual(result);
expect(camelCase(input, depth, options)).toEqual(result);
});
}
});
4 changes: 2 additions & 2 deletions packages/change-case/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function changeKeysFactory(
if (depth === 0 || !isObject(object)) return object;

if (Array.isArray(object)) {
return object.map((item) => changeKeys(item, depth - 1));
return object.map((item) => changeKeys(item, depth - 1, options));
}

const result: Record<string, unknown> = Object.create(
Expand All @@ -24,7 +24,7 @@ function changeKeysFactory(
Object.keys(object as object).forEach((key) => {
const value = (object as Record<string, unknown>)[key];
const changedKey = changeCase(key, options);
const changedValue = changeKeys(value, depth - 1);
const changedValue = changeKeys(value, depth - 1, options);
result[changedKey] = changedValue;
});

Expand Down

0 comments on commit 15ed560

Please sign in to comment.