-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathNumberUtilities.ts
80 lines (71 loc) · 2.1 KB
/
NumberUtilities.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export const clamp = (num: number, min: number, max: number): number => {
let clampedNumber = num;
if (num < min) {
clampedNumber = min;
} else if (num > max) {
clampedNumber = max;
}
return clampedNumber;
};
export const mod = (m: number, n: number): number => {
return ((m % n) + n) % n;
};
export const toFixedIfFloating = (value: string): string => {
if (!value || Number.isNaN(Number(value))) {
return value;
}
const number = Number(value);
return number % 1 ? number.toFixed(3) : String(number);
};
/**
* Rounds a number (including float) down.
*/
export const floor = (value: number, precision = 0): number => {
// Allows for rounding to the nearest whole number.
// Ex: 1 / 10 -> round down to nearest 10th place
// Ex: 1 / 5 -> round down to nearest 5
// Ex: 1 / 50 -> round down to nearest 50
if (precision > 0 && precision < 1) {
precision = 1 / precision;
return Math.floor(value / precision) * precision;
}
const mult = Math.pow(10, precision);
return Math.floor(value * mult) / mult;
};
/**
* Computes the great common divisor for two numbers.
* If the numbers are floats, they will be rounded to an integer.
*/
export const greatestCommonDivisor = (a: number, b: number): number => {
a = Math.round(a);
b = Math.round(b);
while (b !== 0) {
const t = b;
b = a % b;
a = t;
}
return a;
};
const commonRatios = new Map([
['8∶5', '16∶10'],
]);
export const aspectRatio = (width: number, height: number): string => {
const divisor = greatestCommonDivisor(width, height);
if (divisor !== 0) {
width /= divisor;
height /= divisor;
}
const result = `${width}∶${height}`;
return commonRatios.get(result) || result;
};
export const withThousandsSeparator = function(num: number): string {
let str = String(num);
const re = /(\d+)(\d{3})/;
while (str.match(re)) {
str = str.replace(re, '$1\xA0$2');
} // \xa0 is a non-breaking space
return str;
};