@cranberry-money/shared-utils 4.1.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.
@@ -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"}
@@ -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/date.d.ts ADDED
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Date and time formatting utility functions
3
+ *
4
+ * This module provides pure functions for formatting dates and times
5
+ * in various formats suitable for display in the application.
6
+ */
7
+ /**
8
+ * Formats a date string to a localized date
9
+ *
10
+ * @param dateString - ISO date string or null
11
+ * @param fallback - Fallback text when date is null (default: 'No expiry')
12
+ * @returns Formatted date string
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * formatDate('2024-03-15') // '3/15/2024' (in en-US)
17
+ * formatDate(null) // 'No expiry'
18
+ * formatDate(null, 'Not set') // 'Not set'
19
+ * ```
20
+ */
21
+ export declare function formatDate(dateString: string | null, fallback?: string): string;
22
+ /**
23
+ * Formats a date string to a short date format (e.g., "Jan 15")
24
+ *
25
+ * @param dateString - ISO date string
26
+ * @param locale - Locale for formatting (default: 'en-AU')
27
+ * @returns Formatted short date string
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * formatShortDate('2024-01-15') // 'Jan 15'
32
+ * formatShortDate('2024-12-25', 'en-US') // 'Dec 25'
33
+ * ```
34
+ */
35
+ export declare function formatShortDate(dateString: string, locale?: string): string;
36
+ /**
37
+ * Formats a date string to a time format (24-hour)
38
+ *
39
+ * @param dateString - ISO date string
40
+ * @param locale - Locale for formatting (default: 'en-AU')
41
+ * @returns Formatted time string in 24-hour format
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * formatTime('2024-01-15T14:30:00') // '14:30'
46
+ * formatTime('2024-01-15T09:05:00') // '09:05'
47
+ * ```
48
+ */
49
+ export declare function formatTime(dateString: string, locale?: string): string;
50
+ /**
51
+ * Formats a date string to a combined short date and time format
52
+ *
53
+ * @param dateString - ISO date string
54
+ * @param locale - Locale for formatting (default: 'en-AU')
55
+ * @returns Formatted string like "Jan 15 14:30"
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * formatDateTime('2024-01-15T14:30:00') // 'Jan 15 14:30'
60
+ * formatDateTime('2024-12-25T09:00:00', 'en-US') // 'Dec 25 09:00'
61
+ * ```
62
+ */
63
+ export declare function formatDateTime(dateString: string, locale?: string): string;
64
+ //# sourceMappingURL=date.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../src/date.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;;;;;GAaG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,EAAE,QAAQ,GAAE,MAAoB,GAAG,MAAM,CAG5F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,GAAE,MAAgB,GAAG,MAAM,CAMpF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,GAAE,MAAgB,GAAG,MAAM,CAM/E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,GAAE,MAAgB,GAAG,MAAM,CAYnF"}
package/dist/date.js ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Date and time formatting utility functions
3
+ *
4
+ * This module provides pure functions for formatting dates and times
5
+ * in various formats suitable for display in the application.
6
+ */
7
+ /**
8
+ * Formats a date string to a localized date
9
+ *
10
+ * @param dateString - ISO date string or null
11
+ * @param fallback - Fallback text when date is null (default: 'No expiry')
12
+ * @returns Formatted date string
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * formatDate('2024-03-15') // '3/15/2024' (in en-US)
17
+ * formatDate(null) // 'No expiry'
18
+ * formatDate(null, 'Not set') // 'Not set'
19
+ * ```
20
+ */
21
+ export function formatDate(dateString, fallback = 'No expiry') {
22
+ if (!dateString)
23
+ return fallback;
24
+ return new Date(dateString).toLocaleDateString();
25
+ }
26
+ /**
27
+ * Formats a date string to a short date format (e.g., "Jan 15")
28
+ *
29
+ * @param dateString - ISO date string
30
+ * @param locale - Locale for formatting (default: 'en-AU')
31
+ * @returns Formatted short date string
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * formatShortDate('2024-01-15') // 'Jan 15'
36
+ * formatShortDate('2024-12-25', 'en-US') // 'Dec 25'
37
+ * ```
38
+ */
39
+ export function formatShortDate(dateString, locale = 'en-AU') {
40
+ const date = new Date(dateString);
41
+ return date.toLocaleDateString(locale, {
42
+ month: 'short',
43
+ day: 'numeric',
44
+ });
45
+ }
46
+ /**
47
+ * Formats a date string to a time format (24-hour)
48
+ *
49
+ * @param dateString - ISO date string
50
+ * @param locale - Locale for formatting (default: 'en-AU')
51
+ * @returns Formatted time string in 24-hour format
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * formatTime('2024-01-15T14:30:00') // '14:30'
56
+ * formatTime('2024-01-15T09:05:00') // '09:05'
57
+ * ```
58
+ */
59
+ export function formatTime(dateString, locale = 'en-AU') {
60
+ return new Date(dateString).toLocaleTimeString(locale, {
61
+ hour: '2-digit',
62
+ minute: '2-digit',
63
+ hour12: false,
64
+ });
65
+ }
66
+ /**
67
+ * Formats a date string to a combined short date and time format
68
+ *
69
+ * @param dateString - ISO date string
70
+ * @param locale - Locale for formatting (default: 'en-AU')
71
+ * @returns Formatted string like "Jan 15 14:30"
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * formatDateTime('2024-01-15T14:30:00') // 'Jan 15 14:30'
76
+ * formatDateTime('2024-12-25T09:00:00', 'en-US') // 'Dec 25 09:00'
77
+ * ```
78
+ */
79
+ export function formatDateTime(dateString, locale = 'en-AU') {
80
+ const date = new Date(dateString);
81
+ const shortDate = date.toLocaleDateString(locale, {
82
+ month: 'short',
83
+ day: 'numeric',
84
+ });
85
+ const time = date.toLocaleTimeString(locale, {
86
+ hour: '2-digit',
87
+ minute: '2-digit',
88
+ hour12: false,
89
+ });
90
+ return `${shortDate} ${time}`;
91
+ }
package/dist/index.d.ts CHANGED
@@ -5,4 +5,6 @@
5
5
  * All utilities are pure functions with no side effects.
6
6
  */
7
7
  export { truncateText } from './text';
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';
8
10
  //# sourceMappingURL=index.d.ts.map
@@ -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"}
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
@@ -6,3 +6,7 @@
6
6
  */
7
7
  // Text manipulation utilities
8
8
  export { truncateText } from './text';
9
+ // Date and time formatting utilities
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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cranberry-money/shared-utils",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
4
4
  "description": "Shared utility functions for MyPortfolio platform",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",