forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
79 lines (72 loc) · 2.71 KB
/
Copy pathutils.ts
File metadata and controls
79 lines (72 loc) · 2.71 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
type Pair = { key: string; value: string | undefined };
// this function accepts an object and returns an array of keys
// the object can have nested objects as values
// the keys returned are in the format of "parentKey.childKey.grandChildKey"
export function getKeys(obj: any): string[] {
const keys: string[] = [];
function recurse(obj: any, parentKey: string = '') {
for (const key in obj) {
if (typeof obj[key] === 'object') {
recurse(obj[key], `${parentKey}${key}.`);
} else {
keys.push(`${parentKey}${key}`);
}
}
}
recurse(obj);
return keys;
}
// this function accepts two arrays of strings and returns an array of strings
// the returned array contains the keys that are present only in the first array
// and not in the second array
export function getUniqueKeys(arr1: string[], arr2: string[]): string[] {
return arr1.filter(key => !arr2.includes(key));
}
// this function accepts two arrays of strings and returns an array of strings
// the returned array contains the keys that are present in both arrays
export function getCommonKeys(arr1: string[], arr2: string[]): string[] {
return arr1.filter(key => arr2.includes(key));
}
// this function accepts an object and a string which is a key
// it returns the value of the key in the object
// the key is in the format of "parentKey.childKey.grandChildKey"
export function getValue(obj: any, key: string): string {
return key.split('.').reduce((acc, curr) => acc[curr], obj);
}
// this function accepts an array of key value pairs and returns an object
// the key value pairs are in the format of {key: "parentKey.childKey.grandChildKey", value: "some value"}
// the returned object may have nested objects as values depending on the keys and their structure
// the values are assigned to the last key from the key string
// example: {key: "parentKey.childKey.grandChildKey", value: "some value"} => {parentKey: {childKey: {grandChildKey: "some value"}}}
export function createObject(pairs: Pair[]): any {
const obj = {};
pairs.forEach(pair => {
const keys = pair.key.split('.');
const lastKey = keys.pop() as string;
let temp: any = obj;
keys.forEach(key => {
if (!temp[key]) {
temp[key] = {};
}
temp = temp[key];
});
temp[lastKey] = pair.value;
});
return obj;
}
// function that deep merges two objects
// the second object is merged into the first object
// the first object is modified and returned
export function mergeObjects(obj1: any, obj2: any) {
for (const key in obj2) {
if (typeof obj2[key] === 'object') {
if (!obj1[key]) {
obj1[key] = {};
}
mergeObjects(obj1[key], obj2[key]);
} else {
obj1[key] = obj2[key];
}
}
return obj1;
}