@cranberry-money/shared-utils 4.2.0 → 4.4.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,68 @@
1
+ /**
2
+ * Badge System Core - Type definitions and factory function
3
+ *
4
+ * This module provides a standardized way to create and style badges.
5
+ * It ensures consistency in colors, typography, and semantic meaning
6
+ * while maintaining accessibility standards.
7
+ *
8
+ * Note: The actual style classes are designed to work with Tailwind CSS
9
+ * and assume a specific color system is in place.
10
+ */
11
+ /**
12
+ * Badge variant types representing different semantic states
13
+ */
14
+ export type BadgeVariant = 'primary' | 'secondary' | 'success' | 'warning' | 'error' | 'info' | 'neutral';
15
+ /**
16
+ * Badge size options
17
+ */
18
+ export type BadgeSize = 'sm' | 'md' | 'lg';
19
+ /**
20
+ * Configuration options for creating a badge
21
+ */
22
+ export interface BadgeConfig {
23
+ /** The semantic variant of the badge */
24
+ variant: BadgeVariant;
25
+ /** Size of the badge (default: 'md') */
26
+ size?: BadgeSize;
27
+ /** Additional CSS classes to apply */
28
+ className?: string;
29
+ }
30
+ /**
31
+ * Badge style output with generated classes and accessibility attributes
32
+ */
33
+ export interface BadgeStyle {
34
+ /** Combined CSS classes for the badge */
35
+ className: string;
36
+ /** Accessibility label for screen readers */
37
+ ariaLabel?: string;
38
+ }
39
+ /**
40
+ * Creates a badge style configuration with appropriate CSS classes
41
+ *
42
+ * @param config - Badge configuration options
43
+ * @param variantStyles - Optional custom variant styles (defaults to DEFAULT_BADGE_VARIANTS)
44
+ * @param sizeStyles - Optional custom size styles (defaults to DEFAULT_BADGE_SIZES)
45
+ * @returns Badge style object with className and accessibility attributes
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // Create a success badge
50
+ * const successBadge = createBadge({ variant: 'success' });
51
+ * // Returns: { className: 'inline-flex items-center rounded-md font-medium whitespace-nowrap transition-colors bg-success-900/80 text-success-300 text-xs px-2 py-1', ariaLabel: 'success status' }
52
+ *
53
+ * // Create a large warning badge with custom class
54
+ * const warningBadge = createBadge({ variant: 'warning', size: 'lg', className: 'ml-2' });
55
+ *
56
+ * // Use custom variant styles
57
+ * const customVariants = { primary: 'bg-blue-500 text-white', ... };
58
+ * const customBadge = createBadge({ variant: 'primary' }, customVariants);
59
+ * ```
60
+ */
61
+ export declare function createBadge({ variant, size, className }: BadgeConfig, variantStyles?: Record<BadgeVariant, string>, sizeStyles?: Record<BadgeSize, string>): BadgeStyle;
62
+ /**
63
+ * Export the default style mappings for consumers who want to use them directly
64
+ * or extend them with their own styles
65
+ */
66
+ export declare const BADGE_VARIANTS: Record<BadgeVariant, string>;
67
+ export declare const BADGE_SIZES: Record<BadgeSize, string>;
68
+ //# sourceMappingURL=badge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"badge.d.ts","sourceRoot":"","sources":["../src/badge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,CAAC;AAE1G;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAE3C;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,wCAAwC;IACxC,OAAO,EAAE,YAAY,CAAC;IACtB,wCAAwC;IACxC,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,6CAA6C;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA8BD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CACzB,EAAE,OAAO,EAAE,IAAW,EAAE,SAAc,EAAE,EAAE,WAAW,EACrD,aAAa,GAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAA0B,EACpE,UAAU,GAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAAuB,GAC1D,UAAU,CAQZ;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,8BAAyB,CAAC;AACrD,eAAO,MAAM,WAAW,2BAAsB,CAAC"}
package/dist/badge.js ADDED
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Badge System Core - Type definitions and factory function
3
+ *
4
+ * This module provides a standardized way to create and style badges.
5
+ * It ensures consistency in colors, typography, and semantic meaning
6
+ * while maintaining accessibility standards.
7
+ *
8
+ * Note: The actual style classes are designed to work with Tailwind CSS
9
+ * and assume a specific color system is in place.
10
+ */
11
+ /**
12
+ * Default variant styles using Tailwind CSS classes
13
+ * These assume a specific color system with surface and semantic colors
14
+ */
15
+ const DEFAULT_BADGE_VARIANTS = {
16
+ primary: 'bg-surface-tertiary text-content-secondary',
17
+ secondary: 'bg-surface-secondary text-content-body',
18
+ success: 'bg-success-900/80 text-success-300',
19
+ warning: 'bg-warning-900/80 text-warning-300',
20
+ error: 'bg-error-900/80 text-error-300',
21
+ info: 'bg-surface-tertiary text-content-body',
22
+ neutral: 'bg-surface-secondary text-content-muted',
23
+ };
24
+ /**
25
+ * Default size styles for badges
26
+ */
27
+ const DEFAULT_BADGE_SIZES = {
28
+ sm: 'text-xs px-1.5 py-0.5',
29
+ md: 'text-xs px-2 py-1',
30
+ lg: 'text-sm px-3 py-1.5',
31
+ };
32
+ /**
33
+ * Base badge styles that apply to all badges
34
+ */
35
+ const BASE_BADGE_STYLES = 'inline-flex items-center rounded-md font-medium whitespace-nowrap transition-colors';
36
+ /**
37
+ * Creates a badge style configuration with appropriate CSS classes
38
+ *
39
+ * @param config - Badge configuration options
40
+ * @param variantStyles - Optional custom variant styles (defaults to DEFAULT_BADGE_VARIANTS)
41
+ * @param sizeStyles - Optional custom size styles (defaults to DEFAULT_BADGE_SIZES)
42
+ * @returns Badge style object with className and accessibility attributes
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * // Create a success badge
47
+ * const successBadge = createBadge({ variant: 'success' });
48
+ * // Returns: { className: 'inline-flex items-center rounded-md font-medium whitespace-nowrap transition-colors bg-success-900/80 text-success-300 text-xs px-2 py-1', ariaLabel: 'success status' }
49
+ *
50
+ * // Create a large warning badge with custom class
51
+ * const warningBadge = createBadge({ variant: 'warning', size: 'lg', className: 'ml-2' });
52
+ *
53
+ * // Use custom variant styles
54
+ * const customVariants = { primary: 'bg-blue-500 text-white', ... };
55
+ * const customBadge = createBadge({ variant: 'primary' }, customVariants);
56
+ * ```
57
+ */
58
+ export function createBadge({ variant, size = 'md', className = '' }, variantStyles = DEFAULT_BADGE_VARIANTS, sizeStyles = DEFAULT_BADGE_SIZES) {
59
+ const variantClasses = variantStyles[variant] || variantStyles.neutral;
60
+ const sizeClasses = sizeStyles[size];
61
+ return {
62
+ className: `${BASE_BADGE_STYLES} ${variantClasses} ${sizeClasses} ${className}`.trim(),
63
+ ariaLabel: `${variant} status`,
64
+ };
65
+ }
66
+ /**
67
+ * Export the default style mappings for consumers who want to use them directly
68
+ * or extend them with their own styles
69
+ */
70
+ export const BADGE_VARIANTS = DEFAULT_BADGE_VARIANTS;
71
+ export const BADGE_SIZES = DEFAULT_BADGE_SIZES;
@@ -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/index.d.ts CHANGED
@@ -6,4 +6,7 @@
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';
10
+ export type { BadgeVariant, BadgeSize, BadgeConfig, BadgeStyle } from './badge';
11
+ export { createBadge, BADGE_VARIANTS, BADGE_SIZES } from './badge';
9
12
  //# 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;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;AAGpB,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -8,3 +8,6 @@
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';
13
+ export { createBadge, BADGE_VARIANTS, BADGE_SIZES } from './badge';
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@cranberry-money/shared-utils",
3
- "version": "4.2.0",
3
+ "version": "4.4.0",
4
4
  "description": "Shared utility functions for MyPortfolio platform",
5
+ "type": "module",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "exports": {
@@ -18,7 +19,9 @@
18
19
  "build": "tsc",
19
20
  "clean": "rm -rf dist",
20
21
  "typecheck": "tsc --noEmit",
21
- "dev": "tsc --watch"
22
+ "dev": "tsc --watch",
23
+ "lint": "eslint src",
24
+ "lint:fix": "eslint src --fix"
22
25
  },
23
26
  "dependencies": {
24
27
  "@cranberry-money/shared-constants": "^4.0.0",