@star-insure/sdk 3.3.1 → 4.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 +31 -0
- package/dist/components/tables/TableRow.d.ts +1 -1
- package/dist/lib/dates.d.ts +11 -5
- package/dist/lib/money.d.ts +5 -1
- package/dist/sdk.cjs.development.js +113 -19
- package/dist/sdk.cjs.development.js.map +1 -1
- package/dist/sdk.cjs.production.min.js +1 -1
- package/dist/sdk.cjs.production.min.js.map +1 -1
- package/dist/sdk.esm.js +112 -19
- package/dist/sdk.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/components/common/Toasts.tsx +1 -1
- package/src/components/tables/TableRow.tsx +2 -2
- package/src/lib/dates.ts +96 -15
- package/src/lib/money.ts +15 -1
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@star-insure/sdk",
|
|
3
3
|
"description": "The SDK for Star Insure client apps with shared helper functions and TypeScript definitions.",
|
|
4
4
|
"author": "alexclark_nz",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "4.0.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -7,7 +7,7 @@ export default function Toasts() {
|
|
|
7
7
|
const { toasts } = useToast();
|
|
8
8
|
|
|
9
9
|
return (
|
|
10
|
-
<div className="fixed bottom-4 right-4 z-[
|
|
10
|
+
<div className="fixed bottom-4 right-4 z-[120] flex flex-col gap-6">
|
|
11
11
|
{toasts.map((toast: Toast) => (
|
|
12
12
|
<ToastItem {...toast} key={toast._id} />
|
|
13
13
|
))}
|
|
@@ -4,14 +4,14 @@ import cn from 'classnames';
|
|
|
4
4
|
interface Props extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLTableRowElement>, HTMLTableRowElement> {
|
|
5
5
|
children: React.ReactNode;
|
|
6
6
|
className?: string;
|
|
7
|
-
onClick?: () => void;
|
|
7
|
+
onClick?: (e?: React.SyntheticEvent) => void;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export default function TableRow({ children, className = '', onClick = () => {}, ...props }: Props) {
|
|
11
11
|
const bgClass = className.includes('bg') ? '' : 'bg-white even:bg-gray-50'
|
|
12
12
|
|
|
13
13
|
return (
|
|
14
|
-
<tr {...props} className={cn(className, bgClass, 'hover:bg-gray-100')} onClick={onClick}>
|
|
14
|
+
<tr {...props} className={cn(className, bgClass, 'hover:bg-gray-100')} onClick={(e) => onClick(e)}>
|
|
15
15
|
{children}
|
|
16
16
|
</tr>
|
|
17
17
|
)
|
package/src/lib/dates.ts
CHANGED
|
@@ -1,30 +1,111 @@
|
|
|
1
1
|
import { format, parse } from "date-fns";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Formats a date like "Jan 1, 2022 at 9:50am"
|
|
5
|
+
*/
|
|
6
|
+
export function formatDateNice(date: string|null|undefined, includeTime: boolean = true) {
|
|
7
|
+
const formatString = includeTime ? "MMM d, yyyy 'at' h:mma" : 'MMM d, yyyy';
|
|
8
|
+
const formatFunc = includeTime ? formatDateTime : formatDate;
|
|
9
|
+
|
|
10
|
+
return formatFunc(date, formatString);
|
|
6
11
|
}
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Formats a date like "01/01/2022 09:50"
|
|
15
|
+
*/
|
|
16
|
+
export function formatDateForTable(date: string|null|undefined, includeTime: boolean = true) {
|
|
17
|
+
return includeTime ? formatDateTime(date) : formatDate(date);
|
|
10
18
|
}
|
|
11
19
|
|
|
12
20
|
/**
|
|
13
|
-
* Formats a date
|
|
21
|
+
* Formats a date to Star Insure standard format - dd/mm/yyyy
|
|
14
22
|
*/
|
|
15
|
-
export function
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
export function formatDate(date: Date|string|null|undefined, overrideFormatString?: string) {
|
|
24
|
+
// If date is undefined or null
|
|
25
|
+
if (!date) {
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const formatString = overrideFormatString || 'dd/MM/yyyy';
|
|
30
|
+
|
|
31
|
+
// If date is a date object
|
|
32
|
+
if (date instanceof Date) {
|
|
33
|
+
return format(date, formatString);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* If date is a string, first attempt to parse it in non-standard formats
|
|
38
|
+
* Only parse the first 10 characters of the string
|
|
39
|
+
* dd/MM/yyyy, dd-MM-yyyy, dd.MM.yyyy
|
|
40
|
+
*/
|
|
41
|
+
const nonStandardDate = date
|
|
42
|
+
.substring(0, 10)
|
|
43
|
+
.split('-').join('/')
|
|
44
|
+
.split('.').join('/');
|
|
18
45
|
|
|
19
|
-
|
|
46
|
+
const parsedDate = parse(nonStandardDate, 'dd/MM/yyyy', new Date());
|
|
47
|
+
if (parsedDate.toString() !== 'Invalid Date') {
|
|
48
|
+
return format(parsedDate, formatString);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Then attempt to parse it natively
|
|
52
|
+
const nativelyParsedDate = new Date(date);
|
|
53
|
+
if (nativelyParsedDate.toString() !== 'Invalid Date') {
|
|
54
|
+
return format(nativelyParsedDate, formatString);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return '';
|
|
20
58
|
}
|
|
21
59
|
|
|
22
60
|
/**
|
|
23
|
-
* Formats a
|
|
61
|
+
* Formats a dateTime to Star Insure standard format - dd/mm/yyyy hh:mm
|
|
24
62
|
*/
|
|
25
|
-
export function
|
|
26
|
-
|
|
27
|
-
|
|
63
|
+
export function formatDateTime(dateTime: Date|string|null|undefined, overrideFormatString?: string) {
|
|
64
|
+
// If date is undefined or null
|
|
65
|
+
if (!dateTime) {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const formatString = overrideFormatString || 'dd/MM/yyyy HH:mm';
|
|
70
|
+
|
|
71
|
+
// If date is a date object
|
|
72
|
+
if (dateTime instanceof Date) {
|
|
73
|
+
return format(dateTime, formatString);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* If date is a string, first attempt to parse it in non-standard formats
|
|
78
|
+
* dd/MM/yyyy HH:mm, dd-MM-yyyy HH:mm, dd.MM.yyyy HH:mm,
|
|
79
|
+
* dd/MM/yyyy hh:mm a, dd-MM-yyyy hh:mm a, dd.MM.yyyy hh:mm a,
|
|
80
|
+
* dd/MM/yyyy HH:mm:ss, dd-MM-yyyy HH:mm:ss, dd.MM.yyyy HH:mm:ss,
|
|
81
|
+
* dd/MM/yyyy hh:mm:ss a, dd-MM-yyyy hh:mm:ss a, dd.MM.yyyy hh:mm:ss a
|
|
82
|
+
*/
|
|
83
|
+
const nonStandardDateTime = dateTime
|
|
84
|
+
// Remove commas between date and time
|
|
85
|
+
.split(',').join('')
|
|
86
|
+
|
|
87
|
+
// Only parse the first 22 characters of the string
|
|
88
|
+
.substring(0, 22)
|
|
89
|
+
|
|
90
|
+
// Replace dashes and dots with slashes
|
|
91
|
+
.split('-').join('/')
|
|
92
|
+
.split('.').join('/');
|
|
93
|
+
|
|
94
|
+
const is12Hour = ['AM', 'PM'].includes(nonStandardDateTime.slice(-2).toUpperCase());
|
|
95
|
+
const hasSeconds = nonStandardDateTime.split(':').length === 3;
|
|
96
|
+
|
|
97
|
+
const nonStandardFormat = (`dd/MM/yyyy ${is12Hour ? 'hh' : 'HH'}:mm${hasSeconds ? ':ss' : ''} ${is12Hour ? 'a' : ''}`).trim();
|
|
98
|
+
|
|
99
|
+
const parsedDateTime = parse(nonStandardDateTime, nonStandardFormat, new Date());
|
|
100
|
+
if (parsedDateTime.toString() !== 'Invalid Date') {
|
|
101
|
+
return format(parsedDateTime, formatString);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Then attempt to parse it natively
|
|
105
|
+
const nativelyParsedDateTime = new Date(dateTime);
|
|
106
|
+
if (nativelyParsedDateTime.toString() !== 'Invalid Date') {
|
|
107
|
+
return format(nativelyParsedDateTime, formatString);
|
|
108
|
+
}
|
|
28
109
|
|
|
29
|
-
return
|
|
110
|
+
return '';
|
|
30
111
|
}
|
package/src/lib/money.ts
CHANGED
|
@@ -52,7 +52,7 @@ export function gstCalc(input: number|string): {
|
|
|
52
52
|
/**
|
|
53
53
|
* Formats a value nicely
|
|
54
54
|
*/
|
|
55
|
-
export function
|
|
55
|
+
export function formatMoneyNumerals(value: string|number, decimals: number = 2): string {
|
|
56
56
|
let toFormat = value;
|
|
57
57
|
if (typeof value === 'string') {
|
|
58
58
|
toFormat = value.replace(/[^\d.-]/g, '')
|
|
@@ -64,6 +64,20 @@ export function gstCalc(input: number|string): {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Formats a value nicely
|
|
69
|
+
*/
|
|
70
|
+
export function formatMoney(value: string|number|null|undefined, decimals: number = 2): string {
|
|
71
|
+
if (value === null || value === undefined) {
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return `$${formatMoneyNumerals(value, decimals)}`
|
|
76
|
+
|
|
77
|
+
// If the value is negative, we need to move the negative sign to the front
|
|
78
|
+
.replace('$-', '- $');
|
|
79
|
+
}
|
|
80
|
+
|
|
67
81
|
/**
|
|
68
82
|
* Turns a formatted value in to a float
|
|
69
83
|
*/
|