@star-insure/sdk 3.3.1 → 4.1.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 CHANGED
@@ -23,3 +23,34 @@ From the command line just run (you'll need Node version 18+ running):
23
23
  ```
24
24
  npm run publish
25
25
  ```
26
+
27
+ ## Usage
28
+ This library provides the following functions:
29
+
30
+ ### Date and Time
31
+ 1. formatDate - Returns a string in format dd/mm/yyyy
32
+ ```typescript
33
+ import { formatDate } from '@star-insure/sdk';
34
+
35
+ formatDate(new Date());
36
+ formatDate('2024-03-23T20:00:00.000Z');
37
+ formatDate('Sat, 23 Mar 2024 20:00:00 GMT');
38
+ ```
39
+
40
+ 2. formatDateTime - Returns a string in format dd/mm/yyyy hh:mm (24-hour clock)
41
+ ```typescript
42
+ import { formatDateTime } from '@star-insure/sdk';
43
+
44
+ formatDateTime(new Date());
45
+ formatDateTime('2024-03-23T20:00:00.000Z');
46
+ formatDateTime('Sat, 23 Mar 2024 20:00:00 GMT');
47
+ ```
48
+
49
+ ### Money
50
+ 1. formatMoney - Returns a string in format $x,xxx.xx
51
+ ```typescript
52
+ import { formatMoney } from '@star-insure/sdk';
53
+
54
+ formatMoney(1234567.89);
55
+ formatMoney('1234567.89');
56
+ ```
@@ -4,7 +4,8 @@ interface Props {
4
4
  isActive?: boolean;
5
5
  onClose: () => void;
6
6
  title?: string;
7
+ width?: string;
7
8
  className?: string;
8
9
  }
9
- export default function Modal({ children, isActive, onClose, title, className }: Props): JSX.Element;
10
+ export default function Modal({ children, isActive, onClose, title, width, className }: Props): JSX.Element;
10
11
  export {};
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  interface Props extends React.DetailedHTMLProps<React.HTMLAttributes<HTMLTableRowElement>, HTMLTableRowElement> {
3
3
  children: React.ReactNode;
4
4
  className?: string;
5
- onClick?: () => void;
5
+ onClick?: (e?: React.SyntheticEvent) => void;
6
6
  }
7
7
  export default function TableRow({ children, className, onClick, ...props }: Props): JSX.Element;
8
8
  export {};
@@ -1,10 +1,16 @@
1
- export declare function parseDate(dateString: string): Date;
2
- export declare function parseDateTime(dateTimeString: string): Date;
3
1
  /**
4
2
  * Formats a date like "Jan 1, 2022 at 9:50am"
5
3
  */
6
- export declare function formatDateNice(date: string, includeTime?: boolean): string;
4
+ export declare function formatDateNice(date: string | null | undefined, includeTime?: boolean): string;
7
5
  /**
8
- * Formats a date like "01/01/2022 9:50AM"
6
+ * Formats a date like "01/01/2022 09:50"
9
7
  */
10
- export declare function formatDateForTable(date: string, includeTime?: boolean): string;
8
+ export declare function formatDateForTable(date: string | null | undefined, includeTime?: boolean): string;
9
+ /**
10
+ * Formats a date to Star Insure standard format - dd/mm/yyyy
11
+ */
12
+ export declare function formatDate(date: Date | string | null | undefined, overrideFormatString?: string): string;
13
+ /**
14
+ * Formats a dateTime to Star Insure standard format - dd/mm/yyyy hh:mm
15
+ */
16
+ export declare function formatDateTime(dateTime: Date | string | null | undefined, overrideFormatString?: string): string;
@@ -25,7 +25,7 @@ export declare function gstCalc(input: number | string): {
25
25
  /**
26
26
  * Formats a value nicely
27
27
  */
28
- export declare function formatMoney(value: string | number, decimals?: number): string;
28
+ export declare function formatMoney(value: string | number | null | undefined, decimals?: number): string;
29
29
  /**
30
30
  * Turns a formatted value in to a float
31
31
  */
@@ -17,13 +17,6 @@ var reactTooltip = require('react-tooltip');
17
17
  var Select = _interopDefault(require('react-select'));
18
18
  var core = require('@inertiajs/core');
19
19
 
20
- function parseDate(dateString) {
21
- // Make sure we've only got 10 characters
22
- return dateFns.parse(dateString.substring(0, 10), 'yyyy-MM-dd', new Date());
23
- }
24
- function parseDateTime(dateTimeString) {
25
- return dateFns.parse(dateTimeString, 'yyyy-MM-dd HH:mm:ss', new Date());
26
- }
27
20
  /**
28
21
  * Formats a date like "Jan 1, 2022 at 9:50am"
29
22
  */
@@ -34,11 +27,11 @@ function formatDateNice(date, includeTime) {
34
27
  }
35
28
 
36
29
  var formatString = includeTime ? "MMM d, yyyy 'at' h:mma" : 'MMM d, yyyy';
37
- var parsed = includeTime ? parseDateTime(date) : parseDate(date);
38
- return dateFns.format(parsed, formatString);
30
+ var formatFunc = includeTime ? formatDateTime : formatDate;
31
+ return formatFunc(date, formatString);
39
32
  }
40
33
  /**
41
- * Formats a date like "01/01/2022 9:50AM"
34
+ * Formats a date like "01/01/2022 09:50"
42
35
  */
43
36
 
44
37
  function formatDateForTable(date, includeTime) {
@@ -46,9 +39,91 @@ function formatDateForTable(date, includeTime) {
46
39
  includeTime = true;
47
40
  }
48
41
 
49
- var formatString = includeTime ? 'dd/MM/yyyy h:mma' : 'dd/MM/yyyy';
50
- var parsed = includeTime ? parseDateTime(date) : parseDate(date);
51
- return dateFns.format(parsed, formatString);
42
+ return includeTime ? formatDateTime(date) : formatDate(date);
43
+ }
44
+ /**
45
+ * Formats a date to Star Insure standard format - dd/mm/yyyy
46
+ */
47
+
48
+ function formatDate(date, overrideFormatString) {
49
+ // If date is undefined or null
50
+ if (!date) {
51
+ return '';
52
+ }
53
+
54
+ var formatString = overrideFormatString || 'dd/MM/yyyy'; // If date is a date object
55
+
56
+ if (date instanceof Date) {
57
+ return dateFns.format(date, formatString);
58
+ }
59
+ /*
60
+ * If date is a string, first attempt to parse it in non-standard formats
61
+ * Only parse the first 10 characters of the string
62
+ * dd/MM/yyyy, dd-MM-yyyy, dd.MM.yyyy
63
+ */
64
+
65
+
66
+ var nonStandardDate = date.substring(0, 10).split('-').join('/').split('.').join('/');
67
+ var parsedDate = dateFns.parse(nonStandardDate, 'dd/MM/yyyy', new Date());
68
+
69
+ if (parsedDate.toString() !== 'Invalid Date') {
70
+ return dateFns.format(parsedDate, formatString);
71
+ } // Then attempt to parse it natively
72
+
73
+
74
+ var nativelyParsedDate = new Date(date);
75
+
76
+ if (nativelyParsedDate.toString() !== 'Invalid Date') {
77
+ return dateFns.format(nativelyParsedDate, formatString);
78
+ }
79
+
80
+ return '';
81
+ }
82
+ /**
83
+ * Formats a dateTime to Star Insure standard format - dd/mm/yyyy hh:mm
84
+ */
85
+
86
+ function formatDateTime(dateTime, overrideFormatString) {
87
+ // If date is undefined or null
88
+ if (!dateTime) {
89
+ return '';
90
+ }
91
+
92
+ var formatString = overrideFormatString || 'dd/MM/yyyy HH:mm'; // If date is a date object
93
+
94
+ if (dateTime instanceof Date) {
95
+ return dateFns.format(dateTime, formatString);
96
+ }
97
+ /*
98
+ * If date is a string, first attempt to parse it in non-standard formats
99
+ * dd/MM/yyyy HH:mm, dd-MM-yyyy HH:mm, dd.MM.yyyy HH:mm,
100
+ * dd/MM/yyyy hh:mm a, dd-MM-yyyy hh:mm a, dd.MM.yyyy hh:mm a,
101
+ * dd/MM/yyyy HH:mm:ss, dd-MM-yyyy HH:mm:ss, dd.MM.yyyy HH:mm:ss,
102
+ * dd/MM/yyyy hh:mm:ss a, dd-MM-yyyy hh:mm:ss a, dd.MM.yyyy hh:mm:ss a
103
+ */
104
+
105
+
106
+ var nonStandardDateTime = dateTime // Remove commas between date and time
107
+ .split(',').join('') // Only parse the first 22 characters of the string
108
+ .substring(0, 22) // Replace dashes and dots with slashes
109
+ .split('-').join('/').split('.').join('/');
110
+ var is12Hour = ['AM', 'PM'].includes(nonStandardDateTime.slice(-2).toUpperCase());
111
+ var hasSeconds = nonStandardDateTime.split(':').length === 3;
112
+ var nonStandardFormat = ("dd/MM/yyyy " + (is12Hour ? 'hh' : 'HH') + ":mm" + (hasSeconds ? ':ss' : '') + " " + (is12Hour ? 'a' : '')).trim();
113
+ var parsedDateTime = dateFns.parse(nonStandardDateTime, nonStandardFormat, new Date());
114
+
115
+ if (parsedDateTime.toString() !== 'Invalid Date') {
116
+ return dateFns.format(parsedDateTime, formatString);
117
+ } // Then attempt to parse it natively
118
+
119
+
120
+ var nativelyParsedDateTime = new Date(dateTime);
121
+
122
+ if (nativelyParsedDateTime.toString() !== 'Invalid Date') {
123
+ return dateFns.format(nativelyParsedDateTime, formatString);
124
+ }
125
+
126
+ return '';
52
127
  }
53
128
 
54
129
  /**
@@ -105,6 +180,10 @@ function formatMoney(value, decimals) {
105
180
  decimals = 2;
106
181
  }
107
182
 
183
+ if (value === null || value === undefined) {
184
+ return '';
185
+ }
186
+
108
187
  var toFormat = value;
109
188
 
110
189
  if (typeof value === 'string') {
@@ -1449,7 +1528,7 @@ function Toasts() {
1449
1528
  toasts = _useToast.toasts;
1450
1529
 
1451
1530
  return React__default.createElement("div", {
1452
- className: "fixed bottom-4 right-4 z-[110] flex flex-col gap-6"
1531
+ className: "fixed bottom-4 right-4 z-[120] flex flex-col gap-6"
1453
1532
  }, toasts.map(function (toast) {
1454
1533
  return React__default.createElement(ToastItem, Object.assign({}, toast, {
1455
1534
  key: toast._id
@@ -1463,6 +1542,8 @@ function Modal(_ref) {
1463
1542
  isActive = _ref$isActive === void 0 ? false : _ref$isActive,
1464
1543
  onClose = _ref.onClose,
1465
1544
  title = _ref.title,
1545
+ _ref$width = _ref.width,
1546
+ width = _ref$width === void 0 ? '' : _ref$width,
1466
1547
  _ref$className = _ref.className,
1467
1548
  className = _ref$className === void 0 ? '' : _ref$className;
1468
1549
  return React__default.createElement(react$1.Transition.Root, {
@@ -1493,7 +1574,7 @@ function Modal(_ref) {
1493
1574
  leaveFrom: "opacity-100 translate-y-0 sm:scale-100",
1494
1575
  leaveTo: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
1495
1576
  }, React__default.createElement("div", {
1496
- className: className + " w-full align-start inline-block bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-3xl sm:p-6"
1577
+ className: className + " w-full align-start inline-block bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-7xl sm:p-6 " + width
1497
1578
  }, React__default.createElement("div", {
1498
1579
  className: "flex gap-4 mb-4 text-asphalt " + (title ? 'border-b-2 border-asphalt pb-4' : '')
1499
1580
  }, title && React__default.createElement("h3", {
@@ -1637,13 +1718,15 @@ function TableRow(_ref) {
1637
1718
  _ref$className = _ref.className,
1638
1719
  className = _ref$className === void 0 ? '' : _ref$className,
1639
1720
  _ref$onClick = _ref.onClick,
1640
- onClick = _ref$onClick === void 0 ? function () {} : _ref$onClick,
1721
+ _onClick = _ref$onClick === void 0 ? function () {} : _ref$onClick,
1641
1722
  props = _objectWithoutPropertiesLoose(_ref, _excluded$5);
1642
1723
 
1643
1724
  var bgClass = className.includes('bg') ? '' : 'bg-white even:bg-gray-50';
1644
1725
  return React__default.createElement("tr", Object.assign({}, props, {
1645
1726
  className: cn(className, bgClass, 'hover:bg-gray-100'),
1646
- onClick: onClick
1727
+ onClick: function onClick(e) {
1728
+ return _onClick(e);
1729
+ }
1647
1730
  }), children);
1648
1731
  }
1649
1732
 
@@ -3081,16 +3164,16 @@ exports.calcMonthlyEnhancementPrice = calcMonthlyEnhancementPrice;
3081
3164
  exports.calcPurchaseOptionPricing = calcPurchaseOptionPricing;
3082
3165
  exports.calculateAge = calculateAge;
3083
3166
  exports.fixRoundingError = fixRoundingError;
3167
+ exports.formatDate = formatDate;
3084
3168
  exports.formatDateForTable = formatDateForTable;
3085
3169
  exports.formatDateNice = formatDateNice;
3170
+ exports.formatDateTime = formatDateTime;
3086
3171
  exports.formatMoney = formatMoney;
3087
3172
  exports.formatNumber = formatNumber;
3088
3173
  exports.getAddressData = getAddressData;
3089
3174
  exports.getGst = getGst;
3090
3175
  exports.gstCalc = gstCalc;
3091
3176
  exports.initialData = initialData;
3092
- exports.parseDate = parseDate;
3093
- exports.parseDateTime = parseDateTime;
3094
3177
  exports.round = round;
3095
3178
  exports.sanitiseQuoteRequestFormData = sanitiseQuoteRequestFormData;
3096
3179
  exports.sanitiseVehicleType = sanitiseVehicleType;