-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.ts
More file actions
132 lines (109 loc) · 3.26 KB
/
Copy pathindex.ts
File metadata and controls
132 lines (109 loc) · 3.26 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
/**
* Copyright 2017, 2019-2020, 2022-2023, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { v4 } from 'uuid';
const MAX_SAFE_INTEGER_LIMIT = Math.pow(2, 53);
export function currentTimestamp(): number {
return Math.round(new Date().getTime());
}
export function isSafeInteger(number: unknown): boolean {
return typeof number == 'number' && Math.abs(number) <= MAX_SAFE_INTEGER_LIMIT;
}
export function keyBy<K>(arr: K[], key: string): Record<string, K> {
if (!arr) return {};
const base: Record<string, K> = {};
assignBy(arr, key, base);
return base;
}
export function assignBy<K>(arr: K[], key: string, base: Record<string, K>): void {
if (!arr) return;
arr.forEach((e) => {
base[(e as any)[key]] = e;
});
}
function isNumber(value: unknown): boolean {
return typeof value === 'number';
}
export function uuid(): string {
return v4();
}
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export function getTimestamp(): number {
return new Date().getTime();
}
export function groupBy<K>(arr: K[], grouperFn: (item: K) => string): Array<K[]> {
const grouper: { [key: string]: K[] } = {};
arr.forEach(item => {
const key = grouperFn(item);
grouper[key] = grouper[key] || [];
grouper[key].push(item);
});
return objectValues(grouper);
}
export function objectValues<K>(obj: { [key: string]: K }): K[] {
return Object.keys(obj).map(key => obj[key]);
}
export function objectEntries<K>(obj: { [key: string]: K }): [string, K][] {
return Object.keys(obj).map(key => [key, obj[key]]);
}
export function find<K>(arr: K[], cond: (arg: K) => boolean): K | undefined {
let found;
for (const item of arr) {
if (cond(item)) {
found = item;
break;
}
}
return found;
}
// TODO[OASIS-6649]: Don't use any type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function sprintf(format: string, ...args: any[]): string {
let i = 0;
return format.replace(/%s/g, function() {
const arg = args[i++];
const type = typeof arg;
if (type === 'function') {
return arg();
} else if (type === 'string') {
return arg;
} else {
return String(arg);
}
});
}
/**
* Checks two string arrays for equality.
* @param arrayA First Array to be compared against.
* @param arrayB Second Array to be compared against.
* @returns {boolean} True if both arrays are equal, otherwise returns false.
*/
export function checkArrayEquality(arrayA: string[], arrayB: string[]): boolean {
return arrayA.length === arrayB.length && arrayA.every((item, index) => item === arrayB[index]);
}
export default {
checkArrayEquality,
currentTimestamp,
isSafeInteger,
keyBy,
uuid,
isNumber,
getTimestamp,
groupBy,
objectValues,
objectEntries,
find,
sprintf,
};