@cranberry-money/shared-utils 1.0.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/README.md +150 -0
- package/dist/formatters/currency.d.ts +29 -0
- package/dist/formatters/currency.d.ts.map +1 -0
- package/dist/formatters/currency.js +55 -0
- package/dist/formatters/dates.d.ts +23 -0
- package/dist/formatters/dates.d.ts.map +1 -0
- package/dist/formatters/dates.js +50 -0
- package/dist/formatters/index.d.ts +7 -0
- package/dist/formatters/index.d.ts.map +1 -0
- package/dist/formatters/index.js +6 -0
- package/dist/formatters/numbers.d.ts +8 -0
- package/dist/formatters/numbers.d.ts.map +1 -0
- package/dist/formatters/numbers.js +12 -0
- package/dist/helpers/filters.d.ts +25 -0
- package/dist/helpers/filters.d.ts.map +1 -0
- package/dist/helpers/filters.js +41 -0
- package/dist/helpers/index.d.ts +6 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/index.js +5 -0
- package/dist/helpers/text.d.ts +8 -0
- package/dist/helpers/text.d.ts.map +1 -0
- package/dist/helpers/text.js +11 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# @myportfolio/shared-utils
|
|
2
|
+
|
|
3
|
+
Shared utility functions for the MyPortfolio platform, supporting both web (Blueberry) and mobile (Blackberry) applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @myportfolio/shared-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {
|
|
15
|
+
formatCurrency,
|
|
16
|
+
formatDate,
|
|
17
|
+
hasActiveFilters,
|
|
18
|
+
truncateText
|
|
19
|
+
} from '@myportfolio/shared-utils';
|
|
20
|
+
|
|
21
|
+
// Currency formatting
|
|
22
|
+
const amount = formatCurrency(1234.56); // "1,234.56"
|
|
23
|
+
|
|
24
|
+
// Date formatting
|
|
25
|
+
const date = formatDate('2024-01-15T10:30:00Z'); // "1/15/2024"
|
|
26
|
+
|
|
27
|
+
// Filter utilities
|
|
28
|
+
const hasFilters = hasActiveFilters({ name: 'Apple', category: '', page: 1 }); // true
|
|
29
|
+
|
|
30
|
+
// Text utilities
|
|
31
|
+
const short = truncateText('This is a very long text', 10); // "This is a..."
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Available Utilities
|
|
35
|
+
|
|
36
|
+
### Formatters
|
|
37
|
+
|
|
38
|
+
#### Currency (`formatters/currency.ts`)
|
|
39
|
+
- `formatCurrency(value)` - Format number with commas and 2 decimals
|
|
40
|
+
- `parseCurrencyInput(value)` - Parse currency string to number
|
|
41
|
+
- `formatCurrencyWithCode(value, code, locale, minDigits, maxDigits)` - Format with currency code
|
|
42
|
+
- `formatDefaultCurrency(value, minDigits, maxDigits)` - Format with default currency (AUD)
|
|
43
|
+
|
|
44
|
+
#### Dates (`formatters/dates.ts`)
|
|
45
|
+
- `formatDate(dateString, fallback)` - Format ISO date to localized date
|
|
46
|
+
- `formatShortDate(dateString, locale)` - Format to short date (e.g., "Jan 15")
|
|
47
|
+
- `formatTime(dateString, locale)` - Format to 24-hour time
|
|
48
|
+
- `formatDateTime(dateString, locale)` - Format to "Jan 15 14:30"
|
|
49
|
+
|
|
50
|
+
#### Numbers (`formatters/numbers.ts`)
|
|
51
|
+
- `formatShares(shares, locale)` - Format share quantities (no decimals)
|
|
52
|
+
|
|
53
|
+
### Helpers
|
|
54
|
+
|
|
55
|
+
#### Filters (`helpers/filters.ts`)
|
|
56
|
+
- `hasActiveFilters<T>(filters, excludeFields)` - Check if any filters are active
|
|
57
|
+
- `countActiveFilters<T>(filters, excludeFields)` - Count active filters
|
|
58
|
+
- `clearAllFilters<T>(filters, preserveFields)` - Clear all filter values
|
|
59
|
+
- `updateFilters<T>(currentFilters, updates)` - Type-safe filter updates
|
|
60
|
+
|
|
61
|
+
#### Text (`helpers/text.ts`)
|
|
62
|
+
- `truncateText(text, maxLength)` - Truncate text with ellipsis
|
|
63
|
+
|
|
64
|
+
## Platform Compatibility
|
|
65
|
+
|
|
66
|
+
This package is designed to work across:
|
|
67
|
+
- Web applications (React) - Direct use of utility functions
|
|
68
|
+
- Mobile applications (React Native) - Platform-agnostic implementations
|
|
69
|
+
- Node.js environments - For server-side utility needs
|
|
70
|
+
|
|
71
|
+
### Platform-Specific Considerations
|
|
72
|
+
|
|
73
|
+
#### Date Formatting
|
|
74
|
+
The date formatters use `Intl.DateTimeFormat` and `toLocaleDateString` which work consistently across web and React Native platforms.
|
|
75
|
+
|
|
76
|
+
#### Currency Formatting
|
|
77
|
+
Currency formatters use `Intl.NumberFormat` which provides consistent formatting across platforms with proper locale support.
|
|
78
|
+
|
|
79
|
+
#### Filter Utilities
|
|
80
|
+
Filter utilities are completely platform-agnostic and work with any TypeScript object structure.
|
|
81
|
+
|
|
82
|
+
## Examples
|
|
83
|
+
|
|
84
|
+
### Currency Formatting
|
|
85
|
+
```typescript
|
|
86
|
+
import { formatCurrency, formatCurrencyWithCode } from '@myportfolio/shared-utils';
|
|
87
|
+
|
|
88
|
+
// Basic formatting
|
|
89
|
+
formatCurrency(1234.56); // "1,234.56"
|
|
90
|
+
formatCurrency("1234.56"); // "1,234.56"
|
|
91
|
+
|
|
92
|
+
// With currency code
|
|
93
|
+
formatCurrencyWithCode(1234.56, 'USD', 'en-US', 2, 2); // "$1,234.56"
|
|
94
|
+
formatCurrencyWithCode(1234.56, 'AUD', 'en-AU', 0, 0); // "A$1,235"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Filter Management
|
|
98
|
+
```typescript
|
|
99
|
+
import { hasActiveFilters, countActiveFilters, clearAllFilters } from '@myportfolio/shared-utils';
|
|
100
|
+
|
|
101
|
+
const filters = {
|
|
102
|
+
searchQuery: 'apple',
|
|
103
|
+
category: 'technology',
|
|
104
|
+
minPrice: 100,
|
|
105
|
+
maxPrice: null,
|
|
106
|
+
tags: [],
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Check for active filters (excluding searchQuery by default)
|
|
110
|
+
hasActiveFilters(filters); // true (category and minPrice are active)
|
|
111
|
+
|
|
112
|
+
// Count active filters
|
|
113
|
+
countActiveFilters(filters); // 2 (category and minPrice)
|
|
114
|
+
|
|
115
|
+
// Clear all filters while preserving searchQuery
|
|
116
|
+
const cleared = clearAllFilters(filters, ['searchQuery']);
|
|
117
|
+
// Result: { searchQuery: 'apple' }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Date Formatting
|
|
121
|
+
```typescript
|
|
122
|
+
import { formatDate, formatDateTime, formatShortDate } from '@myportfolio/shared-utils';
|
|
123
|
+
|
|
124
|
+
const isoDate = '2024-01-15T14:30:00Z';
|
|
125
|
+
|
|
126
|
+
formatDate(isoDate); // "1/15/2024"
|
|
127
|
+
formatShortDate(isoDate); // "Jan 15"
|
|
128
|
+
formatDateTime(isoDate); // "Jan 15 14:30"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Development
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# Build the package
|
|
135
|
+
npm run build
|
|
136
|
+
|
|
137
|
+
# Watch for changes
|
|
138
|
+
npm run dev
|
|
139
|
+
|
|
140
|
+
# Type check
|
|
141
|
+
npm run typecheck
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Dependencies
|
|
145
|
+
|
|
146
|
+
- `@myportfolio/shared-constants` - For currency and formatting constants
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
MIT
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number as currency with commas and 2 decimal places
|
|
3
|
+
*/
|
|
4
|
+
export declare const formatCurrency: (value: number | string) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Parses a currency string input into a number
|
|
7
|
+
* @param value - Currency string to parse
|
|
8
|
+
* @returns Parsed number value (or 0 if invalid)
|
|
9
|
+
*/
|
|
10
|
+
export declare const parseCurrencyInput: (value: string) => number;
|
|
11
|
+
/**
|
|
12
|
+
* Formats a number as currency with specific currency code and locale
|
|
13
|
+
* @param value - Number to format
|
|
14
|
+
* @param currencyCode - ISO 4217 currency code (default: DEFAULT_CURRENCY)
|
|
15
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
16
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
17
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
18
|
+
* @returns Formatted currency string
|
|
19
|
+
*/
|
|
20
|
+
export declare const formatCurrencyWithCode: (value: number, currencyCode?: string, locale?: string, minimumFractionDigits?: number, maximumFractionDigits?: number) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Convenience function to format currency with the default currency (AUD)
|
|
23
|
+
* @param value - Number to format
|
|
24
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
25
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
26
|
+
* @returns Formatted currency string with default currency
|
|
27
|
+
*/
|
|
28
|
+
export declare const formatDefaultCurrency: (value: number, minimumFractionDigits?: number, maximumFractionDigits?: number) => string;
|
|
29
|
+
//# sourceMappingURL=currency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../../src/formatters/currency.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,GAAG,MAAM,KAAG,MAWvD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,KAAG,MAOlD,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,GACjC,OAAO,MAAM,EACb,eAAc,MAAyB,EACvC,SAAQ,MAAgB,EACxB,wBAAuB,MAAU,EACjC,wBAAuB,MAAU,KAChC,MASF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GAChC,OAAO,MAAM,EACb,wBAAuB,MAAU,EACjC,wBAAuB,MAAU,KAChC,MAEF,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { DEFAULT_CURRENCY } from '@myportfolio/shared-constants';
|
|
2
|
+
/**
|
|
3
|
+
* Formats a number as currency with commas and 2 decimal places
|
|
4
|
+
*/
|
|
5
|
+
export const formatCurrency = (value) => {
|
|
6
|
+
if (!value && value !== 0)
|
|
7
|
+
return '';
|
|
8
|
+
const numValue = typeof value === 'string' ? parseFloat(value) : value;
|
|
9
|
+
if (isNaN(numValue))
|
|
10
|
+
return '';
|
|
11
|
+
return numValue.toLocaleString('en-AU', {
|
|
12
|
+
minimumFractionDigits: 2,
|
|
13
|
+
maximumFractionDigits: 2,
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Parses a currency string input into a number
|
|
18
|
+
* @param value - Currency string to parse
|
|
19
|
+
* @returns Parsed number value (or 0 if invalid)
|
|
20
|
+
*/
|
|
21
|
+
export const parseCurrencyInput = (value) => {
|
|
22
|
+
const cleanValue = value.replace(/[^0-9.]/g, '');
|
|
23
|
+
const parts = cleanValue.split('.');
|
|
24
|
+
const formattedValue = parts[0] + (parts.length > 1 ? '.' + parts[1].slice(0, 2) : '');
|
|
25
|
+
return parseFloat(formattedValue) || 0;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Formats a number as currency with specific currency code and locale
|
|
29
|
+
* @param value - Number to format
|
|
30
|
+
* @param currencyCode - ISO 4217 currency code (default: DEFAULT_CURRENCY)
|
|
31
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
32
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
33
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
34
|
+
* @returns Formatted currency string
|
|
35
|
+
*/
|
|
36
|
+
export const formatCurrencyWithCode = (value, currencyCode = DEFAULT_CURRENCY, locale = 'en-AU', minimumFractionDigits = 0, maximumFractionDigits = 0) => {
|
|
37
|
+
if (value == null || isNaN(value))
|
|
38
|
+
return '';
|
|
39
|
+
return new Intl.NumberFormat(locale, {
|
|
40
|
+
style: 'currency',
|
|
41
|
+
currency: currencyCode,
|
|
42
|
+
minimumFractionDigits,
|
|
43
|
+
maximumFractionDigits,
|
|
44
|
+
}).format(value);
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Convenience function to format currency with the default currency (AUD)
|
|
48
|
+
* @param value - Number to format
|
|
49
|
+
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
50
|
+
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
51
|
+
* @returns Formatted currency string with default currency
|
|
52
|
+
*/
|
|
53
|
+
export const formatDefaultCurrency = (value, minimumFractionDigits = 0, maximumFractionDigits = 0) => {
|
|
54
|
+
return formatCurrencyWithCode(value, DEFAULT_CURRENCY, 'en-AU', minimumFractionDigits, maximumFractionDigits);
|
|
55
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a date string to a localized date
|
|
3
|
+
* @param dateString - ISO date string or null
|
|
4
|
+
* @param fallback - Fallback text when date is null (default: 'No expiry')
|
|
5
|
+
* @returns Formatted date string
|
|
6
|
+
*/
|
|
7
|
+
export declare const formatDate: (dateString: string | null, fallback?: string) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Formats a date string to a short date format (e.g., "Jan 15")
|
|
10
|
+
*/
|
|
11
|
+
export declare const formatShortDate: (dateString: string, locale?: string) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Formats a date string to a time format (24-hour)
|
|
14
|
+
*/
|
|
15
|
+
export declare const formatTime: (dateString: string, locale?: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* Formats a date string to a combined short date and time format
|
|
18
|
+
* @param dateString - ISO date string
|
|
19
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
20
|
+
* @returns Formatted string like "Jan 15 14:30"
|
|
21
|
+
*/
|
|
22
|
+
export declare const formatDateTime: (dateString: string, locale?: string) => string;
|
|
23
|
+
//# sourceMappingURL=dates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dates.d.ts","sourceRoot":"","sources":["../../src/formatters/dates.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,YAAY,MAAM,GAAG,IAAI,EAAE,WAAU,MAAoB,KAAG,MAGtF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,YAAY,MAAM,EAAE,SAAQ,MAAgB,KAAG,MAM9E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,YAAY,MAAM,EAAE,SAAQ,MAAgB,KAAG,MAMzE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,YAAY,MAAM,EAAE,SAAQ,MAAgB,KAAG,MAY7E,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a date string to a localized date
|
|
3
|
+
* @param dateString - ISO date string or null
|
|
4
|
+
* @param fallback - Fallback text when date is null (default: 'No expiry')
|
|
5
|
+
* @returns Formatted date string
|
|
6
|
+
*/
|
|
7
|
+
export const formatDate = (dateString, fallback = 'No expiry') => {
|
|
8
|
+
if (!dateString)
|
|
9
|
+
return fallback;
|
|
10
|
+
return new Date(dateString).toLocaleDateString();
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Formats a date string to a short date format (e.g., "Jan 15")
|
|
14
|
+
*/
|
|
15
|
+
export const formatShortDate = (dateString, locale = 'en-AU') => {
|
|
16
|
+
const date = new Date(dateString);
|
|
17
|
+
return date.toLocaleDateString(locale, {
|
|
18
|
+
month: 'short',
|
|
19
|
+
day: 'numeric',
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Formats a date string to a time format (24-hour)
|
|
24
|
+
*/
|
|
25
|
+
export const formatTime = (dateString, locale = 'en-AU') => {
|
|
26
|
+
return new Date(dateString).toLocaleTimeString(locale, {
|
|
27
|
+
hour: '2-digit',
|
|
28
|
+
minute: '2-digit',
|
|
29
|
+
hour12: false,
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Formats a date string to a combined short date and time format
|
|
34
|
+
* @param dateString - ISO date string
|
|
35
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
36
|
+
* @returns Formatted string like "Jan 15 14:30"
|
|
37
|
+
*/
|
|
38
|
+
export const formatDateTime = (dateString, locale = 'en-AU') => {
|
|
39
|
+
const date = new Date(dateString);
|
|
40
|
+
const shortDate = date.toLocaleDateString(locale, {
|
|
41
|
+
month: 'short',
|
|
42
|
+
day: 'numeric',
|
|
43
|
+
});
|
|
44
|
+
const time = date.toLocaleTimeString(locale, {
|
|
45
|
+
hour: '2-digit',
|
|
46
|
+
minute: '2-digit',
|
|
47
|
+
hour12: false,
|
|
48
|
+
});
|
|
49
|
+
return `${shortDate} ${time}`;
|
|
50
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/formatters/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number for displaying share quantities (no decimals)
|
|
3
|
+
* @param shares - Number of shares to format
|
|
4
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
5
|
+
* @returns Formatted shares string with commas as thousands separators
|
|
6
|
+
*/
|
|
7
|
+
export declare const formatShares: (shares: number, locale?: string) => string;
|
|
8
|
+
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/formatters/numbers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,EAAE,SAAQ,MAAgB,KAAG,MAKvE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number for displaying share quantities (no decimals)
|
|
3
|
+
* @param shares - Number of shares to format
|
|
4
|
+
* @param locale - Locale for formatting (default: 'en-AU')
|
|
5
|
+
* @returns Formatted shares string with commas as thousands separators
|
|
6
|
+
*/
|
|
7
|
+
export const formatShares = (shares, locale = 'en-AU') => {
|
|
8
|
+
return new Intl.NumberFormat(locale, {
|
|
9
|
+
minimumFractionDigits: 0,
|
|
10
|
+
maximumFractionDigits: 0,
|
|
11
|
+
}).format(shares);
|
|
12
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utility functions for trading filter operations
|
|
3
|
+
* Addresses filter utility logic duplications across trading pages
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Generic function to check if any filters are active
|
|
7
|
+
* Replaces hasActiveTradeFilters, hasActiveTransactionFilters, hasActiveInstrumentFilters
|
|
8
|
+
*/
|
|
9
|
+
export declare function hasActiveFilters<T extends Record<string, unknown>>(filters: T, excludeFields?: (keyof T)[]): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Generic function to count active filters
|
|
12
|
+
* Replaces countActiveTradeFilters, countActiveTransactionFilters, countActiveInstrumentFilters
|
|
13
|
+
*/
|
|
14
|
+
export declare function countActiveFilters<T extends Record<string, unknown>>(filters: T, excludeFields?: (keyof T)[]): number;
|
|
15
|
+
/**
|
|
16
|
+
* Generic function to clear all filters
|
|
17
|
+
* Maintains type safety while resetting filter state
|
|
18
|
+
*/
|
|
19
|
+
export declare function clearAllFilters<T extends Record<string, unknown>>(filters: T, preserveFields?: (keyof T)[]): Partial<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Type-safe filter update helper
|
|
22
|
+
* Ensures only valid filter fields can be updated
|
|
23
|
+
*/
|
|
24
|
+
export declare function updateFilters<T extends Record<string, unknown>>(currentFilters: T, updates: Partial<T>): T;
|
|
25
|
+
//# sourceMappingURL=filters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filters.d.ts","sourceRoot":"","sources":["../../src/helpers/filters.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChE,OAAO,EAAE,CAAC,EACV,aAAa,GAAE,CAAC,MAAM,CAAC,CAAC,EAAoB,GAC3C,OAAO,CAOT;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClE,OAAO,EAAE,CAAC,EACV,aAAa,GAAE,CAAC,MAAM,CAAC,CAAC,EAAoB,GAC3C,MAAM,CAOR;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/D,OAAO,EAAE,CAAC,EACV,cAAc,GAAE,CAAC,MAAM,CAAC,CAAC,EAAO,GAC/B,OAAO,CAAC,CAAC,CAAC,CAWZ;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,EAAE,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAE1G"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utility functions for trading filter operations
|
|
3
|
+
* Addresses filter utility logic duplications across trading pages
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Generic function to check if any filters are active
|
|
7
|
+
* Replaces hasActiveTradeFilters, hasActiveTransactionFilters, hasActiveInstrumentFilters
|
|
8
|
+
*/
|
|
9
|
+
export function hasActiveFilters(filters, excludeFields = ['searchQuery']) {
|
|
10
|
+
const relevantEntries = Object.entries(filters).filter(([key]) => !excludeFields.includes(key));
|
|
11
|
+
return relevantEntries.some(([, value]) => value !== undefined && value !== null && value !== '' && !(Array.isArray(value) && value.length === 0));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Generic function to count active filters
|
|
15
|
+
* Replaces countActiveTradeFilters, countActiveTransactionFilters, countActiveInstrumentFilters
|
|
16
|
+
*/
|
|
17
|
+
export function countActiveFilters(filters, excludeFields = ['searchQuery']) {
|
|
18
|
+
const relevantEntries = Object.entries(filters).filter(([key]) => !excludeFields.includes(key));
|
|
19
|
+
return relevantEntries.filter(([, value]) => value !== undefined && value !== null && value !== '' && !(Array.isArray(value) && value.length === 0)).length;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Generic function to clear all filters
|
|
23
|
+
* Maintains type safety while resetting filter state
|
|
24
|
+
*/
|
|
25
|
+
export function clearAllFilters(filters, preserveFields = []) {
|
|
26
|
+
const clearedFilters = {};
|
|
27
|
+
// Preserve specified fields
|
|
28
|
+
preserveFields.forEach((field) => {
|
|
29
|
+
if (field in filters) {
|
|
30
|
+
clearedFilters[field] = filters[field];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return clearedFilters;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Type-safe filter update helper
|
|
37
|
+
* Ensures only valid filter fields can be updated
|
|
38
|
+
*/
|
|
39
|
+
export function updateFilters(currentFilters, updates) {
|
|
40
|
+
return { ...currentFilters, ...updates };
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates text to a specified maximum length and adds ellipsis if needed
|
|
3
|
+
* @param text - The text to truncate
|
|
4
|
+
* @param maxLength - The maximum length of the text (default: 30)
|
|
5
|
+
* @returns The truncated text with ellipsis if it exceeds maxLength
|
|
6
|
+
*/
|
|
7
|
+
export declare const truncateText: (text: string, maxLength?: number) => string;
|
|
8
|
+
//# sourceMappingURL=text.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../src/helpers/text.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,EAAE,YAAW,MAAW,KAAG,MAGnE,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates text to a specified maximum length and adds ellipsis if needed
|
|
3
|
+
* @param text - The text to truncate
|
|
4
|
+
* @param maxLength - The maximum length of the text (default: 30)
|
|
5
|
+
* @returns The truncated text with ellipsis if it exceeds maxLength
|
|
6
|
+
*/
|
|
7
|
+
export const truncateText = (text, maxLength = 30) => {
|
|
8
|
+
if (text.length <= maxLength)
|
|
9
|
+
return text;
|
|
10
|
+
return `${text.substring(0, maxLength)}...`;
|
|
11
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cranberry-money/shared-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shared utility functions for MyPortfolio platform",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"clean": "rm -rf dist",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"dev": "tsc --watch"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@cranberry-money/shared-constants": "^1.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "^5.0.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"typescript": ">=5.0.0"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"typescript",
|
|
34
|
+
"utilities",
|
|
35
|
+
"shared",
|
|
36
|
+
"portfolio"
|
|
37
|
+
],
|
|
38
|
+
"author": "MyPortfolio Team",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "restricted",
|
|
42
|
+
"registry": "https://registry.npmjs.org/"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/your-org/cranberry.git",
|
|
47
|
+
"directory": "packages/shared-utils"
|
|
48
|
+
}
|
|
49
|
+
}
|