Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit 393100f

Browse files
authored
feat(object): ✨ Improve renamer parameter (#83)
1 parent bd687fd commit 393100f

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

.changeset/tender-rabbits-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@terminal-nerds/snippets-object": minor
3+
---
4+
5+
Add `index` to `renameObjectKeys()`-> `renamer(key, index)`
Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
1-
import { returns } from "@terminal-nerds/snippets-test/unit";
1+
import { SAMPLE_PRIMITIVES } from "@terminal-nerds/snippets-test/sample";
2+
import { returns, throws } from "@terminal-nerds/snippets-test/unit";
23
import { describe, it } from "vitest";
4+
import { ZodError } from "zod";
35

46
import { renameObjectKeys } from "./keys.ts";
57

68
const SAMPLE_OBJECT = {
7-
1: "one",
9+
one: "one",
810
two: 2,
9-
3: "threethree",
11+
three: "threethree",
1012
four: "444444",
11-
5: 5,
13+
five: 5,
1214
} as const;
1315

1416
describe("renameObjectKeys(object, renamer)", () => {
17+
it(throws(ZodError).on(`passed non-object`).samples(SAMPLE_PRIMITIVES), ({ expect }) => {
18+
for (const sample of SAMPLE_PRIMITIVES) {
19+
// @ts-ignore Testing
20+
expect(() => renameObjectKeys(sample, (key) => key)).toThrowError(ZodError);
21+
}
22+
});
23+
24+
it(
25+
throws(ZodError)
26+
.on(`passed sample object`)
27+
.sample(SAMPLE_OBJECT)
28+
.and(`with renamer returning non-string value`),
29+
({ expect }) => {
30+
// @ts-ignore Testing
31+
expect(() => renameObjectKeys(SAMPLE_OBJECT, () => void 0)).toThrowError(ZodError);
32+
},
33+
);
34+
1535
const expected = {
16-
"11": "one",
17-
"two1": 2,
18-
"31": "threethree",
19-
"four1": "444444",
20-
"51": 5,
36+
one1: "one",
37+
two2: 2,
38+
three3: "threethree",
39+
four4: "444444",
40+
five5: 5,
2141
} as const;
2242

2343
it(
24-
returns(expected).on(`passed sample object`).sample(SAMPLE_OBJECT).and(`with renamer appending 1 to each key`),
44+
returns(expected)
45+
.on(`passed sample object`)
46+
.sample(SAMPLE_OBJECT)
47+
.and(`with renamer appending {index + 1} to each key`),
2548
({ expect }) => {
26-
expect(renameObjectKeys(SAMPLE_OBJECT, (key) => `${key}1` as const)).toEqual(expected);
49+
expect(renameObjectKeys(SAMPLE_OBJECT, (key, index) => `${key}${index + 1}` as const)).toEqual(expected);
2750
},
2851
);
2952
});

packages/object/source/keys/keys.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { z } from "zod";
22

3+
import { validateObject } from "../schema/schema.ts";
4+
35
export const OBJECT_KEY_SCHEMA = z.number().or(z.string()).or(z.symbol());
46

5-
const RENAMER_SCHEMA = z.function().args(OBJECT_KEY_SCHEMA).returns(OBJECT_KEY_SCHEMA);
7+
const RENAMER_SCHEMA = z.function().args(OBJECT_KEY_SCHEMA, z.number().optional()).returns(OBJECT_KEY_SCHEMA);
68

79
export type ObjectKey = number | string | symbol;
810

911
export function renameObjectKeys<OldKey extends ObjectKey, Value, NewKey extends ObjectKey>(
1012
object: Record<OldKey, Value>,
11-
renamer: (key: OldKey) => NewKey,
13+
renamer: (key: OldKey, index: number) => NewKey,
1214
): Record<NewKey, Value> {
15+
validateObject(object);
1316
RENAMER_SCHEMA.parse(renamer);
1417

18+
function handleRename(key: OldKey, index: number) {
19+
return OBJECT_KEY_SCHEMA.parse(renamer(key, index));
20+
}
21+
1522
return Object.fromEntries(
16-
(Object.entries(object) as Array<[OldKey, Value]>).map(([key, value]) => [renamer(key), value]),
23+
(Object.entries(object) as Array<[OldKey, Value]>).map(([key, value], index) => [
24+
handleRename(key, index),
25+
value,
26+
]),
1727
) as Record<NewKey, Value>;
1828
}

0 commit comments

Comments
 (0)