-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjavascriptHelpers.ts
More file actions
251 lines (221 loc) · 7.12 KB
/
Copy pathjavascriptHelpers.ts
File metadata and controls
251 lines (221 loc) · 7.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { Prettify } from '../typescript-utils';
export async function asyncMap<Type, TReturn>(
array: Array<Type>,
cb: (entry: Type, index: number) => Promise<TReturn>,
) {
const mappingCallbacks = array.map(async (entry, index) => await cb(entry, index));
const mappedValues = await Promise.all(mappingCallbacks);
return mappedValues as Array<TReturn>;
}
export async function asyncForEach<Type>(
array: Array<Type>,
cb: (entry: Type, index: number) => Promise<void>,
) {
await asyncMap(array, cb);
}
export async function asyncFilter<Type>(array: Array<Type>, cb: (entry: Type) => Promise<boolean>) {
// map the elements to their value or undefined and then filter undefined entries
return (
await asyncMap(array, async (entry) => {
const keep = await cb(entry);
return keep ? entry : undefined;
})
).filter((entry) => entry) as Array<Type>;
}
export function pick<T extends object, PickKeys extends (keyof T)[]>(
obj: T,
keys: PickKeys,
): Prettify<Pick<T, PickKeys[number]>> {
return Object.fromEntries(
Object.entries(obj).filter(([key]) => keys.includes(key as keyof T)),
) as Prettify<Pick<T, PickKeys[number]>>;
}
export function omit<T extends object, OmitKeys extends (keyof T)[]>(
obj: T,
keys: OmitKeys,
): Prettify<Omit<T, OmitKeys[number]>> {
return Object.fromEntries(
Object.entries(obj).filter(([key]) => !keys.includes(key as keyof T)),
) as Prettify<Omit<T, OmitKeys[number]>>;
}
export interface DiffResult {
path: string;
valueA: any;
valueB: any;
reason: string;
}
// TODO: Typescriptify or remove
/**
* Compares two values
*
* normal comparison for fundamental data types (number, string etc)
* element wise comparison for objects and arrays
* recursive handling for nested objects and arrays
*
* @param {Any} a some value
* @param {Any} b some value
* @returns {Boolean} - if the two values are equal
*/
export function deepEquals(
a: any,
b: any,
path = '',
verbose = false,
): boolean | DiffResult | null {
// Early exit if types don't match
if (typeof a !== typeof b) {
if (verbose) {
return {
path,
valueA: a,
valueB: b,
reason: 'Type mismatch',
};
}
return false;
}
// Arrays comparison
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
if (verbose) {
return {
path,
valueA: a,
valueB: b,
reason: 'Array length mismatch',
};
}
return false;
}
for (let i = 0; i < a.length; i++) {
const result = deepEquals(a[i], b[i], `${path}[${i}]`, verbose);
if (verbose && result) return result;
else if (!verbose && !result) return false;
}
return verbose ? null : true; // Arrays are equal
}
// Objects comparison
if (typeof a === 'object' && a !== null && b !== null) {
let aKeys = Object.keys(a);
let bKeys = Object.keys(b);
// Objects can't be equal with differing keys
if (aKeys.length !== bKeys.length || aKeys.some((key) => !bKeys.includes(key))) {
if (verbose) {
return {
path,
valueA: a,
valueB: b,
reason: 'Object keys mismatch',
};
}
return false;
}
// Recursively compare all keys and values
for (let key of aKeys) {
const result = deepEquals(a[key], b[key], path ? `${path}.${key}` : key, verbose);
if (verbose && result) return result;
else if (!verbose && !result) return false;
}
return verbose ? null : true; // Objects are equal
}
// Primitive values comparison
if (a !== b) {
if (verbose) {
return {
path,
valueA: a,
valueB: b,
reason: 'Value mismatch',
};
}
return false;
}
return verbose ? null : true; // Values are equal
}
// helper function to find all values of specific key (targetKey) in a json obj
export function findKey(data: any, targetKey = 'src'): string[] {
let results: string[] = [];
if (typeof data === 'object' && data !== null) {
for (const key in data) {
if (key === targetKey) {
results.push(data[key]);
} else if (typeof data[key] === 'object') {
results = results.concat(findKey(data[key], targetKey));
}
}
} else if (Array.isArray(data)) {
for (const item of data) {
results = results.concat(findKey(item, targetKey));
}
}
return results;
}
// TODO: Typescriptify or remove
export function isObject(candidate: any) {
return !!candidate && typeof candidate === 'object' && !Array.isArray(candidate);
}
// TODO: Typescriptify or remove
/**
* Function that allows overwriting entries in an object with values given in another object
*
* @param {Object} target the object to merge into
* @param {Object} toMerge the object containing the new values
* @param {Boolean} deepMerge if nested objects are supposed to be merged recursively (else they are just overwritten)
* @param {Boolean|String} noNewValues flag to disallow new entries being added to the target object ('strict' for error, true for silent ignore)
* @param {Boolean|String} typesafe if entries are not allowed to change their type ('strict' for error, true for silent ignore)
* @returns {Object} object containing the values that were actually changed (some changes might be silently ignored due to flags)
*/
export function mergeIntoObject(
target: any,
toMerge: any,
deepMerge: any,
noNewValues: any,
typesafe: any,
) {
if (!isObject(target)) {
throw new Error('Tried to merge into something that is not an object');
}
if (!isObject(toMerge)) {
throw new Error('Tried to merge something that is not an object');
}
const changedEntries: any = {};
Object.entries(toMerge).forEach(([key, value]) => {
// handle if adding entries is not allowed and target doesn't contain the current key
if (noNewValues && !{}.propertyIsEnumerable.call(target, key)) {
if (noNewValues === 'strict') {
// throw in strict mode
throw new Error('Tried to add new values to target object!');
} else {
// silently ignore
return;
}
}
// do nothing if the given key exists but its value has a different type from the one in the target and typesafe is true
if (
typesafe &&
{}.propertyIsEnumerable.call(target, key) &&
typeof value !== typeof target[key]
) {
if (typesafe === 'strict') {
throw new Error(`Tried changing the type of entry ${key}!`);
} else {
return;
}
}
// recursively merge objects if flag is set
if (deepMerge && isObject(value) && isObject(target[key])) {
changedEntries[key] = mergeIntoObject(target[key], value, deepMerge, noNewValues, typesafe);
} else {
target[key] = value;
changedEntries[key] = value;
}
});
return changedEntries;
}
export const hashString = async (str: string): Promise<string> => {
const encoder = new TextEncoder();
const data = encoder.encode(str);
const hashBuffer = await crypto.subtle.digest('SHA-512', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
};