-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
util.ts
56 lines (49 loc) · 1.42 KB
/
util.ts
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
import _ from "lodash";
export const getWeightedAverage = (
input: {
weight: number;
value: number;
}[],
) => {
if (input.length === 0) {
return 0;
}
let totalWeightTimesValue = 0;
let totalWeight = 0;
for (const { weight, value } of input) {
totalWeightTimesValue += weight * value;
totalWeight += weight;
}
return totalWeightTimesValue / totalWeight;
};
export const sum = (vals: number[]) => vals.reduce((acc, val) => acc + val, 0);
export const filterObj = (obj: Record<string, unknown>, filterFn: any) =>
Object.fromEntries(Object.entries(obj).filter(filterFn));
export const mapObj = (obj: Record<string, unknown>, filterFn: any) =>
Object.fromEntries(Object.entries(obj).map(filterFn) as any[]);
export const filterRuntimeEnvSystemVariables = (
runtime_env: Record<string, any>,
): Record<string, any> => {
const out = _.pickBy(runtime_env, (_, key) => {
if (key.startsWith("_")) {
return false;
}
return true;
});
return out;
};
export const sliceToPage = <T>(
items: T[],
pageNo: number,
pageSize = 10,
): { items: T[]; constrainedPage: number; maxPage: number } => {
const maxPage = Math.ceil(items.length / pageSize);
const constrainedPage = Math.min(maxPage, Math.max(1, pageNo));
const start = (constrainedPage - 1) * pageSize;
const end = constrainedPage * pageSize;
return {
items: items.slice(start, end),
constrainedPage,
maxPage,
};
};