forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import _ from "lodash";
2import * as R from "ramda";
3
4type AnyObject = Record<string, any>;
5
6const isObject = (val: unknown): val is AnyObject =>
7 typeof val === "object" && val !== null && !Array.isArray(val);
8
9export const deepCamelCaseKeys = <T>(obj: T): any => {
10 if (Array.isArray(obj)) {
11 return obj.map(deepCamelCaseKeys);
12 } else if (isObject(obj)) {
13 return R.pipe(
14 R.toPairs,
15 R.map(
16 ([key, value]) =>
17 [_.camelCase(String(key)), deepCamelCaseKeys(value)] as [string, any],
18 ),
19 R.fromPairs,
20 )(obj as object);
21 }
22 return obj;
23};
24
25export const deepSnakeCaseKeys = <T>(obj: T): any => {
26 if (Array.isArray(obj)) {
27 return obj.map(deepSnakeCaseKeys);
28 } else if (isObject(obj)) {
29 return R.pipe(
30 R.toPairs,
31 R.map(
32 ([key, value]) =>
33 [_.snakeCase(String(key)), deepSnakeCaseKeys(value)] as [string, any],
34 ),
35 R.fromPairs,
36 )(obj as object);
37 }
38 return obj;
39};