@dilicorp/ui 1.3.21 → 1.3.23

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.
@@ -2,7 +2,9 @@ declare class NumberHelper {
2
2
  float2Int(float: number): number;
3
3
  sanitize(string: string): string;
4
4
  currency(value?: number, locale?: string, format?: string): string;
5
- currencySanitize(value: string, locale?: string, format?: string): string | null;
5
+ private readonly currencyFractionDigitsCache;
6
+ private getCurrencyFractionDigits;
7
+ currencySanitize(value: string, locale?: string, currency?: string): string | null;
6
8
  percentageSanitize(value: string, decimals?: number): string | null;
7
9
  }
8
10
  declare const _default: NumberHelper;
@@ -1,28 +1,45 @@
1
1
  class NumberHelper {
2
+ constructor() {
3
+ this.currencyFractionDigitsCache = new Map();
4
+ }
2
5
  float2Int(float) {
3
6
  return Math.trunc(float);
4
7
  }
5
8
  sanitize(string) {
6
9
  return string.replace(/\D/g, '');
7
10
  }
8
- currency(value = 0, locale = 'pt-br', format = 'BRL') {
11
+ currency(value = 0, locale = 'pt-BR', format = 'BRL') {
9
12
  return Intl.NumberFormat(locale, {
10
13
  currency: format,
11
14
  style: 'currency'
12
15
  }).format(value);
13
16
  }
14
- currencySanitize(value, locale = 'pt-br', format = 'BRL') {
15
- const fractionDigits = new Intl.NumberFormat(locale, { currency: format, style: 'currency' }).resolvedOptions().minimumFractionDigits;
16
- const sanitize = (Number(this.sanitize(value))).toString();
17
- if (value.replace(/\D/g, '') === '00' || sanitize === '')
17
+ getCurrencyFractionDigits(locale, currency) {
18
+ const cacheKey = `${locale}:${currency}`;
19
+ const cached = this.currencyFractionDigitsCache.get(cacheKey);
20
+ if (cached !== undefined) {
21
+ return cached;
22
+ }
23
+ const fractionDigits = new Intl.NumberFormat(locale, {
24
+ style: 'currency',
25
+ currency
26
+ }).resolvedOptions().minimumFractionDigits;
27
+ this.currencyFractionDigitsCache.set(cacheKey, fractionDigits !== null && fractionDigits !== void 0 ? fractionDigits : 0);
28
+ return fractionDigits !== null && fractionDigits !== void 0 ? fractionDigits : 0;
29
+ }
30
+ currencySanitize(value, locale = 'pt-BR', currency = 'BRL') {
31
+ const fractionDigits = this.getCurrencyFractionDigits(locale, currency);
32
+ const sanitized = this.sanitize(value).replace(/\D/g, '');
33
+ if (!sanitized || /^0+$/.test(sanitized)) {
18
34
  return null;
19
- if (fractionDigits === 0)
20
- return sanitize;
21
- const integer = sanitize.slice(0, -fractionDigits);
22
- const decimal = sanitize.slice(-fractionDigits);
23
- if (decimal.length < fractionDigits)
24
- return `${integer}.${'0'.repeat(fractionDigits - decimal.length)}${decimal}`;
25
- return `${integer}.${decimal}`;
35
+ }
36
+ if (fractionDigits === 0) {
37
+ return String(Number(sanitized));
38
+ }
39
+ const paddedValue = sanitized.padStart(fractionDigits + 1, '0');
40
+ const integer = paddedValue.slice(0, -fractionDigits);
41
+ const decimal = paddedValue.slice(-fractionDigits);
42
+ return `${Number(integer)}.${decimal}`;
26
43
  }
27
44
  percentageSanitize(value, decimals = 2) {
28
45
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dilicorp/ui",
3
- "version": "1.3.21",
3
+ "version": "1.3.23",
4
4
  "description": "A simple UI design for Dilicorp",
5
5
  "repository": {
6
6
  "type": "git",