Skip to content

Commit b580799

Browse files
committed
test(toCamelCaseKeys): cover PascalCase keys and NonPlainObject values
1 parent 694a4a1 commit b580799

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/object/toCamelCaseKeys.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,66 @@ describe('camelizeKeys', () => {
138138
expectTypeOf(result.b).toEqualTypeOf<RegExp>();
139139
expectTypeOf(result.c).toEqualTypeOf<Map<any, any>>();
140140
});
141+
142+
it('should convert ALL_CAPS keys to camelCase at both runtime and type level', () => {
143+
const input = {
144+
FIRST_NAME: 'JinHo',
145+
LAST: 'Yeom',
146+
} as const;
147+
148+
const result = toCamelCaseKeys(input);
149+
150+
expect(result).toEqual({
151+
firstName: 'JinHo',
152+
last: 'Yeom',
153+
});
154+
155+
expectTypeOf(result).toEqualTypeOf<{
156+
firstName: 'JinHo';
157+
last: 'Yeom';
158+
}>();
159+
});
160+
161+
it('should lowercase only the first letter for regular PascalCase keys', () => {
162+
const input = {
163+
UserID: 1,
164+
ApiToken: 'xxx',
165+
};
166+
167+
const result = toCamelCaseKeys(input);
168+
169+
expect(result).toEqual({
170+
userID: 1,
171+
apiToken: 'xxx',
172+
});
173+
174+
expectTypeOf(result).toEqualTypeOf<{
175+
userID: number;
176+
apiToken: string;
177+
}>();
178+
});
179+
180+
it('should not recurse into NonPlainObject values', () => {
181+
const date = new Date();
182+
const map = new Map<string, any>([['first_name', 'JinHo']]);
183+
const set = new Set<number>([1, 2, 3]);
184+
185+
const input = {
186+
created_at: date,
187+
meta_map: map,
188+
ids_set: set,
189+
};
190+
191+
const result = toCamelCaseKeys(input);
192+
193+
expect(result.createdAt).toBe(date);
194+
expect(result.metaMap).toBe(map);
195+
expect(result.idsSet).toBe(set);
196+
197+
expectTypeOf(result).toEqualTypeOf<{
198+
createdAt: Date;
199+
metaMap: Map<string, any>;
200+
idsSet: Set<number>;
201+
}>();
202+
});
141203
});

0 commit comments

Comments
 (0)