Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
45 lines (40 loc) · 1.44 KB
/
Copy pathutils.ts
File metadata and controls
45 lines (40 loc) · 1.44 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
41
42
43
44
45
export function isObject(item: unknown): item is Record<string, any> {
return Boolean(item && typeof item === 'object' && !Array.isArray(item));
}
export function deepMerge<T extends Record<string, any>>(target: T, ...sources: Partial<T>[]): T {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
const sourceVal = source[key];
if (isObject(sourceVal)) {
if (!target[key]) Object.assign(target, { [key]: {} });
deepMerge(target[key], sourceVal);
} else {
Object.assign(target, { [key]: sourceVal });
}
}
}
}
return deepMerge(target, ...sources);
}
export function flattenObject(obj: Record<string, any>, prefix = ''): Record<string, any> {
return Object.keys(obj).reduce((acc: Record<string, any>, key: string) => {
const pre = prefix.length ? `${prefix}.` : '';
if (isObject(obj[key])) {
Object.assign(acc, flattenObject(obj[key], `${pre}${key}`));
} else {
acc[`${pre}${key}`] = obj[key];
}
return acc;
}, {});
}
export function chunkArray<T>(array: T[], size: number): T[][] {
if (size <= 0) throw new Error('Chunk size must be greater than zero');
const result: T[][] = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
}