@star-insure/sdk 3.3.0 → 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 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
+ ```
@@ -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,11 @@ 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 formatMoneyNumerals(value: string | number, decimals?: number): string;
29
+ /**
30
+ * Formats a value nicely
31
+ */
32
+ export declare function formatMoney(value: string | number | null | undefined, decimals?: number): string;
29
33
  /**
30
34
  * Turns a formatted value in to a float
31
35
  */
@@ -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
  /**
@@ -100,7 +175,7 @@ function gstCalc(input) {
100
175
  * Formats a value nicely
101
176
  */
102
177
 
103
- function formatMoney(value, decimals) {
178
+ function formatMoneyNumerals(value, decimals) {
104
179
  if (decimals === void 0) {
105
180
  decimals = 2;
106
181
  }
@@ -116,6 +191,22 @@ function formatMoney(value, decimals) {
116
191
  maximumFractionDigits: decimals
117
192
  });
118
193
  }
194
+ /**
195
+ * Formats a value nicely
196
+ */
197
+
198
+ function formatMoney(value, decimals) {
199
+ if (decimals === void 0) {
200
+ decimals = 2;
201
+ }
202
+
203
+ if (value === null || value === undefined) {
204
+ return '';
205
+ }
206
+
207
+ return ("$" + formatMoneyNumerals(value, decimals) // If the value is negative, we need to move the negative sign to the front
208
+ ).replace('$-', '- $');
209
+ }
119
210
  /**
120
211
  * Turns a formatted value in to a float
121
212
  */
@@ -1449,7 +1540,7 @@ function Toasts() {
1449
1540
  toasts = _useToast.toasts;
1450
1541
 
1451
1542
  return React__default.createElement("div", {
1452
- className: "fixed bottom-4 right-4 z-[110] flex flex-col gap-6"
1543
+ className: "fixed bottom-4 right-4 z-[120] flex flex-col gap-6"
1453
1544
  }, toasts.map(function (toast) {
1454
1545
  return React__default.createElement(ToastItem, Object.assign({}, toast, {
1455
1546
  key: toast._id
@@ -1637,13 +1728,15 @@ function TableRow(_ref) {
1637
1728
  _ref$className = _ref.className,
1638
1729
  className = _ref$className === void 0 ? '' : _ref$className,
1639
1730
  _ref$onClick = _ref.onClick,
1640
- onClick = _ref$onClick === void 0 ? function () {} : _ref$onClick,
1731
+ _onClick = _ref$onClick === void 0 ? function () {} : _ref$onClick,
1641
1732
  props = _objectWithoutPropertiesLoose(_ref, _excluded$5);
1642
1733
 
1643
1734
  var bgClass = className.includes('bg') ? '' : 'bg-white even:bg-gray-50';
1644
1735
  return React__default.createElement("tr", Object.assign({}, props, {
1645
1736
  className: cn(className, bgClass, 'hover:bg-gray-100'),
1646
- onClick: onClick
1737
+ onClick: function onClick(e) {
1738
+ return _onClick(e);
1739
+ }
1647
1740
  }), children);
1648
1741
  }
1649
1742
 
@@ -2750,7 +2843,7 @@ function FilterItem(_ref) {
2750
2843
  className: "whitespace-nowrap"
2751
2844
  }, filter.label), React__default.createElement(hi2.HiChevronDown, null))
2752
2845
  }, React__default.createElement("form", {
2753
- className: "mt-2 flex max-h-[350px] min-w-[200px] max-w-[220px] flex-col gap-2 rounded-md border border-gray-300 bg-white p-4 shadow-lg " + (filter.type && filter.type === 'select' ? '' : 'overflow-y-scroll'),
2846
+ className: "mt-2 flex max-h-[350px] min-w-[200px] max-w-[260px] flex-col gap-2 rounded-md border border-gray-300 bg-white p-4 shadow-lg " + (filter.type && filter.type === 'select' ? '' : 'overflow-y-scroll'),
2754
2847
  onSubmit: handleApply
2755
2848
  }, React__default.createElement("div", {
2756
2849
  className: "flex flex-col items-start gap-1"
@@ -2824,7 +2917,7 @@ function FilterItem(_ref) {
2824
2917
  }, React__default.createElement(Select, {
2825
2918
  isMulti: true,
2826
2919
  options: filter.options,
2827
- className: "basic-multi-select text-sm w-64 !transition-none",
2920
+ className: "basic-multi-select text-xs w-52 !transition-none",
2828
2921
  classNamePrefix: "select",
2829
2922
  onChange: handleSelectedOptions,
2830
2923
  components: {
@@ -3081,16 +3174,17 @@ exports.calcMonthlyEnhancementPrice = calcMonthlyEnhancementPrice;
3081
3174
  exports.calcPurchaseOptionPricing = calcPurchaseOptionPricing;
3082
3175
  exports.calculateAge = calculateAge;
3083
3176
  exports.fixRoundingError = fixRoundingError;
3177
+ exports.formatDate = formatDate;
3084
3178
  exports.formatDateForTable = formatDateForTable;
3085
3179
  exports.formatDateNice = formatDateNice;
3180
+ exports.formatDateTime = formatDateTime;
3086
3181
  exports.formatMoney = formatMoney;
3182
+ exports.formatMoneyNumerals = formatMoneyNumerals;
3087
3183
  exports.formatNumber = formatNumber;
3088
3184
  exports.getAddressData = getAddressData;
3089
3185
  exports.getGst = getGst;
3090
3186
  exports.gstCalc = gstCalc;
3091
3187
  exports.initialData = initialData;
3092
- exports.parseDate = parseDate;
3093
- exports.parseDateTime = parseDateTime;
3094
3188
  exports.round = round;
3095
3189
  exports.sanitiseQuoteRequestFormData = sanitiseQuoteRequestFormData;
3096
3190
  exports.sanitiseVehicleType = sanitiseVehicleType;