@cranberry-money/shared-utils 4.2.0 → 4.3.0
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/currency.d.ts +99 -0
- package/dist/currency.d.ts.map +1 -0
- package/dist/currency.js +128 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Currency formatting utility functions and constants
|
|
3
|
+
*
|
|
4
|
+
* This module provides pure functions for formatting currency values,
|
|
5
|
+
* parsing currency inputs, and working with different currency formats.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Number formatting options for currency display
|
|
9
|
+
*/
|
|
10
|
+
export declare const NUMBER_FORMAT_OPTIONS_CURRENCY: {
|
|
11
|
+
readonly style: "currency";
|
|
12
|
+
readonly minimumFractionDigits: 2;
|
|
13
|
+
readonly maximumFractionDigits: 2;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Number formatting options for currency with explicit sign display
|
|
17
|
+
*/
|
|
18
|
+
export declare const NUMBER_FORMAT_OPTIONS_CURRENCY_SIGNED: {
|
|
19
|
+
readonly signDisplay: "always";
|
|
20
|
+
readonly style: "currency";
|
|
21
|
+
readonly minimumFractionDigits: 2;
|
|
22
|
+
readonly maximumFractionDigits: 2;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Formats a number as currency with commas and 2 decimal places
|
|
26
|
+
*
|
|
27
|
+
* @param value - Number or string value to format
|
|
28
|
+
* @returns Formatted currency string without currency symbol
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* formatCurrency(1234.5) // '1,234.50'
|
|
33
|
+
* formatCurrency('1234.5') // '1,234.50'
|
|
34
|
+
* formatCurrency(0) // '0.00'
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function formatCurrency(value: number | string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Parses a currency string input into a number
|
|
40
|
+
*
|
|
41
|
+
* @param value - Currency string to parse
|
|
42
|
+
* @returns Parsed number value (or 0 if invalid)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* parseCurrencyInput('$1,234.56') // 1234.56
|
|
47
|
+
* parseCurrencyInput('1234.567') // 1234.56
|
|
48
|
+
* parseCurrencyInput('abc') // 0
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function parseCurrencyInput(value: string): number;
|
|
52
|
+
/**
|
|
53
|
+
* Formats a number as currency with specific currency code and locale
|
|
54
|
+
*
|
|
55
|
+
* @param value - Number to format
|
|
56
|
+
* @param currencyCode - ISO 4217 currency code (default: DEFAULT_CURRENCY)
|
|
57
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
58
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
59
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
60
|
+
* @returns Formatted currency string
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* formatCurrencyWithCode(1234.5) // 'A$1,235'
|
|
65
|
+
* formatCurrencyWithCode(1234.5, 'USD', 'en-US') // '$1,235'
|
|
66
|
+
* formatCurrencyWithCode(1234.56, 'AUD', 'en-AU', 2, 2) // 'A$1,234.56'
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function formatCurrencyWithCode(value: number, currencyCode?: string, locale?: string, minimumFractionDigits?: number, maximumFractionDigits?: number): string;
|
|
70
|
+
/**
|
|
71
|
+
* Convenience function to format currency with the default currency (AUD)
|
|
72
|
+
*
|
|
73
|
+
* @param value - Number to format
|
|
74
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
75
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
76
|
+
* @returns Formatted currency string with default currency
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* formatDefaultCurrency(1234.5) // 'A$1,235'
|
|
81
|
+
* formatDefaultCurrency(1234.56, 2, 2) // 'A$1,234.56'
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function formatDefaultCurrency(value: number, minimumFractionDigits?: number, maximumFractionDigits?: number): string;
|
|
85
|
+
/**
|
|
86
|
+
* Formats a number for displaying share quantities (no decimals)
|
|
87
|
+
*
|
|
88
|
+
* @param shares - Number of shares to format
|
|
89
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
90
|
+
* @returns Formatted shares string with commas as thousands separators
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* formatShares(1234) // '1,234'
|
|
95
|
+
* formatShares(1234567) // '1,234,567'
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function formatShares(shares: number, locale?: string): string;
|
|
99
|
+
//# sourceMappingURL=currency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../src/currency.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;GAEG;AACH,eAAO,MAAM,8BAA8B;;;;CAIjC,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,qCAAqC;;;;;CAGxC,CAAC;AAEX;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAW7D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOxD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,MAAM,EACb,YAAY,GAAE,MAAyB,EACvC,MAAM,GAAE,MAAgB,EACxB,qBAAqB,GAAE,MAAU,EACjC,qBAAqB,GAAE,MAAU,GAChC,MAAM,CASR;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,EACb,qBAAqB,GAAE,MAAU,EACjC,qBAAqB,GAAE,MAAU,GAChC,MAAM,CAER;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,MAAgB,GAAG,MAAM,CAK7E"}
|
package/dist/currency.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Currency formatting utility functions and constants
|
|
3
|
+
*
|
|
4
|
+
* This module provides pure functions for formatting currency values,
|
|
5
|
+
* parsing currency inputs, and working with different currency formats.
|
|
6
|
+
*/
|
|
7
|
+
import { DEFAULT_CURRENCY, DECIMAL_PLACES } from '@cranberry-money/shared-constants';
|
|
8
|
+
/**
|
|
9
|
+
* Number formatting options for currency display
|
|
10
|
+
*/
|
|
11
|
+
export const NUMBER_FORMAT_OPTIONS_CURRENCY = {
|
|
12
|
+
style: 'currency',
|
|
13
|
+
minimumFractionDigits: DECIMAL_PLACES,
|
|
14
|
+
maximumFractionDigits: DECIMAL_PLACES,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Number formatting options for currency with explicit sign display
|
|
18
|
+
*/
|
|
19
|
+
export const NUMBER_FORMAT_OPTIONS_CURRENCY_SIGNED = {
|
|
20
|
+
...NUMBER_FORMAT_OPTIONS_CURRENCY,
|
|
21
|
+
signDisplay: 'always',
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Formats a number as currency with commas and 2 decimal places
|
|
25
|
+
*
|
|
26
|
+
* @param value - Number or string value to format
|
|
27
|
+
* @returns Formatted currency string without currency symbol
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* formatCurrency(1234.5) // '1,234.50'
|
|
32
|
+
* formatCurrency('1234.5') // '1,234.50'
|
|
33
|
+
* formatCurrency(0) // '0.00'
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function formatCurrency(value) {
|
|
37
|
+
if (!value && value !== 0)
|
|
38
|
+
return '';
|
|
39
|
+
const numValue = typeof value === 'string' ? parseFloat(value) : value;
|
|
40
|
+
if (isNaN(numValue))
|
|
41
|
+
return '';
|
|
42
|
+
return numValue.toLocaleString('en-AU', {
|
|
43
|
+
minimumFractionDigits: 2,
|
|
44
|
+
maximumFractionDigits: 2,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parses a currency string input into a number
|
|
49
|
+
*
|
|
50
|
+
* @param value - Currency string to parse
|
|
51
|
+
* @returns Parsed number value (or 0 if invalid)
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* parseCurrencyInput('$1,234.56') // 1234.56
|
|
56
|
+
* parseCurrencyInput('1234.567') // 1234.56
|
|
57
|
+
* parseCurrencyInput('abc') // 0
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function parseCurrencyInput(value) {
|
|
61
|
+
const cleanValue = value.replace(/[^0-9.]/g, '');
|
|
62
|
+
const parts = cleanValue.split('.');
|
|
63
|
+
const formattedValue = parts[0] + (parts.length > 1 && parts[1] ? '.' + parts[1].slice(0, 2) : '');
|
|
64
|
+
return parseFloat(formattedValue) || 0;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Formats a number as currency with specific currency code and locale
|
|
68
|
+
*
|
|
69
|
+
* @param value - Number to format
|
|
70
|
+
* @param currencyCode - ISO 4217 currency code (default: DEFAULT_CURRENCY)
|
|
71
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
72
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
73
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
74
|
+
* @returns Formatted currency string
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* formatCurrencyWithCode(1234.5) // 'A$1,235'
|
|
79
|
+
* formatCurrencyWithCode(1234.5, 'USD', 'en-US') // '$1,235'
|
|
80
|
+
* formatCurrencyWithCode(1234.56, 'AUD', 'en-AU', 2, 2) // 'A$1,234.56'
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export function formatCurrencyWithCode(value, currencyCode = DEFAULT_CURRENCY, locale = 'en-AU', minimumFractionDigits = 0, maximumFractionDigits = 0) {
|
|
84
|
+
if (value == null || isNaN(value))
|
|
85
|
+
return '';
|
|
86
|
+
return new Intl.NumberFormat(locale, {
|
|
87
|
+
style: 'currency',
|
|
88
|
+
currency: currencyCode,
|
|
89
|
+
minimumFractionDigits,
|
|
90
|
+
maximumFractionDigits,
|
|
91
|
+
}).format(value);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Convenience function to format currency with the default currency (AUD)
|
|
95
|
+
*
|
|
96
|
+
* @param value - Number to format
|
|
97
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
98
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
99
|
+
* @returns Formatted currency string with default currency
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* formatDefaultCurrency(1234.5) // 'A$1,235'
|
|
104
|
+
* formatDefaultCurrency(1234.56, 2, 2) // 'A$1,234.56'
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export function formatDefaultCurrency(value, minimumFractionDigits = 0, maximumFractionDigits = 0) {
|
|
108
|
+
return formatCurrencyWithCode(value, DEFAULT_CURRENCY, 'en-AU', minimumFractionDigits, maximumFractionDigits);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Formats a number for displaying share quantities (no decimals)
|
|
112
|
+
*
|
|
113
|
+
* @param shares - Number of shares to format
|
|
114
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
115
|
+
* @returns Formatted shares string with commas as thousands separators
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* formatShares(1234) // '1,234'
|
|
120
|
+
* formatShares(1234567) // '1,234,567'
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export function formatShares(shares, locale = 'en-AU') {
|
|
124
|
+
return new Intl.NumberFormat(locale, {
|
|
125
|
+
minimumFractionDigits: 0,
|
|
126
|
+
maximumFractionDigits: 0,
|
|
127
|
+
}).format(shares);
|
|
128
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,5 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export { truncateText } from './text';
|
|
8
8
|
export { formatDate, formatShortDate, formatTime, formatDateTime } from './date';
|
|
9
|
+
export { NUMBER_FORMAT_OPTIONS_CURRENCY, NUMBER_FORMAT_OPTIONS_CURRENCY_SIGNED, formatCurrency, parseCurrencyInput, formatCurrencyWithCode, formatDefaultCurrency, formatShares, } from './currency';
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAGjF,OAAO,EACL,8BAA8B,EAC9B,qCAAqC,EACrC,cAAc,EACd,kBAAkB,EAClB,sBAAsB,EACtB,qBAAqB,EACrB,YAAY,GACb,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -8,3 +8,5 @@
|
|
|
8
8
|
export { truncateText } from './text';
|
|
9
9
|
// Date and time formatting utilities
|
|
10
10
|
export { formatDate, formatShortDate, formatTime, formatDateTime } from './date';
|
|
11
|
+
// Currency formatting utilities
|
|
12
|
+
export { NUMBER_FORMAT_OPTIONS_CURRENCY, NUMBER_FORMAT_OPTIONS_CURRENCY_SIGNED, formatCurrency, parseCurrencyInput, formatCurrencyWithCode, formatDefaultCurrency, formatShares, } from './currency';
|