@becollective/utils 1.5.1 → 1.5.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/bundle.js CHANGED
@@ -38,7 +38,7 @@ var password = {
38
38
  }
39
39
  };
40
40
 
41
- var moment = require('moment');
41
+ var moment = require('moment-timezone');
42
42
  /**
43
43
  * Get current age based on the given birth date
44
44
  * @param {string} dateOfBirth
@@ -87,6 +87,16 @@ var datesByThemselves = function datesByThemselves(a, b) {
87
87
  if (!a.from) return 1;
88
88
  return moment(a.from) - moment(b.from);
89
89
  };
90
+ var getShiftText = function getShiftText(from, to, timezone) {
91
+ try {
92
+ if (!from || !to || !timezone) return '';
93
+ var startToTimezone = moment(from).tz(timezone);
94
+ var endToTimezone = moment(to).tz(timezone);
95
+ return "".concat(moment(startToTimezone).format('dddd, DD MMMM YYYY, h:mma'), "-").concat(moment(endToTimezone).format('h:mma'));
96
+ } catch (e) {
97
+ return '';
98
+ }
99
+ };
90
100
 
91
101
  var _require = require('@becollective/constants'),
92
102
  currencies = _require.currencies; // We should probably throw and error on bad input instead of falling back to a default.
@@ -172,6 +182,22 @@ var getTimeInfo = function getTimeInfo(opportunityUser) {
172
182
  return fields;
173
183
  };
174
184
 
185
+ /**
186
+ *
187
+ * @param {Array} locationList
188
+ */
189
+ var getHomeLocalityFromLocationList = function getHomeLocalityFromLocationList(locationList) {
190
+ var homeLocation = Array.isArray(locationList) && locationList.find(function (location) {
191
+ return location.name === 'Home';
192
+ });
193
+
194
+ if (homeLocation && homeLocation.locality && homeLocation.locality.long) {
195
+ return homeLocation.locality.long;
196
+ }
197
+
198
+ return null;
199
+ };
200
+
175
201
  var util = {
176
202
  getAge: getAge,
177
203
  getCurrencyFromCurrencyCode: getCurrencyFromCurrencyCode,
@@ -182,6 +208,8 @@ var util = {
182
208
  datesByThemselves: datesByThemselves
183
209
  },
184
210
  readableOpportunityType: readableOpportunityType,
185
- getTimeInfo: getTimeInfo
211
+ getTimeInfo: getTimeInfo,
212
+ getHomeLocalityFromLocationList: getHomeLocalityFromLocationList,
213
+ getShiftText: getShiftText
186
214
  };
187
215
  module.exports = util;
package/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { password } from './src/password';
2
- import { isUnderSixteen, getAge, datesByThemselves } from './src/date-time';
2
+ import { isUnderSixteen, getAge, datesByThemselves, getShiftText } from './src/date-time';
3
3
  import { getCurrencyFromCurrencyCode, makeMoneyString } from './src/money';
4
4
  import { readableOpportunityType } from './src/opportunity';
5
5
  import { getTimeInfo } from './src/opportunityUser';
6
+ import { getHomeLocalityFromLocationList } from './src/locality';
6
7
 
7
8
  const util = {
8
9
  getAge,
@@ -15,6 +16,8 @@ const util = {
15
16
  },
16
17
  readableOpportunityType,
17
18
  getTimeInfo,
19
+ getHomeLocalityFromLocationList,
20
+ getShiftText,
18
21
  };
19
22
 
20
23
  module.exports = util;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@becollective/utils",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
4
4
  "description": "Common utilities",
5
5
  "main": "bundle.js",
6
6
  "scripts": {
@@ -21,7 +21,8 @@
21
21
  "dependencies": {
22
22
  "@becollective/constants": "^3.6.3",
23
23
  "lodash": "^4.17.15",
24
- "moment": "^2.24.0"
24
+ "moment": "^2.24.0",
25
+ "moment-timezone": "^0.5.26"
25
26
  },
26
27
  "jest": {
27
28
  "testMatch": [
package/src/date-time.js CHANGED
@@ -1,4 +1,4 @@
1
- const moment = require('moment');
1
+ const moment = require('moment-timezone');
2
2
 
3
3
  /**
4
4
  * Get current age based on the given birth date
@@ -42,3 +42,14 @@ export const datesByThemselves = (a, b) => {
42
42
  if (!a.from) return 1;
43
43
  return moment(a.from) - moment(b.from);
44
44
  };
45
+
46
+ export const getShiftText = (from, to, timezone) => {
47
+ try {
48
+ if (!from || !to || !timezone) return '';
49
+ const startToTimezone = moment(from).tz(timezone);
50
+ const endToTimezone = moment(to).tz(timezone);
51
+ return `${moment(startToTimezone).format('dddd, DD MMMM YYYY, h:mma')}-${moment(endToTimezone).format('h:mma')}`;
52
+ } catch (e) {
53
+ return '';
54
+ }
55
+ };
@@ -1,4 +1,4 @@
1
- const { isUnderSixteen, getAge } = require('../bundle.js');
1
+ const { isUnderSixteen, getAge, getShiftText } = require('../bundle.js');
2
2
  const moment = require('moment');
3
3
 
4
4
  describe('getAge', () => {
@@ -54,3 +54,19 @@ describe('isUnderSixteen', () => {
54
54
  expect(isUnderSixteen('not-valid')).toBe(false);
55
55
  });
56
56
  });
57
+
58
+ describe('getShiftText', () => {
59
+ const from = new Date(Date.UTC(2020, 6, 30, 0, 0, 0));
60
+ const to = new Date(Date.UTC(2020, 6, 30, 5, 30, 0));
61
+ const melbourne = 'Australia/Melbourne';
62
+ const auckland = 'Pacific/Auckland';
63
+ test('get shift text with melbourne timezone', () => {
64
+ expect(getShiftText(from, to,melbourne)).toBe("Thursday, 30 July 2020, 10:00am-3:30pm");
65
+ });
66
+ test('get shift text with auckland timezone', () => {
67
+ expect(getShiftText(from, to,auckland)).toBe("Thursday, 30 July 2020, 12:00pm-5:30pm");
68
+ });
69
+ test('with empty data', () => {
70
+ expect(getShiftText()).toBe('');
71
+ });
72
+ });