@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.
@@ -0,0 +1,8 @@
1
+ import { type Locale } from '../../locale';
2
+ type CompactOptions = {
3
+ decimals?: number;
4
+ trimZeros?: boolean;
5
+ locale?: Locale;
6
+ };
7
+ export declare const compactNumber: (value: number, options?: CompactOptions) => string;
8
+ export {};
@@ -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
+ };
@@ -1,4 +1,5 @@
1
1
  export * from './array-helper';
2
+ export * from './compact-number';
2
3
  export * from './base64-serializer';
3
4
  export * from './browser';
4
5
  export * from './date-helper';
@@ -1,4 +1,5 @@
1
1
  export * from './array-helper';
2
+ export * from './compact-number';
2
3
  export * from './base64-serializer';
3
4
  export * from './browser';
4
5
  export * from './date-helper';
@@ -1,5 +1,4 @@
1
1
  export declare class NumberHelper {
2
2
  static formatNumber: (n: number | string) => string;
3
- static formatCompactNumber: (num: number, locale?: string) => string;
4
3
  static randomIntFromInterval: (min: number, max: number) => number;
5
4
  }
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamscloud/kit",
3
- "version": "0.0.1-1770887346972",
3
+ "version": "0.0.1-1770896468307",
4
4
  "author": "StreamsCloud",
5
5
  "repository": {
6
6
  "type": "git",