-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
34 lines (28 loc) · 1017 Bytes
/
utils.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
export type ChartData = ReadonlyArray<readonly [tm: number, value: number]>;
export function fixContinuousReadings(
val: readonly number[]
): readonly number[] {
return val.reduce((result, input, idx, arr) => {
const newResult = [...result, input];
if (idx === 0) {
return newResult;
}
if (input >= arr[idx - 1]) {
return newResult;
}
const lastCorrectElIdx = result.findLastIndex((el) => el <= input);
if (lastCorrectElIdx === -1) {
// special case: last element is lower than all previous inputs
// so replace all elements with the last one
return Array(newResult.length).fill(input);
}
const lastCorrectEl = result[lastCorrectElIdx];
const stepsCount = idx - lastCorrectElIdx;
const step = (input - lastCorrectEl) / stepsCount;
for (let i = 1; i < stepsCount; ++i) {
const currentIndex = i + lastCorrectElIdx;
newResult[currentIndex] = i * step + lastCorrectEl;
}
return newResult;
}, [] as number[]);
}