@qlibs/utils 1.5.3 → 1.5.5
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/utils/number.d.ts +2 -2
- package/dist/utils/number.js +41 -24
- package/package.json +1 -1
package/dist/utils/number.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
export declare function formatNumberRange(amountStart: number, amountEnd?: number, locale?: string): string;
|
2
2
|
export declare function formatPriceRange(amountStart: number, amountEnd?: number | undefined, currency?: 'IDR' | 'USD' | 'EUR'): string;
|
3
|
-
export declare function formatPrice(num: number, currency?: 'IDR' | 'USD' | 'EUR'): string;
|
4
|
-
export declare function parseFormattedPrice(formattedString: string, currency?: 'IDR' | 'USD' | 'EUR'): number;
|
3
|
+
export declare function formatPrice(num: number | undefined | null, currency?: 'IDR' | 'USD' | 'EUR'): string;
|
5
4
|
export declare function formatNumber(num: number, locale?: string, currency?: 'IDR' | 'USD' | 'EUR'): string;
|
5
|
+
export declare function parseFormattedPrice(formattedString: string, currency?: 'IDR' | 'USD' | 'EUR'): number;
|
6
6
|
export declare function parseFormattedNumber(formattedString: string, locale?: string, currency?: 'IDR' | 'USD' | 'EUR'): number;
|
package/dist/utils/number.js
CHANGED
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatNumberRange = formatNumberRange;
|
4
4
|
exports.formatPriceRange = formatPriceRange;
|
5
5
|
exports.formatPrice = formatPrice;
|
6
|
-
exports.parseFormattedPrice = parseFormattedPrice;
|
7
6
|
exports.formatNumber = formatNumber;
|
7
|
+
exports.parseFormattedPrice = parseFormattedPrice;
|
8
8
|
exports.parseFormattedNumber = parseFormattedNumber;
|
9
9
|
function formatNumberRange(amountStart, amountEnd, locale = 'id-ID') {
|
10
10
|
if (amountStart === amountEnd) {
|
@@ -44,14 +44,28 @@ function formatPriceRange(amountStart, amountEnd, currency = 'IDR') {
|
|
44
44
|
return '';
|
45
45
|
}
|
46
46
|
}
|
47
|
+
const currencyLocaleMap = {
|
48
|
+
IDR: 'id-ID',
|
49
|
+
USD: 'en-US',
|
50
|
+
EUR: 'de-DE',
|
51
|
+
};
|
47
52
|
function formatPrice(num, currency = 'IDR') {
|
48
|
-
if (
|
49
|
-
|
53
|
+
if (num === undefined || num === null || isNaN(num)) {
|
54
|
+
num = 0;
|
50
55
|
}
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
56
|
+
const locale = currencyLocaleMap[currency] || 'id-ID';
|
57
|
+
return formatNumber(num, locale, currency);
|
58
|
+
}
|
59
|
+
function formatNumber(num, locale = 'id-ID', currency) {
|
60
|
+
if (isNaN(num) || num === undefined || num === null) {
|
61
|
+
num = 0;
|
62
|
+
}
|
63
|
+
return new Intl.NumberFormat(locale, {
|
64
|
+
style: currency ? 'currency' : undefined,
|
65
|
+
currency,
|
66
|
+
minimumFractionDigits: 0,
|
67
|
+
maximumFractionDigits: 0,
|
68
|
+
}).format(num);
|
55
69
|
}
|
56
70
|
function parseFormattedPrice(formattedString, currency = 'IDR') {
|
57
71
|
if (currency === 'USD') {
|
@@ -62,29 +76,32 @@ function parseFormattedPrice(formattedString, currency = 'IDR') {
|
|
62
76
|
}
|
63
77
|
return parseFormattedNumber(formattedString, 'id-ID', currency);
|
64
78
|
}
|
65
|
-
function formatNumber(num, locale = 'id-ID', currency) {
|
66
|
-
if (currency) {
|
67
|
-
return new Intl.NumberFormat(locale, {
|
68
|
-
style: 'currency',
|
69
|
-
currency,
|
70
|
-
minimumFractionDigits: 0,
|
71
|
-
}).format(Number(num));
|
72
|
-
}
|
73
|
-
else {
|
74
|
-
return new Intl.NumberFormat(locale).format(num);
|
75
|
-
}
|
76
|
-
}
|
77
79
|
function parseFormattedNumber(formattedString, locale = 'id-ID', currency) {
|
78
80
|
if (!formattedString) {
|
79
81
|
return 0;
|
80
82
|
}
|
81
|
-
const exampleNumber =
|
83
|
+
const exampleNumber = 1245678;
|
82
84
|
const formattedExample = new Intl.NumberFormat(locale, currency ? { style: 'currency', currency, minimumFractionDigits: 0 } : {}).format(exampleNumber);
|
83
85
|
const nonDigitChars = formattedExample.replace(/\d/g, '');
|
84
|
-
const
|
85
|
-
const
|
86
|
-
|
87
|
-
|
86
|
+
const separators = nonDigitChars.replace(/[^\s.,]/g, '');
|
87
|
+
const uniqueSeparators = [...new Set(separators)];
|
88
|
+
const thousandSeparator = uniqueSeparators.length > 1
|
89
|
+
? currency
|
90
|
+
? uniqueSeparators[1]
|
91
|
+
: uniqueSeparators[0]
|
92
|
+
: uniqueSeparators[0] || ',';
|
93
|
+
const decimalSeparator = uniqueSeparators.length > 1
|
94
|
+
? currency
|
95
|
+
? uniqueSeparators[2]
|
96
|
+
: uniqueSeparators[1]
|
97
|
+
: uniqueSeparators[1] || '.';
|
98
|
+
const isNegative = formattedString.includes('-');
|
99
|
+
let normalizedString = formattedString.replace(/[^\d.,]/g, '');
|
100
|
+
const thousandSeparatorRegex = new RegExp(`[\\${thousandSeparator}\\s]`, 'g');
|
101
|
+
normalizedString = normalizedString.replace(thousandSeparatorRegex, '');
|
88
102
|
normalizedString = normalizedString.replace(new RegExp(`\\${decimalSeparator}`), '.');
|
103
|
+
if (isNegative) {
|
104
|
+
normalizedString = '-' + normalizedString;
|
105
|
+
}
|
89
106
|
return parseFloat(normalizedString);
|
90
107
|
}
|