-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathutils.ts
More file actions
80 lines (58 loc) · 2.01 KB
/
utils.ts
File metadata and controls
80 lines (58 loc) · 2.01 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { drizzleSchema } from "@/lib/schemas/app-schema";
const areArraysEqual = (a: string[], b: string[]): boolean => {
if (a.length !== b.length) return false;
const sortedA = [...a].sort();
const sortedB = [...b].sort();
return sortedA.every((val, index) => val === sortedB[index]);
};
function getTimestamp() {
const date = new Date();
// Get ISO string and split date and time
const [datePart, timePart] = date.toISOString().split('T');
// Extract milliseconds
const [time, msZ] = timePart.split('.');
const milliseconds = msZ.slice(0, -1); // remove 'Z'
// Add 3 random digits to simulate microseconds (since JS only gives ms)
const microseconds = milliseconds.padEnd(3, '0') + Math.floor(Math.random() * 1000).toString().padStart(3, '0');
return `${datePart} ${time}.${microseconds}Z`;
}
type NestedObject = { [key: string]: any };
function excludeFields(
obj: NestedObject,
exclusions: { [key: string]: string[] }
): NestedObject {
const newObj: NestedObject = {};
for (const [key, value] of Object.entries(obj)) {
if (exclusions["root"]?.includes(key)) {
// Skip top-level fields listed in 'root'
continue;
}
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
// Recursively exclude fields in nested objects
newObj[key] = exclusions[key]
? Object.fromEntries(
Object.entries(value).filter(([k]) => !exclusions[key].includes(k))
)
: excludeFields(value, exclusions);
} else {
newObj[key] = value;
}
}
return newObj;
}
function groupBy(array: any[], key: string) {
return array.reduce((acc, item) => {
const groupKey = item[key];
if (!acc[groupKey]) {
acc[groupKey] = [];
}
acc[groupKey].push(item);
return acc;
}, {});
}
export {
areArraysEqual,
getTimestamp,
excludeFields,
groupBy
}