Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
40 lines (36 loc) · 1.39 KB
/
Copy pathutils.ts
File metadata and controls
40 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
export type JsonValue = string | number | boolean | null | { [key: string]: JsonValue } | JsonValue[];
export const deepClone = <T>(obj: T): T => {
if (obj === null || typeof obj !== 'object') return obj;
if (Array.isArray(obj)) return obj.map(deepClone) as unknown as T;
const cloned = {} as T;
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
cloned[key] = deepClone(obj[key]);
}
}
return cloned;
};
export const pick = <T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> => {
const result = {} as Pick<T, K>;
keys.forEach((key) => {
if (key in obj) result[key] = obj[key];
});
return result;
};
export const isEmpty = (value: unknown): boolean => {
if (value === null || value === undefined) return true;
if (typeof value === 'string' || Array.isArray(value)) return value.length === 0;
if (typeof value === 'object') return Object.keys(value as object).length === 0;
return false;
};
export const flattenObject = (obj: Record<string, any>, prefix = ''): Record<string, any> => {
return Object.keys(obj).reduce((acc, key) => {
const pre = prefix.length ? `${prefix}.` : '';
if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
Object.assign(acc, flattenObject(obj[key], pre + key));
} else {
acc[pre + key] = obj[key];
}
return acc;
}, {} as Record<string, any>);
};