Skip to content

Instantly share code, notes, and snippets.

@vviikk
Created October 22, 2018 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vviikk/d5d18740f44662ca8a1c2b6b148e62ba to your computer and use it in GitHub Desktop.
Save vviikk/d5d18740f44662ca8a1c2b6b148e62ba to your computer and use it in GitHub Desktop.
Event schedule utility function
/*
Date Calculation
An event takes place twice weekly on a Wednesday and a Saturday at 8pm. Write a function that calculates and returns the next valid draw date based on the current date and time and also on an optional supplied date.
Originally a PHP exercise.
*/
// https://hackernoon.com/a-quick-handbook-for-dates-in-javascript-7b71d0ef8e53
const getIndexedObj = arr => arr.reduce(
(acc, c, idx) => ({ ...acc, ...({ [c.toLowerCase()]: idx }) })
, {})
const namesOfDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const days = getIndexedObj(namesOfDays)
const repeat = {
yearly: 'Year', // @TODO - research on being dynamic
monthly: 'Month', // @TODO - research on being dynamic
weekly: 7,
daily: 'Date',
}
const d = s => s
? new Date(s)
: new Date() // Convert date string to date -- empty string is current date
// console.log(days)
const schedule = [
{
startDateTime: d('3 Oct 2018 20:00'), // wednesday
occurs: [days.wednesday, days.saturday],
repeatType: repeat.daily,
endDate: d('31 Oct 2018 20:00'),
}
]
const getNextDateByDay = (day, initDate = d()) => {
console.log('bef:' + initDate + '')
initDate.setUTCDate(initDate.getUTCDate() + (7 - initDate.getUTCDay()) % 7 + day + 1)
console.log(initDate + '')
return initDate;
}
console.log('start')
function getRecurringDates(
startDate,
endDate,
interval,
intervalType,
occurs,
noweekends = false
) {
// console.log(arguments)
intervalType = intervalType || 'Date';
var date = startDate;
var recurrent = [];
var setget = {set: 'set'+intervalType, get: 'get'+intervalType};
while (date < endDate) {
recurrent.push( occurs ? isMatch(occurs, date) : new Date(date) );
date[setget.set](date[setget.get]() + interval);
}
function isMatch(occurs, d) {
console.log(d, d.getDay())
return true
}
// add 1 day for sunday, subtract one for saturday
function noWeekend() {
var currdate = new Date(date), day = date.getDay();
if (~[6,0].indexOf(day)) {
currdate.setDate(currdate.getDate() + (day == 6 ? -1 : 1));
}
return new Date(currdate);
}
return recurrent;
}
console.log(
'asd',
getRecurringDates(
new Date('23 Oct 2018'),
new Date('25 Oct 2018'),
1,
null,
[days.monday]
).map(d => namesOfDays[d.getDate()])
)
const getNextEventDate = (event, dateFrom = Date.now()) => {
/*console.log(
event.occurs.sort().find(
(i) => {
console.log(i)
return true
}
)
);
return dd.setUTCDate(dd.getUTCDate() + (7 - dd.getUTCDay()) % 7 + 1);*/
}
// console.log(getNextEventDate(schedule[0]))
// Assertion below
describe('get next scheduled event', function() {
it('should generate the days arrays', () => {
// expect(addr(1 ,2)).toEqual(3);
expect(days.monday).toEqual(0)
expect(days.friday).toEqual(namesOfDays.indexOf('Friday'))
namesOfDays.forEach(
(dayName, index) => expect(days[dayName.toLowerCase()]).toEqual(index)
)
// Uncomment below to see Failing test
// expect(addr(-1 ,-2)).toEqual(-4);
})
it('should return next event day', () => {
// expect(getNextEventDate(schedule[0], Date.now())).toEqual()
})
it('should return next event date', () => {
// expect(getNextEventDate(schedule[0], Date.now())).toEqual()
})
it('shoudl return the date of the next occuring day (i.e. monday, tuesday)', () => {
// expect(getNextDateByDay(days.tuesday, new Date('1 Oct 2018 16:00'))).toEqual(10)
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment