@becollective/utils 1.10.2 → 2.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/.semaphore/utils.yml +1 -1
- package/index.ts +7 -0
- package/lib/FeatureFlag.d.ts +12 -0
- package/lib/FeatureFlag.js +51 -0
- package/lib/constants/allowedHtmlTags.d.ts +7 -0
- package/lib/constants/allowedHtmlTags.js +8 -0
- package/lib/constants/avatarColours.d.ts +35 -0
- package/lib/constants/avatarColours.js +36 -0
- package/lib/constants/countries.d.ts +253 -0
- package/lib/constants/countries.js +254 -0
- package/lib/constants/currencies.d.ts +29 -0
- package/lib/constants/currencies.js +36 -0
- package/lib/constants/customFields.d.ts +11 -0
- package/lib/constants/customFields.js +12 -0
- package/lib/constants/distancesUnits.d.ts +11 -0
- package/lib/constants/distancesUnits.js +13 -0
- package/lib/constants/groups.d.ts +13 -0
- package/lib/constants/groups.js +85 -0
- package/lib/constants/insuranceStatuses.d.ts +15 -0
- package/lib/constants/insuranceStatuses.js +7 -0
- package/lib/constants/insuranceTypes.d.ts +35 -0
- package/lib/constants/insuranceTypes.js +36 -0
- package/lib/constants/languages.d.ts +2 -0
- package/lib/constants/languages.js +194 -0
- package/lib/constants/locales.d.ts +119 -0
- package/lib/constants/locales.js +191 -0
- package/lib/constants/notifications.d.ts +47 -0
- package/lib/constants/notifications.js +31 -0
- package/lib/constants/opportunities.d.ts +72 -0
- package/lib/constants/opportunities.js +82 -0
- package/lib/constants/qualificationTypes.d.ts +4 -0
- package/lib/constants/qualificationTypes.js +18 -0
- package/lib/constants/rules.d.ts +13 -0
- package/lib/constants/rules.js +21 -0
- package/lib/constants/subscriptions.d.ts +9 -0
- package/lib/constants/subscriptions.js +10 -0
- package/lib/constants/surveys.d.ts +16 -0
- package/lib/constants/surveys.js +17 -0
- package/lib/constants.d.ts +1 -0
- package/lib/constants.js +76 -0
- package/lib/date-time.d.ts +28 -0
- package/lib/date-time.js +79 -0
- package/lib/forms.d.ts +1 -0
- package/lib/forms.js +728 -0
- package/lib/locality.d.ts +5 -0
- package/lib/locality.js +15 -0
- package/lib/money.d.ts +7 -0
- package/lib/money.js +32 -0
- package/lib/opportunity.d.ts +1 -0
- package/lib/opportunity.js +11 -0
- package/lib/opportunityUser.d.ts +8 -0
- package/lib/opportunityUser.js +29 -0
- package/lib/password.d.ts +8 -0
- package/lib/password.js +43 -0
- package/lib/rrule/date.d.ts +4 -0
- package/lib/rrule/date.js +30 -0
- package/lib/rrule/index.d.ts +2 -0
- package/lib/rrule/index.js +18 -0
- package/lib/rrule/parsing.d.ts +6 -0
- package/lib/rrule/parsing.js +130 -0
- package/lib/rrule/parsing.test.d.ts +1 -0
- package/lib/rrule/parsing.test.js +11 -0
- package/lib/rrule.d.ts +49 -0
- package/lib/rrule.js +303 -0
- package/lib/src/FeatureFlag.d.ts +12 -0
- package/lib/src/FeatureFlag.js +51 -0
- package/lib/src/date-time.d.ts +28 -0
- package/lib/src/date-time.js +79 -0
- package/lib/src/forms.d.ts +1 -0
- package/lib/src/forms.js +728 -0
- package/lib/src/locality.d.ts +5 -0
- package/lib/src/locality.js +15 -0
- package/lib/src/money.d.ts +7 -0
- package/lib/src/money.js +32 -0
- package/lib/src/opportunity.d.ts +1 -0
- package/lib/src/opportunity.js +11 -0
- package/lib/src/opportunityUser.d.ts +8 -0
- package/lib/src/opportunityUser.js +29 -0
- package/lib/src/password.d.ts +8 -0
- package/lib/src/password.js +43 -0
- package/package.json +15 -29
- package/src/FeatureFlag.ts +26 -10
- package/src/constants/allowedHtmlTags.ts +6 -0
- package/src/constants/avatarColours.ts +34 -0
- package/src/constants/countries.ts +252 -0
- package/src/constants/currencies.ts +33 -0
- package/src/constants/customFields.ts +10 -0
- package/src/constants/distancesUnits.ts +11 -0
- package/src/constants/groups.ts +83 -0
- package/src/constants/insuranceStatuses.ts +5 -0
- package/src/constants/insuranceTypes.ts +34 -0
- package/src/constants/languages.ts +192 -0
- package/src/constants/locales.ts +189 -0
- package/src/constants/notifications.ts +30 -0
- package/src/constants/opportunities.ts +84 -0
- package/src/constants/qualificationTypes.ts +17 -0
- package/src/constants/rules.ts +22 -0
- package/src/constants/subscriptions.ts +8 -0
- package/src/constants/surveys.ts +15 -0
- package/src/constants.ts +40 -0
- package/src/{date-time.js → date-time.ts} +18 -11
- package/src/forms.ts +807 -0
- package/src/{opportunityUser.js → opportunityUser.ts} +5 -1
- package/src/{password.js → password.ts} +2 -0
- package/src/rrule/date.ts +25 -0
- package/src/rrule/index.ts +2 -0
- package/src/rrule/parsing.ts +136 -0
- package/src/rrule.ts +367 -0
- package/tests/{date-time.test.js → date-time.test.ts} +48 -46
- package/tests/featureFlag.test.ts +36 -39
- package/tests/forms.test-data.json +43 -0
- package/tests/forms.test-schema.json +144 -0
- package/tests/forms.test-uischema.json +117 -0
- package/tests/forms.test.ts +674 -0
- package/tests/{money.test.js → money.test.ts} +2 -3
- package/tests/{password.test.js → password.test.ts} +7 -6
- package/tests/rrule/parsing.test.ts +12 -0
- package/tests/rrule.test.ts +625 -0
- package/tsconfig.json +15 -12
- package/bundle.js +0 -270
- package/index.js +0 -25
- package/rollup.config.js +0 -25
- /package/src/{locality.js → locality.ts} +0 -0
- /package/src/{money.js → money.ts} +0 -0
- /package/src/{opportunity.js → opportunity.ts} +0 -0
package/bundle.js
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var axios = require('./node_modules/axios/index.js');
|
|
4
|
-
var lodash_js = require('./node_modules/lodash/lodash.js');
|
|
5
|
-
|
|
6
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
-
|
|
8
|
-
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
|
|
9
|
-
|
|
10
|
-
var password = {
|
|
11
|
-
hasUppercase: function hasUppercase(input) {
|
|
12
|
-
return !!input.match(/[A-Z]/);
|
|
13
|
-
},
|
|
14
|
-
hasLowerCase: function hasLowerCase(input) {
|
|
15
|
-
return !!input.match(/[a-z]/);
|
|
16
|
-
},
|
|
17
|
-
hasNumeral: function hasNumeral(input) {
|
|
18
|
-
return !!input.match(/[0-9]/);
|
|
19
|
-
},
|
|
20
|
-
validate: function validate(input) {
|
|
21
|
-
if (typeof input !== 'string') {
|
|
22
|
-
throw new Error('not-string');
|
|
23
|
-
} else if (input.length < 12) {
|
|
24
|
-
throw new Error('invalid-length');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var rules = 0;
|
|
28
|
-
if (password.hasUppercase(input)) rules++;
|
|
29
|
-
if (password.hasLowerCase(input)) rules++;
|
|
30
|
-
if (password.hasNumeral(input)) rules++;
|
|
31
|
-
|
|
32
|
-
if (rules < 3) {
|
|
33
|
-
throw new Error('invalid-minimum-rules');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return true;
|
|
37
|
-
},
|
|
38
|
-
isValid: function isValid(input) {
|
|
39
|
-
try {
|
|
40
|
-
password.validate(input);
|
|
41
|
-
return true;
|
|
42
|
-
} catch (e) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
var moment = require('moment-timezone');
|
|
49
|
-
/**
|
|
50
|
-
* Get current age based on the given birth date
|
|
51
|
-
* @param {string} dateOfBirth
|
|
52
|
-
* @param {string} unit the measurement of the the difference
|
|
53
|
-
* The supported measurements are:
|
|
54
|
-
* years, months, weeks, days, hours, minutes, and seconds.
|
|
55
|
-
* @returns {number} age
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var getAge = function getAge(dateOfBirth) {
|
|
60
|
-
var unit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'years';
|
|
61
|
-
var age = moment().diff(dateOfBirth, unit);
|
|
62
|
-
|
|
63
|
-
if (isNaN(age)) {
|
|
64
|
-
throw new Error('Invalid date');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return age;
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Check if a user requires parent(guardian) consent
|
|
71
|
-
* i.e. Is under the age of 16 years, based on GDPR
|
|
72
|
-
* https://gdpr-info.eu/art-8-gdpr/
|
|
73
|
-
* @param {string} dateOfBirth moment.js compatible date time
|
|
74
|
-
* @returns {boolean}
|
|
75
|
-
* NOTE:
|
|
76
|
-
* We used to not allow child (under 16) to register,
|
|
77
|
-
* and birthdate is not a required field for ghost user
|
|
78
|
-
* So an existing user could have no birthdate, we assume these users are adults
|
|
79
|
-
* TODO:
|
|
80
|
-
* Remove above logic after we migrated all legacy
|
|
81
|
-
* data to have a consistent birthdate property.
|
|
82
|
-
* Then we should throw error if birthdate is not valid
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
var isUnderSixteen = function isUnderSixteen(dateOfBirth) {
|
|
86
|
-
if (!dateOfBirth || !moment(dateOfBirth).isValid()) {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
var age = getAge(dateOfBirth);
|
|
91
|
-
return isNaN(age) ? false : age < 16;
|
|
92
|
-
};
|
|
93
|
-
var isUnderAge = function isUnderAge(dateOfBirth, limit, maxAcknowledgedAge) {
|
|
94
|
-
if ((!dateOfBirth || !moment(dateOfBirth).isValid()) && !maxAcknowledgedAge) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (dateOfBirth) {
|
|
99
|
-
var age = getAge(dateOfBirth);
|
|
100
|
-
return isNaN(age) ? true : age < limit;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return maxAcknowledgedAge ? maxAcknowledgedAge < limit : true;
|
|
104
|
-
};
|
|
105
|
-
var datesByThemselves = function datesByThemselves(a, b) {
|
|
106
|
-
if (!a.from) return 1;
|
|
107
|
-
return moment(a.from) - moment(b.from);
|
|
108
|
-
};
|
|
109
|
-
var getShiftText = function getShiftText(from, to, timezone) {
|
|
110
|
-
try {
|
|
111
|
-
if (!from || !to || !timezone) return '';
|
|
112
|
-
var startToTimezone = moment(from).tz(timezone);
|
|
113
|
-
var endToTimezone = moment(to).tz(timezone);
|
|
114
|
-
return "".concat(moment(startToTimezone).format('dddd, DD MMMM YYYY, h:mma'), "-").concat(moment(endToTimezone).format('h:mma'));
|
|
115
|
-
} catch (e) {
|
|
116
|
-
return '';
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
var _require$2 = require('@becollective/constants'),
|
|
121
|
-
currencies = _require$2.currencies; // We should probably throw and error on bad input instead of falling back to a default.
|
|
122
|
-
// We're making an assumption that whoever is calling this function can safely work
|
|
123
|
-
// with AUD. But this assumption removes a lot of error handling elsewhere.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
var getCurrencyFromCurrencyCode = function getCurrencyFromCurrencyCode(currencyCode) {
|
|
127
|
-
if (typeof currencyCode !== 'string' || !currencies[currencyCode]) {
|
|
128
|
-
console.error(new Error("Invalid currencyCode: ".concat(currencyCode, ". Falling back to 'AUD'.")));
|
|
129
|
-
return currencies.AUD;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return currencies[currencyCode];
|
|
133
|
-
};
|
|
134
|
-
var makeMoneyString = function makeMoneyString(_ref) {
|
|
135
|
-
var inputAmount = _ref.amount,
|
|
136
|
-
currencyCode = _ref.currencyCode,
|
|
137
|
-
_ref$rounded = _ref.rounded,
|
|
138
|
-
rounded = _ref$rounded === void 0 ? false : _ref$rounded,
|
|
139
|
-
_ref$showCurrencyCode = _ref.showCurrencyCode,
|
|
140
|
-
showCurrencyCode = _ref$showCurrencyCode === void 0 ? false : _ref$showCurrencyCode;
|
|
141
|
-
var amount = Number(inputAmount);
|
|
142
|
-
|
|
143
|
-
if (isNaN(amount)) {
|
|
144
|
-
console.error(new Error("Invalid amount: ".concat(inputAmount, ". Falling back to 0.")));
|
|
145
|
-
amount = 0;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
var currency = getCurrencyFromCurrencyCode(currencyCode);
|
|
149
|
-
var minorUnit = currency.minorUnit,
|
|
150
|
-
symbol = currency.symbol;
|
|
151
|
-
var decimalPlaces = rounded ? 0 : minorUnit;
|
|
152
|
-
var localeAmount = amount.toLocaleString(undefined, {
|
|
153
|
-
minimumFractionDigits: decimalPlaces,
|
|
154
|
-
maximumFractionDigits: decimalPlaces
|
|
155
|
-
});
|
|
156
|
-
var currencyCodeString = showCurrencyCode ? " ".concat(currency.code) : '';
|
|
157
|
-
return "".concat(symbol).concat(localeAmount).concat(currencyCodeString);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
function _defineProperty(obj, key, value) {
|
|
161
|
-
if (key in obj) {
|
|
162
|
-
Object.defineProperty(obj, key, {
|
|
163
|
-
value: value,
|
|
164
|
-
enumerable: true,
|
|
165
|
-
configurable: true,
|
|
166
|
-
writable: true
|
|
167
|
-
});
|
|
168
|
-
} else {
|
|
169
|
-
obj[key] = value;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return obj;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
var _TYPE_MAP;
|
|
176
|
-
|
|
177
|
-
var _require$1 = require('@becollective/constants'),
|
|
178
|
-
opportunities = _require$1.opportunities;
|
|
179
|
-
|
|
180
|
-
var opportunityTypes = opportunities.typesMapV2;
|
|
181
|
-
var TYPE_MAP = (_TYPE_MAP = {}, _defineProperty(_TYPE_MAP, opportunityTypes.shift, 'Shifts'), _defineProperty(_TYPE_MAP, opportunityTypes.flexible, 'Flexible'), _TYPE_MAP);
|
|
182
|
-
var readableOpportunityType = function readableOpportunityType(type) {
|
|
183
|
-
return TYPE_MAP[type] || null;
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
var _require = require('lodash'),
|
|
187
|
-
pick = _require.pick;
|
|
188
|
-
/**
|
|
189
|
-
*
|
|
190
|
-
* @param {*} opportunityUser
|
|
191
|
-
* @param {*} opportunity
|
|
192
|
-
*/
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
var getTimeInfo = function getTimeInfo(opportunityUser) {
|
|
196
|
-
var opportunity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
197
|
-
var opp = opportunity || opportunityUser.opportunity;
|
|
198
|
-
var isEoi = opportunityUser && opportunityUser.eoi || opp && opp.eoi;
|
|
199
|
-
var record = isEoi ? opportunityUser : opp;
|
|
200
|
-
var fields = pick(record, ['startDate', 'endDate', 'type', 'dates', 'recurrenceRule', 'timezone', 'location', 'locationOther', 'locationVirtual']);
|
|
201
|
-
return fields;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
*
|
|
206
|
-
* @param {Array} locationList
|
|
207
|
-
*/
|
|
208
|
-
var getHomeLocalityFromLocationList = function getHomeLocalityFromLocationList(locationList) {
|
|
209
|
-
var homeLocation = Array.isArray(locationList) && locationList.find(function (location) {
|
|
210
|
-
return location.name === 'Home';
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
if (homeLocation && homeLocation.locality && homeLocation.locality.long) {
|
|
214
|
-
return homeLocation.locality.long;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return null;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
const fetchFeatureActive = async (feature, options) => {
|
|
221
|
-
const getFlagByFeatureUrl = `https://lb-central.becollective.com/api/v2/feature-flags/${feature}`;
|
|
222
|
-
let isFeatureActive = false;
|
|
223
|
-
const featureFlags = await axios__default['default'].get(getFlagByFeatureUrl);
|
|
224
|
-
const environments = lodash_js.get(featureFlags, 'data.Environments');
|
|
225
|
-
if (environments) {
|
|
226
|
-
isFeatureActive = environments.includes(options.env);
|
|
227
|
-
}
|
|
228
|
-
return isFeatureActive;
|
|
229
|
-
};
|
|
230
|
-
const memoizedFeatureActive = lodash_js.memoize(fetchFeatureActive, (feature, options) => JSON.stringify({ feature, options }));
|
|
231
|
-
class FeatureFlag {
|
|
232
|
-
constructor(ttl) {
|
|
233
|
-
this.ttl = ttl || 1000 * 60 * 5; // default to 5 minutes
|
|
234
|
-
this.expiry = Date.now() + ttl;
|
|
235
|
-
}
|
|
236
|
-
async isFeatureActive(feature, options) {
|
|
237
|
-
// Lazy expiration upon function call
|
|
238
|
-
const now = Date.now();
|
|
239
|
-
if (now >= this.expiry) {
|
|
240
|
-
memoizedFeatureActive.cache.clear();
|
|
241
|
-
this.expiry = now + this.ttl;
|
|
242
|
-
}
|
|
243
|
-
return await memoizedFeatureActive(feature, options);
|
|
244
|
-
}
|
|
245
|
-
clearCache() {
|
|
246
|
-
memoizedFeatureActive.cache.clear();
|
|
247
|
-
}
|
|
248
|
-
setCacheTtl(ttl) {
|
|
249
|
-
this.ttl = ttl;
|
|
250
|
-
this.expiry = Date.now() + ttl;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
var util = {
|
|
255
|
-
getAge: getAge,
|
|
256
|
-
getCurrencyFromCurrencyCode: getCurrencyFromCurrencyCode,
|
|
257
|
-
isUnderAge: isUnderAge,
|
|
258
|
-
isUnderSixteen: isUnderSixteen,
|
|
259
|
-
makeMoneyString: makeMoneyString,
|
|
260
|
-
password: password,
|
|
261
|
-
sort: {
|
|
262
|
-
datesByThemselves: datesByThemselves
|
|
263
|
-
},
|
|
264
|
-
readableOpportunityType: readableOpportunityType,
|
|
265
|
-
getTimeInfo: getTimeInfo,
|
|
266
|
-
getHomeLocalityFromLocationList: getHomeLocalityFromLocationList,
|
|
267
|
-
getShiftText: getShiftText,
|
|
268
|
-
FeatureFlag: FeatureFlag
|
|
269
|
-
};
|
|
270
|
-
module.exports = util;
|
package/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { password } from './src/password';
|
|
2
|
-
import { isUnderSixteen, isUnderAge, getAge, datesByThemselves, getShiftText } from './src/date-time';
|
|
3
|
-
import { getCurrencyFromCurrencyCode, makeMoneyString } from './src/money';
|
|
4
|
-
import { readableOpportunityType } from './src/opportunity';
|
|
5
|
-
import { getTimeInfo } from './src/opportunityUser';
|
|
6
|
-
import { getHomeLocalityFromLocationList } from './src/locality';
|
|
7
|
-
import FeatureFlag from './src/FeatureFlag';
|
|
8
|
-
const util = {
|
|
9
|
-
getAge,
|
|
10
|
-
getCurrencyFromCurrencyCode,
|
|
11
|
-
isUnderAge,
|
|
12
|
-
isUnderSixteen,
|
|
13
|
-
makeMoneyString,
|
|
14
|
-
password,
|
|
15
|
-
sort: {
|
|
16
|
-
datesByThemselves,
|
|
17
|
-
},
|
|
18
|
-
readableOpportunityType,
|
|
19
|
-
getTimeInfo,
|
|
20
|
-
getHomeLocalityFromLocationList,
|
|
21
|
-
getShiftText,
|
|
22
|
-
FeatureFlag,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
module.exports = util;
|
package/rollup.config.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import resolve from 'rollup-plugin-node-resolve';
|
|
2
|
-
import babel from 'rollup-plugin-babel';
|
|
3
|
-
import typescript from '@rollup/plugin-typescript';
|
|
4
|
-
import commonjs from '@rollup/plugin-commonjs';
|
|
5
|
-
import json from '@rollup/plugin-json';
|
|
6
|
-
|
|
7
|
-
export default {
|
|
8
|
-
input: 'index.js',
|
|
9
|
-
output: {
|
|
10
|
-
file: 'bundle.js',
|
|
11
|
-
format: 'cjs',
|
|
12
|
-
},
|
|
13
|
-
external:[/node_modules/],
|
|
14
|
-
plugins: [
|
|
15
|
-
json(),
|
|
16
|
-
commonjs(),
|
|
17
|
-
typescript(),
|
|
18
|
-
resolve(),
|
|
19
|
-
babel({
|
|
20
|
-
exclude: 'node_modules/**', // only transpile our source code
|
|
21
|
-
babelrc: false,
|
|
22
|
-
presets: ['@babel/preset-env'],
|
|
23
|
-
}),
|
|
24
|
-
],
|
|
25
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|