@becollective/utils 2.0.15 → 2.0.16
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/lib/date-time.js +5 -2
- package/package.json +1 -1
- package/src/date-time.ts +4 -2
- package/tests/date-time.test.ts +5 -0
package/lib/date-time.js
CHANGED
|
@@ -68,8 +68,11 @@ const getShiftText = (from, to, timezone) => {
|
|
|
68
68
|
try {
|
|
69
69
|
if (!from || !to || !timezone)
|
|
70
70
|
return '';
|
|
71
|
-
|
|
72
|
-
const
|
|
71
|
+
// GraphQL / JSON often yield ISO strings; fromJSDate alone rejects those as Invalid.
|
|
72
|
+
const startToTimezone = luxon_1.DateTime.fromJSDate(new Date(from)).setZone(timezone);
|
|
73
|
+
const endToTimezone = luxon_1.DateTime.fromJSDate(new Date(to)).setZone(timezone);
|
|
74
|
+
if (!startToTimezone.isValid || !endToTimezone.isValid)
|
|
75
|
+
return '';
|
|
73
76
|
return `${startToTimezone.toFormat('cccc, d MMMM yyyy, h:mma')}-${endToTimezone.toFormat('h:mma')}`;
|
|
74
77
|
}
|
|
75
78
|
catch (e) {
|
package/package.json
CHANGED
package/src/date-time.ts
CHANGED
|
@@ -65,8 +65,10 @@ export const datesByThemselves = (a, b) => {
|
|
|
65
65
|
export const getShiftText = (from, to, timezone) => {
|
|
66
66
|
try {
|
|
67
67
|
if (!from || !to || !timezone) return '';
|
|
68
|
-
|
|
69
|
-
const
|
|
68
|
+
// GraphQL / JSON often yield ISO strings; fromJSDate alone rejects those as Invalid.
|
|
69
|
+
const startToTimezone = DateTime.fromJSDate(new Date(from)).setZone(timezone);
|
|
70
|
+
const endToTimezone = DateTime.fromJSDate(new Date(to)).setZone(timezone);
|
|
71
|
+
if (!startToTimezone.isValid || !endToTimezone.isValid) return '';
|
|
70
72
|
return `${startToTimezone.toFormat('cccc, d MMMM yyyy, h:mma')}-${endToTimezone.toFormat('h:mma')}`;
|
|
71
73
|
}
|
|
72
74
|
catch (e) {
|
package/tests/date-time.test.ts
CHANGED
|
@@ -150,6 +150,11 @@ describe('getShiftText', () => {
|
|
|
150
150
|
test('get shift text with auckland timezone', () => {
|
|
151
151
|
expect(getShiftText(from, to, auckland)).toBe('Thursday, 30 July 2020, 12:00PM-5:30PM');
|
|
152
152
|
});
|
|
153
|
+
test('accepts ISO strings from GraphQL', () => {
|
|
154
|
+
expect(getShiftText(from.toISOString(), to.toISOString(), melbourne)).toBe(
|
|
155
|
+
'Thursday, 30 July 2020, 10:00AM-3:30PM',
|
|
156
|
+
);
|
|
157
|
+
});
|
|
153
158
|
// Will not execute due to TypeScript
|
|
154
159
|
// test('with empty data', () => {
|
|
155
160
|
// expect(getShiftText()).toBe('');
|