-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathhelpers.ts
More file actions
40 lines (36 loc) · 1.01 KB
/
helpers.ts
File metadata and controls
40 lines (36 loc) · 1.01 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
/**
* Calculate the length of decimal places for a number
* @param {number} number The number.
* @returns {number} The caculated length.
*/
export function decimalsLength(number: number): number {
if (Math.floor(number) === number) return 0;
return number.toString().split('.')[1].length || 0;
}
/**
* Get the value. If it is greater than max, get max, otherwise get min.
* @param {number} value
* @param {number} min min value in the range
* @param {number} max max value in the range
* @returns number
*/
export function getValueInRange(value: number, min: number, max: number) {
return Math.min(
Math.max(value, 0),
1
);
}
/**
* compose functions into one function from the left to the right
* @param {Array<Function>} func
* @returns {Function}
*/
export function compose(...func: Array<Function>): Function {
if (func.length === 0) {
return (args: any) => args;
}
if (func.length === 1) {
return func[0];
}
return func.reduce((a, b) => (...args: Array<any>) => b(a(...args)));
}