@cranberry-money/shared-utils 3.0.1 → 3.0.3
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/validation/index.d.ts +48 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +77 -0
- package/package.json +4 -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/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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;AAC1B,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utility functions
|
|
3
|
+
* Common validation logic for forms and data
|
|
4
|
+
*/
|
|
5
|
+
import type { PasswordValidation, TokenValidation } from '@cranberry-money/shared-types';
|
|
6
|
+
/**
|
|
7
|
+
* Checks if a string contains only numeric characters
|
|
8
|
+
* @param str - The string to validate
|
|
9
|
+
* @returns true if the string contains only digits, false otherwise
|
|
10
|
+
*/
|
|
11
|
+
export declare const isNumericOnly: (str: string) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Validates password strength based on application requirements
|
|
14
|
+
* @param password - The password to validate
|
|
15
|
+
* @returns An object containing validation results
|
|
16
|
+
*/
|
|
17
|
+
export declare const validatePassword: (password: string) => PasswordValidation;
|
|
18
|
+
/**
|
|
19
|
+
* Validates if a verification token is in the expected format
|
|
20
|
+
* @param token - The verification token to validate
|
|
21
|
+
* @returns true if token appears to be valid format, false otherwise
|
|
22
|
+
*/
|
|
23
|
+
export declare const isValidTokenFormat: (token: string) => boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Formats a verification token by removing whitespace and converting to uppercase
|
|
26
|
+
* @param token - The token to format
|
|
27
|
+
* @returns The formatted token
|
|
28
|
+
*/
|
|
29
|
+
export declare const formatVerificationToken: (token: string) => string;
|
|
30
|
+
/**
|
|
31
|
+
* Validates email confirmation form data
|
|
32
|
+
* @param token - The verification token
|
|
33
|
+
* @returns An object containing validation results
|
|
34
|
+
*/
|
|
35
|
+
export declare const validateEmailConfirmation: (token: string) => TokenValidation;
|
|
36
|
+
/**
|
|
37
|
+
* Validates email format
|
|
38
|
+
* @param email - The email to validate
|
|
39
|
+
* @returns true if email format is valid
|
|
40
|
+
*/
|
|
41
|
+
export declare const isValidEmail: (email: string) => boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Validates phone number format (basic validation)
|
|
44
|
+
* @param phone - The phone number to validate
|
|
45
|
+
* @returns true if phone format is valid
|
|
46
|
+
*/
|
|
47
|
+
export declare const isValidPhone: (phone: string) => boolean;
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;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"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utility functions
|
|
3
|
+
* Common validation logic for forms and data
|
|
4
|
+
*/
|
|
5
|
+
import { PASSWORD_MIN_LENGTH, TOKEN_MIN_LENGTH, TOKEN_MAX_LENGTH } from '@cranberry-money/shared-constants';
|
|
6
|
+
/**
|
|
7
|
+
* Checks if a string contains only numeric characters
|
|
8
|
+
* @param str - The string to validate
|
|
9
|
+
* @returns true if the string contains only digits, false otherwise
|
|
10
|
+
*/
|
|
11
|
+
export const isNumericOnly = (str) => {
|
|
12
|
+
return /^\d+$/.test(str);
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Validates password strength based on application requirements
|
|
16
|
+
* @param password - The password to validate
|
|
17
|
+
* @returns An object containing validation results
|
|
18
|
+
*/
|
|
19
|
+
export const validatePassword = (password) => {
|
|
20
|
+
return {
|
|
21
|
+
lengthValid: password.length >= PASSWORD_MIN_LENGTH,
|
|
22
|
+
notNumeric: !isNumericOnly(password),
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Validates if a verification token is in the expected format
|
|
27
|
+
* @param token - The verification token to validate
|
|
28
|
+
* @returns true if token appears to be valid format, false otherwise
|
|
29
|
+
*/
|
|
30
|
+
export const isValidTokenFormat = (token) => {
|
|
31
|
+
const trimmedToken = token.trim();
|
|
32
|
+
// Tokens are alphanumeric and within length limits
|
|
33
|
+
return (trimmedToken.length >= TOKEN_MIN_LENGTH &&
|
|
34
|
+
trimmedToken.length <= TOKEN_MAX_LENGTH &&
|
|
35
|
+
/^[a-zA-Z0-9]+$/.test(trimmedToken));
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Formats a verification token by removing whitespace and converting to uppercase
|
|
39
|
+
* @param token - The token to format
|
|
40
|
+
* @returns The formatted token
|
|
41
|
+
*/
|
|
42
|
+
export const formatVerificationToken = (token) => {
|
|
43
|
+
return token.trim().toUpperCase();
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Validates email confirmation form data
|
|
47
|
+
* @param token - The verification token
|
|
48
|
+
* @returns An object containing validation results
|
|
49
|
+
*/
|
|
50
|
+
export const validateEmailConfirmation = (token) => {
|
|
51
|
+
const formattedToken = formatVerificationToken(token);
|
|
52
|
+
return {
|
|
53
|
+
isValid: formattedToken.length > 0 && isValidTokenFormat(formattedToken),
|
|
54
|
+
isEmpty: formattedToken.length === 0,
|
|
55
|
+
isValidFormat: isValidTokenFormat(formattedToken),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Validates email format
|
|
60
|
+
* @param email - The email to validate
|
|
61
|
+
* @returns true if email format is valid
|
|
62
|
+
*/
|
|
63
|
+
export const isValidEmail = (email) => {
|
|
64
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
65
|
+
return emailRegex.test(email);
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Validates phone number format (basic validation)
|
|
69
|
+
* @param phone - The phone number to validate
|
|
70
|
+
* @returns true if phone format is valid
|
|
71
|
+
*/
|
|
72
|
+
export const isValidPhone = (phone) => {
|
|
73
|
+
// Remove common formatting characters
|
|
74
|
+
const cleaned = phone.replace(/[\s\-\(\)]/g, '');
|
|
75
|
+
// Check if it's numeric and reasonable length
|
|
76
|
+
return /^\+?\d{10,15}$/.test(cleaned);
|
|
77
|
+
};
|
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.3",
|
|
4
4
|
"description": "Shared utility functions for MyPortfolio platform",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
"dev": "tsc --watch"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cranberry-money/shared-constants": "^3.0.
|
|
25
|
-
"@cranberry-money/shared-services": "^3.0.0"
|
|
24
|
+
"@cranberry-money/shared-constants": "^3.0.1",
|
|
25
|
+
"@cranberry-money/shared-services": "^3.0.0",
|
|
26
|
+
"@cranberry-money/shared-types": "^3.0.5"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"typescript": "^5.0.0"
|