@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/lib/locality.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getHomeLocalityFromLocationList = void 0;
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param {Array} locationList
|
|
7
|
+
*/
|
|
8
|
+
const getHomeLocalityFromLocationList = (locationList) => {
|
|
9
|
+
const homeLocation = Array.isArray(locationList) && locationList.find((location) => location.name === 'Home');
|
|
10
|
+
if (homeLocation && homeLocation.locality && homeLocation.locality.long) {
|
|
11
|
+
return homeLocation.locality.long;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
};
|
|
15
|
+
exports.getHomeLocalityFromLocationList = getHomeLocalityFromLocationList;
|
package/lib/money.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const getCurrencyFromCurrencyCode: (currencyCode: any) => any;
|
|
2
|
+
export declare const makeMoneyString: ({ amount: inputAmount, currencyCode, rounded, showCurrencyCode }: {
|
|
3
|
+
amount: any;
|
|
4
|
+
currencyCode: any;
|
|
5
|
+
rounded?: boolean | undefined;
|
|
6
|
+
showCurrencyCode?: boolean | undefined;
|
|
7
|
+
}) => string;
|
package/lib/money.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeMoneyString = exports.getCurrencyFromCurrencyCode = void 0;
|
|
4
|
+
const { currencies } = require('@becollective/constants');
|
|
5
|
+
// We should probably throw and error on bad input instead of falling back to a default.
|
|
6
|
+
// We're making an assumption that whoever is calling this function can safely work
|
|
7
|
+
// with AUD. But this assumption removes a lot of error handling elsewhere.
|
|
8
|
+
const getCurrencyFromCurrencyCode = (currencyCode) => {
|
|
9
|
+
if (typeof currencyCode !== 'string' || !currencies[currencyCode]) {
|
|
10
|
+
console.error(new Error(`Invalid currencyCode: ${currencyCode}. Falling back to 'AUD'.`));
|
|
11
|
+
return currencies.AUD;
|
|
12
|
+
}
|
|
13
|
+
return currencies[currencyCode];
|
|
14
|
+
};
|
|
15
|
+
exports.getCurrencyFromCurrencyCode = getCurrencyFromCurrencyCode;
|
|
16
|
+
const makeMoneyString = ({ amount: inputAmount, currencyCode, rounded = false, showCurrencyCode = false }) => {
|
|
17
|
+
let amount = Number(inputAmount);
|
|
18
|
+
if (isNaN(amount)) {
|
|
19
|
+
console.error(new Error(`Invalid amount: ${inputAmount}. Falling back to 0.`));
|
|
20
|
+
amount = 0;
|
|
21
|
+
}
|
|
22
|
+
const currency = (0, exports.getCurrencyFromCurrencyCode)(currencyCode);
|
|
23
|
+
const { minorUnit, symbol } = currency;
|
|
24
|
+
const decimalPlaces = rounded ? 0 : minorUnit;
|
|
25
|
+
const localeAmount = amount.toLocaleString(undefined, {
|
|
26
|
+
minimumFractionDigits: decimalPlaces,
|
|
27
|
+
maximumFractionDigits: decimalPlaces,
|
|
28
|
+
});
|
|
29
|
+
const currencyCodeString = showCurrencyCode ? ` ${currency.code}` : '';
|
|
30
|
+
return `${symbol}${localeAmount}${currencyCodeString}`;
|
|
31
|
+
};
|
|
32
|
+
exports.makeMoneyString = makeMoneyString;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const readableOpportunityType: (type: any) => string | null;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readableOpportunityType = void 0;
|
|
4
|
+
const { opportunities } = require('@becollective/constants');
|
|
5
|
+
const { typesMapV2: opportunityTypes } = opportunities;
|
|
6
|
+
const TYPE_MAP = {
|
|
7
|
+
[opportunityTypes.shift]: 'Shifts',
|
|
8
|
+
[opportunityTypes.flexible]: 'Flexible',
|
|
9
|
+
};
|
|
10
|
+
const readableOpportunityType = (type) => TYPE_MAP[type] || null;
|
|
11
|
+
exports.readableOpportunityType = readableOpportunityType;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTimeInfo = void 0;
|
|
4
|
+
const pick = (obj, keysToPick = []) => {
|
|
5
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => keysToPick.includes(key)));
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {*} opportunityUser
|
|
10
|
+
* @param {*} opportunity
|
|
11
|
+
*/
|
|
12
|
+
const getTimeInfo = (opportunityUser, opportunity = null) => {
|
|
13
|
+
const opp = opportunity || opportunityUser.opportunity;
|
|
14
|
+
const isEoi = (opportunityUser && opportunityUser.eoi) || (opp && opp.eoi);
|
|
15
|
+
const record = isEoi ? opportunityUser : opp;
|
|
16
|
+
const fields = pick(record, [
|
|
17
|
+
'startDate',
|
|
18
|
+
'endDate',
|
|
19
|
+
'type',
|
|
20
|
+
'dates',
|
|
21
|
+
'recurrenceRule',
|
|
22
|
+
'timezone',
|
|
23
|
+
'location',
|
|
24
|
+
'locationOther',
|
|
25
|
+
'locationVirtual',
|
|
26
|
+
]);
|
|
27
|
+
return fields;
|
|
28
|
+
};
|
|
29
|
+
exports.getTimeInfo = getTimeInfo;
|
package/lib/password.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.password = void 0;
|
|
4
|
+
exports.password = {
|
|
5
|
+
hasUppercase: (input) => {
|
|
6
|
+
return !!input.match(/[A-Z]/);
|
|
7
|
+
},
|
|
8
|
+
hasLowerCase: (input) => {
|
|
9
|
+
return !!input.match(/[a-z]/);
|
|
10
|
+
},
|
|
11
|
+
hasNumeral: (input) => {
|
|
12
|
+
return !!input.match(/[0-9]/);
|
|
13
|
+
},
|
|
14
|
+
validate: (input) => {
|
|
15
|
+
if (typeof input !== 'string') {
|
|
16
|
+
throw new Error('not-string');
|
|
17
|
+
}
|
|
18
|
+
else if (input.length < 12) {
|
|
19
|
+
throw new Error('invalid-length');
|
|
20
|
+
}
|
|
21
|
+
let rules = 0;
|
|
22
|
+
if (exports.password.hasUppercase(input))
|
|
23
|
+
rules++;
|
|
24
|
+
if (exports.password.hasLowerCase(input))
|
|
25
|
+
rules++;
|
|
26
|
+
if (exports.password.hasNumeral(input))
|
|
27
|
+
rules++;
|
|
28
|
+
if (rules < 3) {
|
|
29
|
+
throw new Error('invalid-minimum-rules');
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
},
|
|
33
|
+
isValid: (input) => {
|
|
34
|
+
try {
|
|
35
|
+
exports.password.validate(input);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
exports.default = exports.password;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const getEndTime: (start: any, duration: any) => Date;
|
|
2
|
+
export declare const munge: (date: any, time: any) => string;
|
|
3
|
+
export declare const transpose: (date: any, time: any, timezone: any) => any;
|
|
4
|
+
export declare const getStartTime: (rule: any, timezone: any) => any;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getStartTime = exports.transpose = exports.munge = exports.getEndTime = void 0;
|
|
7
|
+
const luxon_1 = require("luxon");
|
|
8
|
+
const rrule_1 = __importDefault(require("rrule"));
|
|
9
|
+
const parsing_1 = require("./parsing");
|
|
10
|
+
// Given a start (Date object) and a duration in milliseconds, return a new Date object specifying the end time
|
|
11
|
+
const getEndTime = (start, duration) => new Date(start.getTime() + duration);
|
|
12
|
+
exports.getEndTime = getEndTime;
|
|
13
|
+
// Combine a date and time string in formats: YYYYMMDD, HHss
|
|
14
|
+
const munge = (date, time) => {
|
|
15
|
+
const datePart = luxon_1.DateTime.fromISO(date).toFormat('YYYYMMDD');
|
|
16
|
+
return `${datePart}${time}`;
|
|
17
|
+
};
|
|
18
|
+
exports.munge = munge;
|
|
19
|
+
// Return a date object representing the given date and time string in the timezone specified
|
|
20
|
+
const transpose = (date, time, timezone) => {
|
|
21
|
+
return luxon_1.DateTime.fromISO((0, exports.munge)(date, time)).setZone(timezone).toDate();
|
|
22
|
+
};
|
|
23
|
+
exports.transpose = transpose;
|
|
24
|
+
// Gets the start time (HHmm format) in the timezone specified for an rrule
|
|
25
|
+
const getStartTime = (rule, timezone) => {
|
|
26
|
+
// eslint-disable-next-line no-undef
|
|
27
|
+
const obj = typeof rule === 'object' ? rrule_1.default : (0, parsing_1.toRruleObj)(rule);
|
|
28
|
+
return luxon_1.DateTime.fromISO(obj.DTSTART).setZone(timezone).toFormat('HHmm');
|
|
29
|
+
};
|
|
30
|
+
exports.getStartTime = getStartTime;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./parsing"), exports);
|
|
18
|
+
__exportStar(require("./date"), exports);
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDuration = exports.parseDuration = exports.toRruleStr = exports.toRruleObj = void 0;
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
const omitBy = (obj, predicate) => {
|
|
6
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => !predicate(value, key)));
|
|
7
|
+
};
|
|
8
|
+
const toRruleObj = function toRruleObj(str) {
|
|
9
|
+
const config = (str || '')
|
|
10
|
+
.split(';')
|
|
11
|
+
.map((component) => component.split('=', 2))
|
|
12
|
+
.reduce((all, [k, v]) => {
|
|
13
|
+
all[k] = v;
|
|
14
|
+
return all;
|
|
15
|
+
}, {});
|
|
16
|
+
return omitBy(config, (value, key) => !key || value === null);
|
|
17
|
+
};
|
|
18
|
+
exports.toRruleObj = toRruleObj;
|
|
19
|
+
const toRruleStr = function toRruleStr(obj) {
|
|
20
|
+
const filter = Object.keys(obj).filter((k) => obj[k] !== null && obj[k] !== undefined);
|
|
21
|
+
const map = filter.map((key) => `${key}=${obj[key]}`);
|
|
22
|
+
const join = map.join(';');
|
|
23
|
+
return Object.keys(obj)
|
|
24
|
+
.filter((k) => obj[k] !== null && obj[k] !== undefined)
|
|
25
|
+
.map((key) => `${key}=${obj[key]}`)
|
|
26
|
+
.join(';');
|
|
27
|
+
};
|
|
28
|
+
exports.toRruleStr = toRruleStr;
|
|
29
|
+
const TOKEN_TYPES = {
|
|
30
|
+
SIGN: 'SIGN',
|
|
31
|
+
DAYS: 'DAYS',
|
|
32
|
+
TIME_START: 'TIME_START',
|
|
33
|
+
HOURS: 'HOURS',
|
|
34
|
+
MINUTES: 'MINUTES',
|
|
35
|
+
SECONDS: 'SECONDS',
|
|
36
|
+
WEEKS: 'WEEKS',
|
|
37
|
+
UNKNOWN: 'UNKNOWN',
|
|
38
|
+
};
|
|
39
|
+
const parseDuration = (duration) => {
|
|
40
|
+
// --- lexer
|
|
41
|
+
// follows https://tools.ietf.org/html/rfc5545#section-3.3.6
|
|
42
|
+
let windowStart = 0;
|
|
43
|
+
const tokens = [];
|
|
44
|
+
for (let windowEnd = 1; windowEnd <= duration.length; windowEnd++) {
|
|
45
|
+
const token = duration.slice(windowStart, windowEnd);
|
|
46
|
+
if (/^[+-]?P$/.test(token)) {
|
|
47
|
+
const val = token[0] === '-' ? -1 : 1;
|
|
48
|
+
tokens.push({ type: TOKEN_TYPES.SIGN, val: val });
|
|
49
|
+
windowStart = windowEnd;
|
|
50
|
+
}
|
|
51
|
+
else if (/^[0-9]*W$/.test(token)) {
|
|
52
|
+
const val = parseInt(token.slice(0, -1), 10);
|
|
53
|
+
tokens.push({ type: TOKEN_TYPES.WEEKS, val: val });
|
|
54
|
+
windowStart = windowEnd;
|
|
55
|
+
}
|
|
56
|
+
else if (/^[0-9]*D$/.test(token)) {
|
|
57
|
+
const val = parseInt(token.slice(0, -1), 10);
|
|
58
|
+
tokens.push({ type: TOKEN_TYPES.DAYS, val: val });
|
|
59
|
+
windowStart = windowEnd;
|
|
60
|
+
}
|
|
61
|
+
else if (token === 'T') {
|
|
62
|
+
tokens.push({ type: TOKEN_TYPES.TIME_START, val: -1 });
|
|
63
|
+
windowStart = windowEnd;
|
|
64
|
+
}
|
|
65
|
+
else if (/^[0-9]*\.?[0-9]*H$/.test(token)) {
|
|
66
|
+
const val = parseFloat(token.slice(0, -1));
|
|
67
|
+
tokens.push({ type: TOKEN_TYPES.HOURS, val: val });
|
|
68
|
+
windowStart = windowEnd;
|
|
69
|
+
}
|
|
70
|
+
else if (/^[0-9]*M$/.test(token)) {
|
|
71
|
+
const val = parseInt(token.slice(0, -1), 10);
|
|
72
|
+
tokens.push({ type: TOKEN_TYPES.MINUTES, val: val });
|
|
73
|
+
windowStart = windowEnd;
|
|
74
|
+
}
|
|
75
|
+
else if (/^[0-9]*S$/.test(token)) {
|
|
76
|
+
const val = parseInt(token.slice(0, -1), 10);
|
|
77
|
+
tokens.push({ type: TOKEN_TYPES.SECONDS, val: val });
|
|
78
|
+
windowStart = windowEnd;
|
|
79
|
+
}
|
|
80
|
+
else if (windowEnd === duration.length) {
|
|
81
|
+
tokens.push({ type: TOKEN_TYPES.UNKNOWN, val: token });
|
|
82
|
+
windowStart = windowEnd;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// --- parser
|
|
86
|
+
// this parser is very permissive and doesn't care about token order, repetitions, etc.
|
|
87
|
+
let parsedDurationValue = 0;
|
|
88
|
+
// for token type of hours, because we made it support fractions, apply parseInt
|
|
89
|
+
// to round it up to integer
|
|
90
|
+
for (const token of tokens) {
|
|
91
|
+
if (token.type === TOKEN_TYPES.SIGN)
|
|
92
|
+
parsedDurationValue = token.val;
|
|
93
|
+
else if (token.type === TOKEN_TYPES.WEEKS)
|
|
94
|
+
parsedDurationValue = token.val * 7 * 24 * 60 * 60 * 1000;
|
|
95
|
+
else if (token.type === TOKEN_TYPES.DAYS)
|
|
96
|
+
parsedDurationValue = token.val * 24 * 60 * 60 * 1000;
|
|
97
|
+
else if (token.type === TOKEN_TYPES.HOURS)
|
|
98
|
+
parsedDurationValue = token.val * 60 * 60 * 1000, 10;
|
|
99
|
+
else if (token.type === TOKEN_TYPES.MINUTES)
|
|
100
|
+
parsedDurationValue = token.val * 60 * 1000;
|
|
101
|
+
else if (token.type === TOKEN_TYPES.SECONDS)
|
|
102
|
+
parsedDurationValue = token.val * 1000;
|
|
103
|
+
// TODO check if we use this, MILLISECONDS is no longer supported
|
|
104
|
+
// else if (token.type === TOKEN_TYPES.MILLISECONDS) parsedDurationValue = token.val;
|
|
105
|
+
}
|
|
106
|
+
return parsedDurationValue;
|
|
107
|
+
};
|
|
108
|
+
exports.parseDuration = parseDuration;
|
|
109
|
+
const getDuration = (icalObj) => {
|
|
110
|
+
const duration = icalObj.DURATION;
|
|
111
|
+
// make it v1 backward compatible
|
|
112
|
+
const dtend = icalObj.DTEND;
|
|
113
|
+
const dtstart = icalObj.DTSTART;
|
|
114
|
+
if (duration && dtend) {
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
if (duration) {
|
|
118
|
+
return (0, exports.parseDuration)(duration);
|
|
119
|
+
}
|
|
120
|
+
if (dtstart && dtend) {
|
|
121
|
+
const start = luxon_1.DateTime.fromISO(dtstart);
|
|
122
|
+
const end = luxon_1.DateTime.fromISO(dtend);
|
|
123
|
+
const difference = end.diff(start);
|
|
124
|
+
if (!difference.isValid)
|
|
125
|
+
return 0;
|
|
126
|
+
return difference.toMillis();
|
|
127
|
+
}
|
|
128
|
+
return 0;
|
|
129
|
+
};
|
|
130
|
+
exports.getDuration = getDuration;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const parsing_1 = require("./parsing");
|
|
4
|
+
it('removes null or undefined fields', () => {
|
|
5
|
+
expect((0, parsing_1.toRruleStr)({
|
|
6
|
+
DTSTART: '20120201T023000Z',
|
|
7
|
+
DURATION: '1h',
|
|
8
|
+
COUNT: null,
|
|
9
|
+
UNTIL: undefined,
|
|
10
|
+
})).toEqual('DTSTART=20120201T023000Z;DURATION=1h');
|
|
11
|
+
});
|
package/lib/rrule.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { RRule } from 'rrule';
|
|
2
|
+
import { toRruleObj, toRruleStr } from './rrule/index';
|
|
3
|
+
type ToFrom = {
|
|
4
|
+
from: Date;
|
|
5
|
+
to: Date;
|
|
6
|
+
};
|
|
7
|
+
export { toRruleObj, toRruleStr };
|
|
8
|
+
export declare const parse: (str: any) => {
|
|
9
|
+
duration: any;
|
|
10
|
+
durationString: unknown;
|
|
11
|
+
rrule: RRule | import("rrule").RRuleSet;
|
|
12
|
+
dtend: unknown;
|
|
13
|
+
};
|
|
14
|
+
export declare const validateRruleConfig: (obj: any) => string[];
|
|
15
|
+
export declare const validateRruleConfigV2: (bcRruleInstance: any) => string[];
|
|
16
|
+
/**
|
|
17
|
+
* a helper function that put offical rrule and becollective rrule fields together
|
|
18
|
+
* @param {Object} rruleInstance an RRule instance.
|
|
19
|
+
* @param {*} duration non rrule standard duration field.
|
|
20
|
+
*/
|
|
21
|
+
export declare const buildBcRruleString: (rruleInstance: any, duration?: string) => any;
|
|
22
|
+
export declare const isValidRrule: (obj: any) => boolean;
|
|
23
|
+
export declare const recurrenceToText: (recurrenceRule: any, timezone: any, isFlexible?: boolean) => {
|
|
24
|
+
text: string;
|
|
25
|
+
timezone: any;
|
|
26
|
+
time?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
text: string;
|
|
29
|
+
time: string;
|
|
30
|
+
timezone: any;
|
|
31
|
+
};
|
|
32
|
+
export declare const recurrenceToTextInTimezone: (recurrenceRule: any, timezone: any) => {
|
|
33
|
+
text: string;
|
|
34
|
+
time: string;
|
|
35
|
+
localTimezone: any;
|
|
36
|
+
};
|
|
37
|
+
export declare const recurrencesBetween: (fromDate: any, toDate: any, recurrenceRule: any, timezone: any) => {
|
|
38
|
+
from: any;
|
|
39
|
+
to: Date;
|
|
40
|
+
}[];
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @param {*} rrule
|
|
44
|
+
* @param {*} timezone
|
|
45
|
+
* @param {*} from
|
|
46
|
+
* @param {*} isFlexible
|
|
47
|
+
* @returns an array of date in true utc (DST offset is considered)
|
|
48
|
+
*/
|
|
49
|
+
export declare const recurrencesBetweenV2: (rrule: any, timezone: any, from?: Date, isFlexible?: boolean) => ToFrom[];
|