@cranberry-money/shared-utils 3.0.2 → 3.0.4
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/formatters/currency.d.ts +1 -1
- package/dist/formatters/currency.d.ts.map +1 -1
- package/dist/formatters/currency.js +5 -5
- package/dist/formatters/dates.d.ts +1 -1
- package/dist/formatters/dates.d.ts.map +1 -1
- package/dist/formatters/dates.js +5 -4
- package/dist/formatters/numbers.d.ts +1 -1
- package/dist/formatters/numbers.d.ts.map +1 -1
- package/dist/formatters/numbers.js +3 -2
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/portfolio.d.ts +20 -0
- package/dist/helpers/portfolio.d.ts.map +1 -0
- package/dist/helpers/portfolio.js +28 -0
- package/dist/validation/index.d.ts +0 -8
- package/dist/validation/index.d.ts.map +1 -1
- package/dist/validation/index.js +4 -11
- package/package.json +3 -3
|
@@ -12,7 +12,7 @@ export declare const parseCurrencyInput: (value: string) => number;
|
|
|
12
12
|
* Formats a number as currency with specific currency code and locale
|
|
13
13
|
* @param value - Number to format
|
|
14
14
|
* @param currencyCode - ISO 4217 currency code (default: DEFAULT_CURRENCY)
|
|
15
|
-
* @param locale - Locale for formatting (default:
|
|
15
|
+
* @param locale - Locale for formatting (default: LOCALE_AUSTRALIA)
|
|
16
16
|
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
17
17
|
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
18
18
|
* @returns Formatted currency string
|
|
@@ -1 +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,
|
|
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,MAAyB,EACjC,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"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_CURRENCY } from '@cranberry-money/shared-constants';
|
|
1
|
+
import { DEFAULT_CURRENCY, LOCALE_AUSTRALIA } from '@cranberry-money/shared-constants';
|
|
2
2
|
/**
|
|
3
3
|
* Formats a number as currency with commas and 2 decimal places
|
|
4
4
|
*/
|
|
@@ -8,7 +8,7 @@ export const formatCurrency = (value) => {
|
|
|
8
8
|
const numValue = typeof value === 'string' ? parseFloat(value) : value;
|
|
9
9
|
if (isNaN(numValue))
|
|
10
10
|
return '';
|
|
11
|
-
return numValue.toLocaleString(
|
|
11
|
+
return numValue.toLocaleString(LOCALE_AUSTRALIA, {
|
|
12
12
|
minimumFractionDigits: 2,
|
|
13
13
|
maximumFractionDigits: 2,
|
|
14
14
|
});
|
|
@@ -28,12 +28,12 @@ export const parseCurrencyInput = (value) => {
|
|
|
28
28
|
* Formats a number as currency with specific currency code and locale
|
|
29
29
|
* @param value - Number to format
|
|
30
30
|
* @param currencyCode - ISO 4217 currency code (default: DEFAULT_CURRENCY)
|
|
31
|
-
* @param locale - Locale for formatting (default:
|
|
31
|
+
* @param locale - Locale for formatting (default: LOCALE_AUSTRALIA)
|
|
32
32
|
* @param minimumFractionDigits - Minimum decimal places (default: 0)
|
|
33
33
|
* @param maximumFractionDigits - Maximum decimal places (default: 0)
|
|
34
34
|
* @returns Formatted currency string
|
|
35
35
|
*/
|
|
36
|
-
export const formatCurrencyWithCode = (value, currencyCode = DEFAULT_CURRENCY, locale =
|
|
36
|
+
export const formatCurrencyWithCode = (value, currencyCode = DEFAULT_CURRENCY, locale = LOCALE_AUSTRALIA, minimumFractionDigits = 0, maximumFractionDigits = 0) => {
|
|
37
37
|
if (value == null || isNaN(value))
|
|
38
38
|
return '';
|
|
39
39
|
return new Intl.NumberFormat(locale, {
|
|
@@ -51,5 +51,5 @@ export const formatCurrencyWithCode = (value, currencyCode = DEFAULT_CURRENCY, l
|
|
|
51
51
|
* @returns Formatted currency string with default currency
|
|
52
52
|
*/
|
|
53
53
|
export const formatDefaultCurrency = (value, minimumFractionDigits = 0, maximumFractionDigits = 0) => {
|
|
54
|
-
return formatCurrencyWithCode(value, DEFAULT_CURRENCY,
|
|
54
|
+
return formatCurrencyWithCode(value, DEFAULT_CURRENCY, LOCALE_AUSTRALIA, minimumFractionDigits, maximumFractionDigits);
|
|
55
55
|
};
|
|
@@ -16,7 +16,7 @@ export declare const formatTime: (dateString: string, locale?: string) => string
|
|
|
16
16
|
/**
|
|
17
17
|
* Formats a date string to a combined short date and time format
|
|
18
18
|
* @param dateString - ISO date string
|
|
19
|
-
* @param locale - Locale for formatting (default:
|
|
19
|
+
* @param locale - Locale for formatting (default: LOCALE_AUSTRALIA)
|
|
20
20
|
* @returns Formatted string like "Jan 15 14:30"
|
|
21
21
|
*/
|
|
22
22
|
export declare const formatDateTime: (dateString: string, locale?: string) => string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dates.d.ts","sourceRoot":"","sources":["../../src/formatters/dates.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dates.d.ts","sourceRoot":"","sources":["../../src/formatters/dates.ts"],"names":[],"mappings":"AAEA;;;;;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,MAAyB,KAAG,MAMvF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,GAAI,YAAY,MAAM,EAAE,SAAQ,MAAyB,KAAG,MAMlF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,YAAY,MAAM,EAAE,SAAQ,MAAyB,KAAG,MAYtF,CAAC"}
|
package/dist/formatters/dates.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LOCALE_AUSTRALIA } from '@cranberry-money/shared-constants';
|
|
1
2
|
/**
|
|
2
3
|
* Formats a date string to a localized date
|
|
3
4
|
* @param dateString - ISO date string or null
|
|
@@ -12,7 +13,7 @@ export const formatDate = (dateString, fallback = 'No expiry') => {
|
|
|
12
13
|
/**
|
|
13
14
|
* Formats a date string to a short date format (e.g., "Jan 15")
|
|
14
15
|
*/
|
|
15
|
-
export const formatShortDate = (dateString, locale =
|
|
16
|
+
export const formatShortDate = (dateString, locale = LOCALE_AUSTRALIA) => {
|
|
16
17
|
const date = new Date(dateString);
|
|
17
18
|
return date.toLocaleDateString(locale, {
|
|
18
19
|
month: 'short',
|
|
@@ -22,7 +23,7 @@ export const formatShortDate = (dateString, locale = 'en-AU') => {
|
|
|
22
23
|
/**
|
|
23
24
|
* Formats a date string to a time format (24-hour)
|
|
24
25
|
*/
|
|
25
|
-
export const formatTime = (dateString, locale =
|
|
26
|
+
export const formatTime = (dateString, locale = LOCALE_AUSTRALIA) => {
|
|
26
27
|
return new Date(dateString).toLocaleTimeString(locale, {
|
|
27
28
|
hour: '2-digit',
|
|
28
29
|
minute: '2-digit',
|
|
@@ -32,10 +33,10 @@ export const formatTime = (dateString, locale = 'en-AU') => {
|
|
|
32
33
|
/**
|
|
33
34
|
* Formats a date string to a combined short date and time format
|
|
34
35
|
* @param dateString - ISO date string
|
|
35
|
-
* @param locale - Locale for formatting (default:
|
|
36
|
+
* @param locale - Locale for formatting (default: LOCALE_AUSTRALIA)
|
|
36
37
|
* @returns Formatted string like "Jan 15 14:30"
|
|
37
38
|
*/
|
|
38
|
-
export const formatDateTime = (dateString, locale =
|
|
39
|
+
export const formatDateTime = (dateString, locale = LOCALE_AUSTRALIA) => {
|
|
39
40
|
const date = new Date(dateString);
|
|
40
41
|
const shortDate = date.toLocaleDateString(locale, {
|
|
41
42
|
month: 'short',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Formats a number for displaying share quantities (no decimals)
|
|
3
3
|
* @param shares - Number of shares to format
|
|
4
|
-
* @param locale - Locale for formatting (default:
|
|
4
|
+
* @param locale - Locale for formatting (default: LOCALE_AUSTRALIA)
|
|
5
5
|
* @returns Formatted shares string with commas as thousands separators
|
|
6
6
|
*/
|
|
7
7
|
export declare const formatShares: (shares: number, locale?: string) => string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/formatters/numbers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/formatters/numbers.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,EAAE,SAAQ,MAAyB,KAAG,MAKhF,CAAC"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { LOCALE_AUSTRALIA } from '@cranberry-money/shared-constants';
|
|
1
2
|
/**
|
|
2
3
|
* Formats a number for displaying share quantities (no decimals)
|
|
3
4
|
* @param shares - Number of shares to format
|
|
4
|
-
* @param locale - Locale for formatting (default:
|
|
5
|
+
* @param locale - Locale for formatting (default: LOCALE_AUSTRALIA)
|
|
5
6
|
* @returns Formatted shares string with commas as thousands separators
|
|
6
7
|
*/
|
|
7
|
-
export const formatShares = (shares, locale =
|
|
8
|
+
export const formatShares = (shares, locale = LOCALE_AUSTRALIA) => {
|
|
8
9
|
return new Intl.NumberFormat(locale, {
|
|
9
10
|
minimumFractionDigits: 0,
|
|
10
11
|
maximumFractionDigits: 0,
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1 +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"}
|
|
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;AACvB,cAAc,aAAa,CAAC"}
|
package/dist/helpers/index.js
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portfolio-related helper functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Calculate total portfolio value
|
|
6
|
+
*/
|
|
7
|
+
export declare const calculateTotalValue: (marketValue: number, cashBalance: number) => number;
|
|
8
|
+
/**
|
|
9
|
+
* Calculate market allocation percentage
|
|
10
|
+
*/
|
|
11
|
+
export declare const getMarketAllocation: (marketValue: number, totalValue: number) => number;
|
|
12
|
+
/**
|
|
13
|
+
* Calculate cash allocation percentage
|
|
14
|
+
*/
|
|
15
|
+
export declare const getCashAllocation: (cashBalance: number, totalValue: number) => number;
|
|
16
|
+
/**
|
|
17
|
+
* Format portfolio value with default currency
|
|
18
|
+
*/
|
|
19
|
+
export declare const formatPortfolioValue: (value: number) => string;
|
|
20
|
+
//# sourceMappingURL=portfolio.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"portfolio.d.ts","sourceRoot":"","sources":["../../src/helpers/portfolio.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,aAAa,MAAM,EAAE,aAAa,MAAM,KAAG,MAE9E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,aAAa,MAAM,EAAE,YAAY,MAAM,KAAG,MAE7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,aAAa,MAAM,EAAE,YAAY,MAAM,KAAG,MAE3E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,GAAI,OAAO,MAAM,KAAG,MAEpD,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portfolio-related helper functions
|
|
3
|
+
*/
|
|
4
|
+
import { formatCurrency } from '../formatters/currency';
|
|
5
|
+
/**
|
|
6
|
+
* Calculate total portfolio value
|
|
7
|
+
*/
|
|
8
|
+
export const calculateTotalValue = (marketValue, cashBalance) => {
|
|
9
|
+
return marketValue + cashBalance;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Calculate market allocation percentage
|
|
13
|
+
*/
|
|
14
|
+
export const getMarketAllocation = (marketValue, totalValue) => {
|
|
15
|
+
return totalValue > 0 ? (marketValue / totalValue) * 100 : 0;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Calculate cash allocation percentage
|
|
19
|
+
*/
|
|
20
|
+
export const getCashAllocation = (cashBalance, totalValue) => {
|
|
21
|
+
return totalValue > 0 ? (cashBalance / totalValue) * 100 : 0;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Format portfolio value with default currency
|
|
25
|
+
*/
|
|
26
|
+
export const formatPortfolioValue = (value) => {
|
|
27
|
+
return formatCurrency(value);
|
|
28
|
+
};
|
|
@@ -3,14 +3,6 @@
|
|
|
3
3
|
* Common validation logic for forms and data
|
|
4
4
|
*/
|
|
5
5
|
import type { PasswordValidation, TokenValidation } from '@cranberry-money/shared-types';
|
|
6
|
-
/**
|
|
7
|
-
* Validation rule constants
|
|
8
|
-
*/
|
|
9
|
-
export declare const VALIDATION_RULES: {
|
|
10
|
-
readonly PASSWORD_MIN_LENGTH: 8;
|
|
11
|
-
readonly TOKEN_MIN_LENGTH: 6;
|
|
12
|
-
readonly TOKEN_MAX_LENGTH: 20;
|
|
13
|
-
};
|
|
14
6
|
/**
|
|
15
7
|
* Checks if a string contains only numeric characters
|
|
16
8
|
* @param str - The string to validate
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAGzF;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,KAAK,MAAM,KAAG,OAE3C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,UAAU,MAAM,KAAG,kBAKnD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,GAAI,OAAO,MAAM,KAAG,OAQlD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,GAAI,OAAO,MAAM,KAAG,MAEvD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,GAAI,OAAO,MAAM,KAAG,eAQzD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,KAAG,OAG5C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,KAAG,OAK5C,CAAC"}
|
package/dist/validation/index.js
CHANGED
|
@@ -2,14 +2,7 @@
|
|
|
2
2
|
* Validation utility functions
|
|
3
3
|
* Common validation logic for forms and data
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
* Validation rule constants
|
|
7
|
-
*/
|
|
8
|
-
export const VALIDATION_RULES = {
|
|
9
|
-
PASSWORD_MIN_LENGTH: 8,
|
|
10
|
-
TOKEN_MIN_LENGTH: 6,
|
|
11
|
-
TOKEN_MAX_LENGTH: 20,
|
|
12
|
-
};
|
|
5
|
+
import { PASSWORD_MIN_LENGTH, TOKEN_MIN_LENGTH, TOKEN_MAX_LENGTH } from '@cranberry-money/shared-constants';
|
|
13
6
|
/**
|
|
14
7
|
* Checks if a string contains only numeric characters
|
|
15
8
|
* @param str - The string to validate
|
|
@@ -25,7 +18,7 @@ export const isNumericOnly = (str) => {
|
|
|
25
18
|
*/
|
|
26
19
|
export const validatePassword = (password) => {
|
|
27
20
|
return {
|
|
28
|
-
lengthValid: password.length >=
|
|
21
|
+
lengthValid: password.length >= PASSWORD_MIN_LENGTH,
|
|
29
22
|
notNumeric: !isNumericOnly(password),
|
|
30
23
|
};
|
|
31
24
|
};
|
|
@@ -37,8 +30,8 @@ export const validatePassword = (password) => {
|
|
|
37
30
|
export const isValidTokenFormat = (token) => {
|
|
38
31
|
const trimmedToken = token.trim();
|
|
39
32
|
// Tokens are alphanumeric and within length limits
|
|
40
|
-
return (trimmedToken.length >=
|
|
41
|
-
trimmedToken.length <=
|
|
33
|
+
return (trimmedToken.length >= TOKEN_MIN_LENGTH &&
|
|
34
|
+
trimmedToken.length <= TOKEN_MAX_LENGTH &&
|
|
42
35
|
/^[a-zA-Z0-9]+$/.test(trimmedToken));
|
|
43
36
|
};
|
|
44
37
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cranberry-money/shared-utils",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "Shared utility functions for MyPortfolio platform",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"dev": "tsc --watch"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cranberry-money/shared-constants": "^3.0.
|
|
24
|
+
"@cranberry-money/shared-constants": "^3.0.1",
|
|
25
25
|
"@cranberry-money/shared-services": "^3.0.0",
|
|
26
|
-
"@cranberry-money/shared-types": "^3.0.
|
|
26
|
+
"@cranberry-money/shared-types": "^3.0.5"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"typescript": "^5.0.0"
|