This was forked from Brian Holt
A Pen by piggyslasher on CodePen.
This was forked from Brian Holt
A Pen by piggyslasher on CodePen.
/* | |
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" /> |