1import { isPlainObject } from "./data.ts";
2
3export const camelCaseKey = (key: string) =>
4 key
5 .replace(/^_+/, "")
6 .replace(/_([a-z])/g, (_, letter: string) => letter.toUpperCase());
7
8export const toCamelCase = <T>(value: unknown): T => {
9 if (Array.isArray(value)) {
10 return value.map((item) => toCamelCase(item)) as T;
11 }
12 if (isPlainObject(value)) {
13 return Object.fromEntries(
14 Object.entries(value).map(([key, nested]) => [
15 camelCaseKey(key),
16 toCamelCase(nested),
17 ]),
18 ) as T;
19 }
20 return value as T;
21};