@cranberry-money/shared-utils 8.17.7 → 8.17.8
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/allocations.d.ts +18 -0
- package/dist/allocations.js +20 -0
- package/dist/cash-account.d.ts +43 -0
- package/dist/cash-account.d.ts.map +1 -0
- package/dist/cash-account.js +52 -0
- package/dist/collections.d.ts +81 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +127 -0
- package/dist/country.d.ts +108 -0
- package/dist/country.d.ts.map +1 -0
- package/dist/country.js +116 -0
- package/dist/currency.d.ts +99 -0
- package/dist/currency.d.ts.map +1 -0
- package/dist/currency.js +128 -0
- package/dist/dashboard.d.ts +72 -0
- package/dist/dashboard.js +121 -0
- package/dist/date.d.ts +64 -0
- package/dist/date.d.ts.map +1 -0
- package/dist/date.js +91 -0
- package/dist/document.d.ts +38 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +56 -0
- package/dist/downloads.d.ts +46 -0
- package/dist/downloads.d.ts.map +1 -0
- package/dist/downloads.js +91 -0
- package/dist/filters.d.ts +121 -0
- package/dist/filters.d.ts.map +1 -0
- package/dist/filters.js +206 -0
- package/dist/formatting.d.ts +59 -0
- package/dist/formatting.d.ts.map +1 -0
- package/dist/formatting.js +81 -0
- package/dist/holdings.d.ts +79 -0
- package/dist/holdings.js +139 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +68 -0
- package/dist/industry.d.ts +128 -0
- package/dist/industry.d.ts.map +1 -0
- package/dist/industry.js +152 -0
- package/dist/investment-preference.d.ts +25 -0
- package/dist/investment-preference.js +33 -0
- package/dist/numbers.d.ts +72 -0
- package/dist/numbers.d.ts.map +1 -0
- package/dist/numbers.js +101 -0
- package/dist/portfolio-template.d.ts +57 -0
- package/dist/portfolio-template.d.ts.map +1 -0
- package/dist/portfolio-template.js +60 -0
- package/dist/portfolio.d.ts +68 -0
- package/dist/portfolio.d.ts.map +1 -0
- package/dist/portfolio.js +87 -0
- package/dist/sector.d.ts +124 -0
- package/dist/sector.d.ts.map +1 -0
- package/dist/sector.js +134 -0
- package/dist/stock-exchange.d.ts +89 -0
- package/dist/stock-exchange.d.ts.map +1 -0
- package/dist/stock-exchange.js +101 -0
- package/dist/tax-residency.d.ts +67 -0
- package/dist/tax-residency.d.ts.map +1 -0
- package/dist/tax-residency.js +70 -0
- package/dist/text.d.ts +22 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +25 -0
- package/dist/withdrawal-status.d.ts +72 -0
- package/dist/withdrawal-status.d.ts.map +1 -0
- package/dist/withdrawal-status.js +127 -0
- package/package.json +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AssetAllocation } from '@cranberry-money/shared-types';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if allocation percentages sum to 100%
|
|
4
|
+
* Allows for small floating point errors (e.g., 99.99 or 100.01)
|
|
5
|
+
*
|
|
6
|
+
* @param allocations - Array of asset allocations to validate
|
|
7
|
+
* @returns true if allocations sum to approximately 100%, false otherwise
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const allocations = [
|
|
11
|
+
* { percentage: '50.00' },
|
|
12
|
+
* { percentage: '30.00' },
|
|
13
|
+
* { percentage: '20.00' }
|
|
14
|
+
* ];
|
|
15
|
+
* validateAllocations(allocations); // returns true
|
|
16
|
+
*/
|
|
17
|
+
export declare const validateAllocations: (allocations: readonly AssetAllocation[]) => boolean;
|
|
18
|
+
//# sourceMappingURL=allocations.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates if allocation percentages sum to 100%
|
|
3
|
+
* Allows for small floating point errors (e.g., 99.99 or 100.01)
|
|
4
|
+
*
|
|
5
|
+
* @param allocations - Array of asset allocations to validate
|
|
6
|
+
* @returns true if allocations sum to approximately 100%, false otherwise
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const allocations = [
|
|
10
|
+
* { percentage: '50.00' },
|
|
11
|
+
* { percentage: '30.00' },
|
|
12
|
+
* { percentage: '20.00' }
|
|
13
|
+
* ];
|
|
14
|
+
* validateAllocations(allocations); // returns true
|
|
15
|
+
*/
|
|
16
|
+
export const validateAllocations = (allocations) => {
|
|
17
|
+
const totalPercentage = allocations.reduce((sum, allocation) => sum + parseFloat(allocation.percentage), 0);
|
|
18
|
+
// Allow for small floating point errors (e.g., 99.99 or 100.01)
|
|
19
|
+
return Math.abs(totalPercentage - 100) < 0.01;
|
|
20
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base transaction type interface for type flexibility
|
|
3
|
+
*/
|
|
4
|
+
export interface BaseTransactionType {
|
|
5
|
+
readonly [key: string]: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Format cash account balance with AUD currency formatting
|
|
9
|
+
*
|
|
10
|
+
* @param balance - Balance value to format (string or number)
|
|
11
|
+
* @returns Formatted currency string using Australian locale and AUD currency
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* formatBalance(1234.56); // returns '$1,234.56'
|
|
15
|
+
* formatBalance('1000'); // returns '$1,000.00'
|
|
16
|
+
* formatBalance(0); // returns '$0.00'
|
|
17
|
+
*/
|
|
18
|
+
export declare const formatBalance: (balance: string | number) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Format transaction amount with signed currency formatting (shows + or -)
|
|
21
|
+
*
|
|
22
|
+
* @param amount - Transaction amount to format (string or number)
|
|
23
|
+
* @returns Formatted signed currency string using Australian locale and AUD currency
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* formatTransactionAmount(1234.56); // returns '+$1,234.56'
|
|
27
|
+
* formatTransactionAmount(-500); // returns '-$500.00'
|
|
28
|
+
* formatTransactionAmount('1000'); // returns '+$1,000.00'
|
|
29
|
+
*/
|
|
30
|
+
export declare const formatTransactionAmount: (amount: string | number) => string;
|
|
31
|
+
/**
|
|
32
|
+
* Get human-readable label for transaction type
|
|
33
|
+
*
|
|
34
|
+
* @param transactionType - Transaction type key
|
|
35
|
+
* @param labels - Optional custom transaction type labels (defaults to shared constants)
|
|
36
|
+
* @returns Human-readable transaction type label
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* getTransactionTypeLabel('DEPOSIT'); // returns transaction type label from constants
|
|
40
|
+
* getTransactionTypeLabel('WITHDRAWAL'); // returns transaction type label from constants
|
|
41
|
+
*/
|
|
42
|
+
export declare const getTransactionTypeLabel: <T extends string>(transactionType: T, labels?: Record<T, string>) => string;
|
|
43
|
+
//# sourceMappingURL=cash-account.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cash-account.d.ts","sourceRoot":"","sources":["../src/cash-account.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CAChC;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,GAAI,SAAS,MAAM,GAAG,MAAM,KAAG,MAMxD,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GAAI,QAAQ,MAAM,GAAG,MAAM,KAAG,MAMjE,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,GAAI,CAAC,SAAS,MAAM,EACtD,iBAAiB,CAAC,EAClB,SAAQ,MAAM,CAAC,CAAC,EAAE,MAAM,CAA6D,KACpF,MAEF,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { LOCALE_AUSTRALIA, CURRENCY_AUD, CASH_ACCOUNT_TRANSACTION_TYPE_LABELS, } from '@cranberry-money/shared-constants';
|
|
2
|
+
import { NUMBER_FORMAT_OPTIONS_CURRENCY, NUMBER_FORMAT_OPTIONS_CURRENCY_SIGNED } from './currency';
|
|
3
|
+
/**
|
|
4
|
+
* Format cash account balance with AUD currency formatting
|
|
5
|
+
*
|
|
6
|
+
* @param balance - Balance value to format (string or number)
|
|
7
|
+
* @returns Formatted currency string using Australian locale and AUD currency
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* formatBalance(1234.56); // returns '$1,234.56'
|
|
11
|
+
* formatBalance('1000'); // returns '$1,000.00'
|
|
12
|
+
* formatBalance(0); // returns '$0.00'
|
|
13
|
+
*/
|
|
14
|
+
export const formatBalance = (balance) => {
|
|
15
|
+
const numBalance = typeof balance === 'string' ? parseFloat(balance) : balance;
|
|
16
|
+
return new Intl.NumberFormat(LOCALE_AUSTRALIA, {
|
|
17
|
+
...NUMBER_FORMAT_OPTIONS_CURRENCY,
|
|
18
|
+
currency: CURRENCY_AUD,
|
|
19
|
+
}).format(numBalance);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Format transaction amount with signed currency formatting (shows + or -)
|
|
23
|
+
*
|
|
24
|
+
* @param amount - Transaction amount to format (string or number)
|
|
25
|
+
* @returns Formatted signed currency string using Australian locale and AUD currency
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* formatTransactionAmount(1234.56); // returns '+$1,234.56'
|
|
29
|
+
* formatTransactionAmount(-500); // returns '-$500.00'
|
|
30
|
+
* formatTransactionAmount('1000'); // returns '+$1,000.00'
|
|
31
|
+
*/
|
|
32
|
+
export const formatTransactionAmount = (amount) => {
|
|
33
|
+
const numAmount = typeof amount === 'string' ? parseFloat(amount) : amount;
|
|
34
|
+
return new Intl.NumberFormat(LOCALE_AUSTRALIA, {
|
|
35
|
+
...NUMBER_FORMAT_OPTIONS_CURRENCY_SIGNED,
|
|
36
|
+
currency: CURRENCY_AUD,
|
|
37
|
+
}).format(numAmount);
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Get human-readable label for transaction type
|
|
41
|
+
*
|
|
42
|
+
* @param transactionType - Transaction type key
|
|
43
|
+
* @param labels - Optional custom transaction type labels (defaults to shared constants)
|
|
44
|
+
* @returns Human-readable transaction type label
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* getTransactionTypeLabel('DEPOSIT'); // returns transaction type label from constants
|
|
48
|
+
* getTransactionTypeLabel('WITHDRAWAL'); // returns transaction type label from constants
|
|
49
|
+
*/
|
|
50
|
+
export const getTransactionTypeLabel = (transactionType, labels = CASH_ACCOUNT_TRANSACTION_TYPE_LABELS) => {
|
|
51
|
+
return labels[transactionType];
|
|
52
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic collection and array utilities
|
|
3
|
+
* Pure functions for common array operations with type safety
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Sort array of objects by a string field
|
|
7
|
+
* @param items - Array of objects to sort
|
|
8
|
+
* @param fieldName - Name of the field to sort by
|
|
9
|
+
* @returns New sorted array
|
|
10
|
+
*/
|
|
11
|
+
export declare function sortByStringField<T>(items: T[], fieldName: keyof T): T[];
|
|
12
|
+
/**
|
|
13
|
+
* Filter array by text search in a specific field (case-insensitive)
|
|
14
|
+
* @param items - Array of objects to filter
|
|
15
|
+
* @param fieldName - Name of the field to search in
|
|
16
|
+
* @param searchTerm - Search term
|
|
17
|
+
* @returns Filtered array
|
|
18
|
+
*/
|
|
19
|
+
export declare function filterByTextSearch<T>(items: T[], fieldName: keyof T, searchTerm: string): T[];
|
|
20
|
+
/**
|
|
21
|
+
* Filter array by boolean field
|
|
22
|
+
* @param items - Array of objects to filter
|
|
23
|
+
* @param fieldName - Name of the boolean field
|
|
24
|
+
* @param value - Boolean value to filter by
|
|
25
|
+
* @returns Filtered array
|
|
26
|
+
*/
|
|
27
|
+
export declare function filterByBooleanField<T>(items: T[], fieldName: keyof T, value: boolean): T[];
|
|
28
|
+
/**
|
|
29
|
+
* Find item by exact field match
|
|
30
|
+
* @param items - Array of objects to search
|
|
31
|
+
* @param fieldName - Name of the field to match
|
|
32
|
+
* @param value - Value to match
|
|
33
|
+
* @returns Found item or undefined
|
|
34
|
+
*/
|
|
35
|
+
export declare function findByField<T>(items: T[], fieldName: keyof T, value: unknown): T | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Find item by case-insensitive string field match
|
|
38
|
+
* @param items - Array of objects to search
|
|
39
|
+
* @param fieldName - Name of the string field to match
|
|
40
|
+
* @param value - String value to match (case-insensitive)
|
|
41
|
+
* @returns Found item or undefined
|
|
42
|
+
*/
|
|
43
|
+
export declare function findByStringField<T>(items: T[], fieldName: keyof T, value: string): T | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Extract values from a specific field and sort them
|
|
46
|
+
* @param items - Array of objects
|
|
47
|
+
* @param fieldName - Name of the field to extract
|
|
48
|
+
* @returns Sorted array of extracted values
|
|
49
|
+
*/
|
|
50
|
+
export declare function extractAndSortField<T, K extends keyof T>(items: T[], fieldName: K): T[K][];
|
|
51
|
+
/**
|
|
52
|
+
* Group array items by the first character of a string field
|
|
53
|
+
* @param items - Array of objects to group
|
|
54
|
+
* @param fieldName - Name of the string field to group by
|
|
55
|
+
* @returns Object with first letters as keys and arrays of items as values
|
|
56
|
+
*/
|
|
57
|
+
export declare function groupByFirstLetter<T>(items: T[], fieldName: keyof T): Record<string, T[]>;
|
|
58
|
+
/**
|
|
59
|
+
* Group array items by a field value
|
|
60
|
+
* @param items - Array of objects to group
|
|
61
|
+
* @param fieldName - Name of the field to group by
|
|
62
|
+
* @returns Object with field values as keys and arrays of items as values
|
|
63
|
+
*/
|
|
64
|
+
export declare function groupByField<T, K extends keyof T>(items: T[], fieldName: K): Record<string, T[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Check if any item in array has a specific boolean field value
|
|
67
|
+
* @param items - Array of objects to check
|
|
68
|
+
* @param fieldName - Name of the boolean field
|
|
69
|
+
* @param value - Boolean value to check for
|
|
70
|
+
* @returns true if any item matches, false otherwise
|
|
71
|
+
*/
|
|
72
|
+
export declare function hasItemWithFieldValue<T>(items: T[], fieldName: keyof T, value: unknown): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Count items that match a field value
|
|
75
|
+
* @param items - Array of objects to count
|
|
76
|
+
* @param fieldName - Name of the field to check
|
|
77
|
+
* @param value - Value to count
|
|
78
|
+
* @returns Number of matching items
|
|
79
|
+
*/
|
|
80
|
+
export declare function countByFieldValue<T>(items: T[], fieldName: keyof T, value: unknown): number;
|
|
81
|
+
//# sourceMappingURL=collections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.d.ts","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAMxE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,UAAU,EAAE,MAAM,GAAG,CAAC,EAAE,CAM7F;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,EAAE,CAE3F;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,CAAC,GAAG,SAAS,CAE5F;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAMjG;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAE1F;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAazF;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAYhG;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAEhG;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,MAAM,CAE3F"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic collection and array utilities
|
|
3
|
+
* Pure functions for common array operations with type safety
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Sort array of objects by a string field
|
|
7
|
+
* @param items - Array of objects to sort
|
|
8
|
+
* @param fieldName - Name of the field to sort by
|
|
9
|
+
* @returns New sorted array
|
|
10
|
+
*/
|
|
11
|
+
export function sortByStringField(items, fieldName) {
|
|
12
|
+
return [...items].sort((a, b) => {
|
|
13
|
+
const aValue = String(a[fieldName]);
|
|
14
|
+
const bValue = String(b[fieldName]);
|
|
15
|
+
return aValue.localeCompare(bValue);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Filter array by text search in a specific field (case-insensitive)
|
|
20
|
+
* @param items - Array of objects to filter
|
|
21
|
+
* @param fieldName - Name of the field to search in
|
|
22
|
+
* @param searchTerm - Search term
|
|
23
|
+
* @returns Filtered array
|
|
24
|
+
*/
|
|
25
|
+
export function filterByTextSearch(items, fieldName, searchTerm) {
|
|
26
|
+
const lowercaseSearch = searchTerm.toLowerCase();
|
|
27
|
+
return items.filter(item => {
|
|
28
|
+
const fieldValue = String(item[fieldName]).toLowerCase();
|
|
29
|
+
return fieldValue.includes(lowercaseSearch);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Filter array by boolean field
|
|
34
|
+
* @param items - Array of objects to filter
|
|
35
|
+
* @param fieldName - Name of the boolean field
|
|
36
|
+
* @param value - Boolean value to filter by
|
|
37
|
+
* @returns Filtered array
|
|
38
|
+
*/
|
|
39
|
+
export function filterByBooleanField(items, fieldName, value) {
|
|
40
|
+
return items.filter(item => Boolean(item[fieldName]) === value);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Find item by exact field match
|
|
44
|
+
* @param items - Array of objects to search
|
|
45
|
+
* @param fieldName - Name of the field to match
|
|
46
|
+
* @param value - Value to match
|
|
47
|
+
* @returns Found item or undefined
|
|
48
|
+
*/
|
|
49
|
+
export function findByField(items, fieldName, value) {
|
|
50
|
+
return items.find(item => item[fieldName] === value);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Find item by case-insensitive string field match
|
|
54
|
+
* @param items - Array of objects to search
|
|
55
|
+
* @param fieldName - Name of the string field to match
|
|
56
|
+
* @param value - String value to match (case-insensitive)
|
|
57
|
+
* @returns Found item or undefined
|
|
58
|
+
*/
|
|
59
|
+
export function findByStringField(items, fieldName, value) {
|
|
60
|
+
const lowercaseValue = value.toLowerCase();
|
|
61
|
+
return items.find(item => {
|
|
62
|
+
const fieldValue = String(item[fieldName]).toLowerCase();
|
|
63
|
+
return fieldValue === lowercaseValue;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Extract values from a specific field and sort them
|
|
68
|
+
* @param items - Array of objects
|
|
69
|
+
* @param fieldName - Name of the field to extract
|
|
70
|
+
* @returns Sorted array of extracted values
|
|
71
|
+
*/
|
|
72
|
+
export function extractAndSortField(items, fieldName) {
|
|
73
|
+
return items.map(item => item[fieldName]).sort();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Group array items by the first character of a string field
|
|
77
|
+
* @param items - Array of objects to group
|
|
78
|
+
* @param fieldName - Name of the string field to group by
|
|
79
|
+
* @returns Object with first letters as keys and arrays of items as values
|
|
80
|
+
*/
|
|
81
|
+
export function groupByFirstLetter(items, fieldName) {
|
|
82
|
+
return items.reduce((groups, item) => {
|
|
83
|
+
const fieldValue = String(item[fieldName]);
|
|
84
|
+
const firstLetter = fieldValue.charAt(0).toUpperCase();
|
|
85
|
+
if (!groups[firstLetter]) {
|
|
86
|
+
groups[firstLetter] = [];
|
|
87
|
+
}
|
|
88
|
+
groups[firstLetter].push(item);
|
|
89
|
+
return groups;
|
|
90
|
+
}, {});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Group array items by a field value
|
|
94
|
+
* @param items - Array of objects to group
|
|
95
|
+
* @param fieldName - Name of the field to group by
|
|
96
|
+
* @returns Object with field values as keys and arrays of items as values
|
|
97
|
+
*/
|
|
98
|
+
export function groupByField(items, fieldName) {
|
|
99
|
+
return items.reduce((groups, item) => {
|
|
100
|
+
const fieldValue = String(item[fieldName]);
|
|
101
|
+
if (!groups[fieldValue]) {
|
|
102
|
+
groups[fieldValue] = [];
|
|
103
|
+
}
|
|
104
|
+
groups[fieldValue].push(item);
|
|
105
|
+
return groups;
|
|
106
|
+
}, {});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if any item in array has a specific boolean field value
|
|
110
|
+
* @param items - Array of objects to check
|
|
111
|
+
* @param fieldName - Name of the boolean field
|
|
112
|
+
* @param value - Boolean value to check for
|
|
113
|
+
* @returns true if any item matches, false otherwise
|
|
114
|
+
*/
|
|
115
|
+
export function hasItemWithFieldValue(items, fieldName, value) {
|
|
116
|
+
return items.some(item => item[fieldName] === value);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Count items that match a field value
|
|
120
|
+
* @param items - Array of objects to count
|
|
121
|
+
* @param fieldName - Name of the field to check
|
|
122
|
+
* @param value - Value to count
|
|
123
|
+
* @returns Number of matching items
|
|
124
|
+
*/
|
|
125
|
+
export function countByFieldValue(items, fieldName, value) {
|
|
126
|
+
return items.filter(item => item[fieldName] === value).length;
|
|
127
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base interface for country objects
|
|
3
|
+
*/
|
|
4
|
+
export interface BaseCountry {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly code: string;
|
|
7
|
+
readonly uuid: string;
|
|
8
|
+
readonly isAvailable: boolean;
|
|
9
|
+
readonly dialCode?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Sort countries by name
|
|
13
|
+
*
|
|
14
|
+
* @param countries - Array of countries to sort
|
|
15
|
+
* @returns Sorted array of countries
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const sorted = sortCountriesByName(countries);
|
|
19
|
+
*/
|
|
20
|
+
export declare const sortCountriesByName: <T extends BaseCountry>(countries: readonly T[]) => T[];
|
|
21
|
+
/**
|
|
22
|
+
* Filter countries by name (case-insensitive search)
|
|
23
|
+
*
|
|
24
|
+
* @param countries - Array of countries to filter
|
|
25
|
+
* @param searchTerm - Search term to filter by
|
|
26
|
+
* @returns Filtered array of countries
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const filtered = filterCountriesByName(countries, 'united');
|
|
30
|
+
* // Returns countries with 'united' in the name
|
|
31
|
+
*/
|
|
32
|
+
export declare const filterCountriesByName: <T extends BaseCountry>(countries: readonly T[], searchTerm: string) => T[];
|
|
33
|
+
/**
|
|
34
|
+
* Filter countries by availability
|
|
35
|
+
*
|
|
36
|
+
* @param countries - Array of countries to filter
|
|
37
|
+
* @returns Array of available countries
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const available = filterAvailableCountries(countries);
|
|
41
|
+
*/
|
|
42
|
+
export declare const filterAvailableCountries: <T extends BaseCountry>(countries: readonly T[]) => T[];
|
|
43
|
+
/**
|
|
44
|
+
* Find country by exact name match
|
|
45
|
+
*
|
|
46
|
+
* @param countries - Array of countries to search
|
|
47
|
+
* @param name - Exact name to find
|
|
48
|
+
* @returns Found country or undefined
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const country = findCountryByName(countries, 'United States');
|
|
52
|
+
*/
|
|
53
|
+
export declare const findCountryByName: <T extends BaseCountry>(countries: readonly T[], name: string) => T | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Find country by country code
|
|
56
|
+
*
|
|
57
|
+
* @param countries - Array of countries to search
|
|
58
|
+
* @param code - Country code to find (e.g., 'US', 'CA')
|
|
59
|
+
* @returns Found country or undefined
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const country = findCountryByCode(countries, 'US');
|
|
63
|
+
*/
|
|
64
|
+
export declare const findCountryByCode: <T extends BaseCountry>(countries: readonly T[], code: string) => T | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Get sorted list of country names
|
|
67
|
+
*
|
|
68
|
+
* @param countries - Array of countries
|
|
69
|
+
* @returns Sorted array of country names
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* const names = getCountryNames(countries);
|
|
73
|
+
* // ['Australia', 'Canada', 'United States', ...]
|
|
74
|
+
*/
|
|
75
|
+
export declare const getCountryNames: <T extends BaseCountry>(countries: readonly T[]) => string[];
|
|
76
|
+
/**
|
|
77
|
+
* Group countries alphabetically by first letter
|
|
78
|
+
*
|
|
79
|
+
* @param countries - Array of countries to group
|
|
80
|
+
* @returns Record of countries grouped by first letter
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* const grouped = groupCountriesAlphabetically(countries);
|
|
84
|
+
* // { 'A': [...], 'B': [...], 'C': [...], ... }
|
|
85
|
+
*/
|
|
86
|
+
export declare const groupCountriesAlphabetically: <T extends BaseCountry>(countries: readonly T[]) => Record<string, T[]>;
|
|
87
|
+
/**
|
|
88
|
+
* Format country with dial code for display
|
|
89
|
+
*
|
|
90
|
+
* @param country - Country to format
|
|
91
|
+
* @returns Formatted string with country name and dial code
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* formatCountryWithDialCode({ name: 'United States', dialCode: '+1', ... });
|
|
95
|
+
* // Returns 'United States (+1)'
|
|
96
|
+
*/
|
|
97
|
+
export declare const formatCountryWithDialCode: <T extends BaseCountry>(country: T) => string;
|
|
98
|
+
/**
|
|
99
|
+
* Check if a country is available
|
|
100
|
+
*
|
|
101
|
+
* @param country - Country to check
|
|
102
|
+
* @returns True if country is available
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* const available = isCountryAvailable(country);
|
|
106
|
+
*/
|
|
107
|
+
export declare const isCountryAvailable: <T extends BaseCountry>(country: T) => boolean;
|
|
108
|
+
//# sourceMappingURL=country.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"country.d.ts","sourceRoot":"","sources":["../src/country.ts"],"names":[],"mappings":"AASA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,KAAG,CAAC,EAErF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,EAAE,YAAY,MAAM,KAAG,CAAC,EAE3G,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,wBAAwB,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,KAAG,CAAC,EAE1F,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,EAAE,MAAM,MAAM,KAAG,CAAC,GAAG,SAEpG,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,EAAE,MAAM,MAAM,KAAG,CAAC,GAAG,SAEpG,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,KAAG,MAAM,EAEtF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,4BAA4B,GAAI,CAAC,SAAS,WAAW,EAAE,WAAW,SAAS,CAAC,EAAE,KAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,CAE/G,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,yBAAyB,GAAI,CAAC,SAAS,WAAW,EAAE,SAAS,CAAC,KAAG,MAE7E,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,WAAW,EAAE,SAAS,CAAC,KAAG,OAEtE,CAAC"}
|
package/dist/country.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { sortByStringField, filterByTextSearch, filterByBooleanField, findByStringField, extractAndSortField, groupByFirstLetter, } from './collections';
|
|
2
|
+
/**
|
|
3
|
+
* Sort countries by name
|
|
4
|
+
*
|
|
5
|
+
* @param countries - Array of countries to sort
|
|
6
|
+
* @returns Sorted array of countries
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const sorted = sortCountriesByName(countries);
|
|
10
|
+
*/
|
|
11
|
+
export const sortCountriesByName = (countries) => {
|
|
12
|
+
return sortByStringField([...countries], 'name');
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Filter countries by name (case-insensitive search)
|
|
16
|
+
*
|
|
17
|
+
* @param countries - Array of countries to filter
|
|
18
|
+
* @param searchTerm - Search term to filter by
|
|
19
|
+
* @returns Filtered array of countries
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const filtered = filterCountriesByName(countries, 'united');
|
|
23
|
+
* // Returns countries with 'united' in the name
|
|
24
|
+
*/
|
|
25
|
+
export const filterCountriesByName = (countries, searchTerm) => {
|
|
26
|
+
return filterByTextSearch([...countries], 'name', searchTerm);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Filter countries by availability
|
|
30
|
+
*
|
|
31
|
+
* @param countries - Array of countries to filter
|
|
32
|
+
* @returns Array of available countries
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const available = filterAvailableCountries(countries);
|
|
36
|
+
*/
|
|
37
|
+
export const filterAvailableCountries = (countries) => {
|
|
38
|
+
return filterByBooleanField([...countries], 'isAvailable', true);
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Find country by exact name match
|
|
42
|
+
*
|
|
43
|
+
* @param countries - Array of countries to search
|
|
44
|
+
* @param name - Exact name to find
|
|
45
|
+
* @returns Found country or undefined
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const country = findCountryByName(countries, 'United States');
|
|
49
|
+
*/
|
|
50
|
+
export const findCountryByName = (countries, name) => {
|
|
51
|
+
return findByStringField([...countries], 'name', name);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Find country by country code
|
|
55
|
+
*
|
|
56
|
+
* @param countries - Array of countries to search
|
|
57
|
+
* @param code - Country code to find (e.g., 'US', 'CA')
|
|
58
|
+
* @returns Found country or undefined
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const country = findCountryByCode(countries, 'US');
|
|
62
|
+
*/
|
|
63
|
+
export const findCountryByCode = (countries, code) => {
|
|
64
|
+
return findByStringField([...countries], 'code', code);
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Get sorted list of country names
|
|
68
|
+
*
|
|
69
|
+
* @param countries - Array of countries
|
|
70
|
+
* @returns Sorted array of country names
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* const names = getCountryNames(countries);
|
|
74
|
+
* // ['Australia', 'Canada', 'United States', ...]
|
|
75
|
+
*/
|
|
76
|
+
export const getCountryNames = (countries) => {
|
|
77
|
+
return extractAndSortField([...countries], 'name');
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Group countries alphabetically by first letter
|
|
81
|
+
*
|
|
82
|
+
* @param countries - Array of countries to group
|
|
83
|
+
* @returns Record of countries grouped by first letter
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* const grouped = groupCountriesAlphabetically(countries);
|
|
87
|
+
* // { 'A': [...], 'B': [...], 'C': [...], ... }
|
|
88
|
+
*/
|
|
89
|
+
export const groupCountriesAlphabetically = (countries) => {
|
|
90
|
+
return groupByFirstLetter([...countries], 'name');
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Format country with dial code for display
|
|
94
|
+
*
|
|
95
|
+
* @param country - Country to format
|
|
96
|
+
* @returns Formatted string with country name and dial code
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* formatCountryWithDialCode({ name: 'United States', dialCode: '+1', ... });
|
|
100
|
+
* // Returns 'United States (+1)'
|
|
101
|
+
*/
|
|
102
|
+
export const formatCountryWithDialCode = (country) => {
|
|
103
|
+
return country.dialCode ? `${country.name} (${country.dialCode})` : country.name;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Check if a country is available
|
|
107
|
+
*
|
|
108
|
+
* @param country - Country to check
|
|
109
|
+
* @returns True if country is available
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* const available = isCountryAvailable(country);
|
|
113
|
+
*/
|
|
114
|
+
export const isCountryAvailable = (country) => {
|
|
115
|
+
return country.isAvailable;
|
|
116
|
+
};
|
|
@@ -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
|