@streamscloud/kit 0.0.1-1770887346972 → 0.0.1-1770896468307
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/utils/compact-number.d.ts +8 -0
- package/dist/core/utils/compact-number.js +35 -0
- package/dist/core/utils/index.d.ts +1 -0
- package/dist/core/utils/index.js +1 -0
- package/dist/core/utils/number-helper.d.ts +0 -1
- package/dist/core/utils/number-helper.js +0 -10
- package/package.json +1 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { getLocale } from '../../locale';
|
|
2
|
+
const THOUSAND = 1_000;
|
|
3
|
+
const MILLION = 1_000_000;
|
|
4
|
+
const BILLION = 1_000_000_000;
|
|
5
|
+
const TRILLION = 1_000_000_000_000;
|
|
6
|
+
export const compactNumber = (value, options = {}) => {
|
|
7
|
+
const { decimals = 1, trimZeros = true, locale = getLocale() } = options;
|
|
8
|
+
const abs = Math.abs(value);
|
|
9
|
+
const formatter = makeNumberFormatter(locale, decimals, trimZeros);
|
|
10
|
+
if (abs >= TRILLION) {
|
|
11
|
+
return `${formatter.format(value / TRILLION)}${loc.T[locale]}`;
|
|
12
|
+
}
|
|
13
|
+
if (abs >= BILLION) {
|
|
14
|
+
return `${formatter.format(value / BILLION)}${loc.B[locale]}`;
|
|
15
|
+
}
|
|
16
|
+
if (abs >= MILLION) {
|
|
17
|
+
return `${formatter.format(value / MILLION)}${loc.M[locale]}`;
|
|
18
|
+
}
|
|
19
|
+
if (abs >= THOUSAND) {
|
|
20
|
+
return `${formatter.format(value / THOUSAND)}${loc.K[locale]}`;
|
|
21
|
+
}
|
|
22
|
+
return formatter.format(value);
|
|
23
|
+
};
|
|
24
|
+
const makeNumberFormatter = (locale, decimals, trimZeros) => new Intl.NumberFormat(loc.numberLocale[locale], {
|
|
25
|
+
minimumFractionDigits: trimZeros ? 0 : decimals,
|
|
26
|
+
maximumFractionDigits: decimals,
|
|
27
|
+
useGrouping: false
|
|
28
|
+
});
|
|
29
|
+
const loc = {
|
|
30
|
+
K: { en: 'K', no: 'K' },
|
|
31
|
+
M: { en: 'M', no: 'M' },
|
|
32
|
+
B: { en: 'B', no: 'B' },
|
|
33
|
+
T: { en: 'T', no: 'T' },
|
|
34
|
+
numberLocale: { en: 'nb-NO', no: 'nb-NO' }
|
|
35
|
+
};
|
package/dist/core/utils/index.js
CHANGED
|
@@ -15,16 +15,6 @@ export class NumberHelper {
|
|
|
15
15
|
output.unshift(modulo);
|
|
16
16
|
return `${isNegative ? '-' : ''}${output.join(' ')}`;
|
|
17
17
|
};
|
|
18
|
-
static formatCompactNumber = (num, locale) => {
|
|
19
|
-
if (num < 1000) {
|
|
20
|
-
return num.toString();
|
|
21
|
-
}
|
|
22
|
-
return new Intl.NumberFormat(locale, {
|
|
23
|
-
notation: 'compact',
|
|
24
|
-
compactDisplay: 'short',
|
|
25
|
-
maximumFractionDigits: 1
|
|
26
|
-
}).format(num);
|
|
27
|
-
};
|
|
28
18
|
static randomIntFromInterval = (min, max) => {
|
|
29
19
|
// min and max included
|
|
30
20
|
return Math.floor(Math.random() * (max - min + 1) + min);
|