@dilicorp/ui 1.3.20 → 1.3.22
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.
|
@@ -5,7 +5,7 @@ export const InputCurrency = ({ attrs, handleChange, currency, name }) => {
|
|
|
5
5
|
const { maxLength = 20 } = attrs;
|
|
6
6
|
const handleChangeCurrency = (event) => {
|
|
7
7
|
const { value } = event.currentTarget;
|
|
8
|
-
const sanitize = numberHelper.currencySanitize(value);
|
|
8
|
+
const sanitize = numberHelper.currencySanitize(value, currency.locale, currency.format);
|
|
9
9
|
handleChange({
|
|
10
10
|
currentTarget: {
|
|
11
11
|
name,
|
|
@@ -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
|
-
|
|
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,4 +1,7 @@
|
|
|
1
1
|
class NumberHelper {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.currencyFractionDigitsCache = new Map();
|
|
4
|
+
}
|
|
2
5
|
float2Int(float) {
|
|
3
6
|
return Math.trunc(float);
|
|
4
7
|
}
|
|
@@ -11,15 +14,32 @@ class NumberHelper {
|
|
|
11
14
|
style: 'currency'
|
|
12
15
|
}).format(value);
|
|
13
16
|
}
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
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)) {
|
|
19
34
|
return null;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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}`;
|
|
23
43
|
}
|
|
24
44
|
percentageSanitize(value, decimals = 2) {
|
|
25
45
|
var _a;
|