@qlibs/utils 1.5.2 → 1.5.4

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.
@@ -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: string, currency?: 'IDR' | 'USD' | 'EUR'): string;
3
+ export declare function formatPrice(num: number | undefined | null, currency?: 'IDR' | 'USD' | 'EUR'): string;
4
+ export declare function formatNumber(num: number, locale?: string, currency?: 'IDR' | 'USD' | 'EUR'): string;
4
5
  export declare function parseFormattedPrice(formattedString: string, currency?: 'IDR' | 'USD' | 'EUR'): number;
5
- export declare function formatNumber(num: string | number | undefined, locale?: string, currency?: 'IDR' | 'USD' | 'EUR'): string;
6
6
  export declare function parseFormattedNumber(formattedString: string, locale?: string, currency?: 'IDR' | 'USD' | 'EUR'): number;
@@ -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 (currency === 'USD') {
49
- return formatNumber(num, 'en-US', 'USD');
53
+ if (num === undefined || num === null || isNaN(num)) {
54
+ num = 0;
50
55
  }
51
- else if (currency === 'EUR') {
52
- return formatNumber(num, 'de-DE', 'EUR');
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;
53
62
  }
54
- return formatNumber(num, 'id-ID', 'IDR');
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,35 +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 (!num) {
67
- num = 0;
68
- }
69
- if (num && isNaN(Number(num))) {
70
- num = 0;
71
- }
72
- if (currency) {
73
- return new Intl.NumberFormat(locale, {
74
- style: 'currency',
75
- currency,
76
- minimumFractionDigits: 0,
77
- }).format(Number(num));
78
- }
79
- else {
80
- return new Intl.NumberFormat(locale).format(Number(num));
81
- }
82
- }
83
79
  function parseFormattedNumber(formattedString, locale = 'id-ID', currency) {
84
80
  if (!formattedString) {
85
81
  return 0;
86
82
  }
87
- const exampleNumber = 12345678912345.6;
83
+ const exampleNumber = 1245678;
88
84
  const formattedExample = new Intl.NumberFormat(locale, currency ? { style: 'currency', currency, minimumFractionDigits: 0 } : {}).format(exampleNumber);
89
85
  const nonDigitChars = formattedExample.replace(/\d/g, '');
90
- const thousandSeparator = nonDigitChars[0] || ',';
91
- const decimalSeparator = nonDigitChars[1] || '.';
92
- let normalizedString = formattedString.replace(/[^\d.,-]/g, '');
93
- normalizedString = normalizedString.replace(new RegExp(`\\${thousandSeparator}`, 'g'), '');
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
+ : ',';
93
+ const decimalSeparator = uniqueSeparators.length > 1
94
+ ? currency
95
+ ? uniqueSeparators[2]
96
+ : uniqueSeparators[1]
97
+ : '.';
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, '');
94
102
  normalizedString = normalizedString.replace(new RegExp(`\\${decimalSeparator}`), '.');
103
+ if (isNegative) {
104
+ normalizedString = '-' + normalizedString;
105
+ }
95
106
  return parseFloat(normalizedString);
96
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qlibs/utils",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
4
4
  "description": "",
5
5
  "author": "QBIT Developer",
6
6
  "license": "MIT",