Skip to content

Commit

Permalink
helpers: Separate concerns between helper files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Parnell committed Mar 26, 2021
1 parent 88c39cb commit c5418ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
29 changes: 9 additions & 20 deletions src/helpers/rrule-options.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
const getFrequency = require('./frequency-converter');
const getDateTime = require('./datetime-helper');

function generateRRuleOptions(node) {
const { freq, interval } = getFrequency(node.getValue('repeatFrequency'));
const properties = {
freq,
interval,
byDay: node.getValue('byDay'),
byMonth: node.getValue('byMonth'),
byMonthDay: node.getValue('byMonthDay'),
startDate: node.getValue('startDate'),
startTime: node.getValue('startTime'),
endDate: node.getValue('endDate'),
endTime: node.getValue('endTime'),
count: node.getValue('count'),
scheduleTimezone: node.getValue('scheduleTimezone'),
exceptDate: node.getValue('exceptDate'),
};

function generateRRuleOptions(properties) {
const dtStart = getDateTime(properties.startDate, properties.startTime);
const dtEnd = getDateTime(properties.endDate, properties.endTime);

const rruleOptions = { freq, interval }; // this is the only required one
const rruleOptions = {};

if (typeof properties.freq !== 'undefined') {
rruleOptions.freq = properties.freq;
}
if (typeof properties.interval !== 'undefined') {
rruleOptions.interval = properties.interval;
}
if (typeof dtStart !== 'undefined') {
rruleOptions.dtstart = dtStart;
}
Expand All @@ -44,7 +33,7 @@ function generateRRuleOptions(node) {
if (typeof properties.scheduleTimezone !== 'undefined') {
rruleOptions.tzid = properties.scheduleTimezone;
}
return { rruleOptions, properties };
return rruleOptions;
}

module.exports = generateRRuleOptions;
22 changes: 22 additions & 0 deletions src/helpers/schedule-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const getFrequency = require('./frequency-converter');

function getScheduleProperties(node) {
const { freq, interval } = getFrequency(node.getValue('repeatFrequency'));
const properties = {
freq,
interval,
byDay: node.getValue('byDay'),
byMonth: node.getValue('byMonth'),
byMonthDay: node.getValue('byMonthDay'),
startDate: node.getValue('startDate'),
startTime: node.getValue('startTime'),
endDate: node.getValue('endDate'),
endTime: node.getValue('endTime'),
count: node.getValue('count'),
scheduleTimezone: node.getValue('scheduleTimezone'),
exceptDate: node.getValue('exceptDate'),
};
return properties;
}

module.exports = getScheduleProperties;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { RRule } = require('rrule');
const Rule = require('../rule');
const generateRRuleOptions = require('../../helpers/rrule-options');
const getScheduleProperties = require('../../helpers/schedule-properties');
const ValidationErrorType = require('../../errors/validation-error-type');
const ValidationErrorCategory = require('../../errors/validation-error-category');
const ValidationErrorSeverity = require('../../errors/validation-error-severity');
Expand Down Expand Up @@ -48,7 +49,8 @@ module.exports = class ValidRecurrenceRule extends Rule {
validateModel(node) {
const errors = [];

const { rruleOptions, properties } = generateRRuleOptions(node);
const properties = getScheduleProperties(node);
const rruleOptions = generateRRuleOptions(properties);

if (typeof properties.startDate === 'undefined'
|| typeof properties.startTime === 'undefined'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const generateRRuleOptions = require('../../helpers/rrule-options');
const ValidationErrorType = require('../../errors/validation-error-type');
const ValidationErrorCategory = require('../../errors/validation-error-category');
const ValidationErrorSeverity = require('../../errors/validation-error-severity');
const getScheduleProperties = require('../../helpers/schedule-properties');

module.exports = class ExceptDatesAreInSchedule extends Rule {
constructor(options) {
Expand Down Expand Up @@ -37,7 +38,8 @@ module.exports = class ExceptDatesAreInSchedule extends Rule {
validateModel(node) {
const errors = [];

const { rruleOptions, properties } = generateRRuleOptions(node);
const properties = getScheduleProperties(node);
const rruleOptions = generateRRuleOptions(properties);

if (typeof properties.exceptDate === 'undefined') {
return [];
Expand Down

0 comments on commit c5418ff

Please sign in to comment.