Add timeframe helper methods

This commit is contained in:
Kushal Pandya 2018-06-07 21:38:27 +05:30
parent 760b12dc6b
commit dd7a59bf94
2 changed files with 59 additions and 41 deletions

View file

@ -269,6 +269,17 @@ export const totalDaysInMonth = date => {
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};
/**
* Returns number of days in a quarter from provided
* months array.
*
* @param {Array} quarter
*/
export const totalDaysInQuarter = quarter => quarter.reduce(
(acc, month) => acc + totalDaysInMonth(month),
0,
);
/**
* Returns list of Dates referring to Sundays of the month
* based on provided date
@ -309,42 +320,27 @@ export const getSundays = date => {
};
/**
* Returns list of Dates representing a timeframe of Months from month of provided date (inclusive)
* up to provided length
*
* For eg;
* If current month is January 2018 and `length` provided is `6`
* Then this method will return list of Date objects as follows;
*
* [ October 2017, November 2017, December 2017, January 2018, February 2018, March 2018 ]
*
* If current month is March 2018 and `length` provided is `3`
* Then this method will return list of Date objects as follows;
*
* [ February 2018, March 2018, April 2018 ]
* Returns list of Dates representing a timeframe of months from startDate and length
*
* @param {Date} startDate
* @param {Number} length
* @param {Date} date
*/
export const getTimeframeWindow = (length, date) => {
if (!length) {
export const getTimeframeWindowFrom = (startDate, length) => {
if (!(startDate instanceof Date) || !length) {
return [];
}
const currentDate = date instanceof Date ? date : new Date();
const currentMonthIndex = Math.floor(length / 2);
const timeframe = [];
// Move date object backward to the first month of timeframe
currentDate.setDate(1);
currentDate.setMonth(currentDate.getMonth() - currentMonthIndex);
// Iterate and update date for the size of length
// Iterate and set date for the size of length
// and push date reference to timeframe list
for (let i = 0; i < length; i += 1) {
timeframe.push(new Date(currentDate.getTime()));
currentDate.setMonth(currentDate.getMonth() + 1);
}
const timeframe = new Array(length)
.fill()
.map(
(val, i) => new Date(
startDate.getFullYear(),
startDate.getMonth() + i,
1,
),
);
// Change date of last timeframe item to last date of the month
timeframe[length - 1].setDate(totalDaysInMonth(timeframe[length - 1]));
@ -352,6 +348,29 @@ export const getTimeframeWindow = (length, date) => {
return timeframe;
};
/**
* Returns count of day within current quarter from provided date
* and array of months for the quarter
*
* Eg;
* If date is 15 Feb 2018
* and quarter is [Jan, Feb, Mar]
*
* Then 15th Feb is 46th day of the quarter
* Where 31 (days in Jan) + 15 (date of Feb).
*
* @param {Date} date
* @param {Array} quarter
*/
export const dayInQuarter = (date, quarter) => quarter.reduce((acc, month) => {
if (date.getMonth() > month.getMonth()) {
return acc + totalDaysInMonth(month);
} else if (date.getMonth() === month.getMonth()) {
return acc + date.getDate();
}
return acc + 0;
}, 0);
window.gl = window.gl || {};
window.gl.utils = {
...(window.gl.utils || {}),

View file

@ -149,23 +149,22 @@ describe('getSundays', () => {
});
});
describe('getTimeframeWindow', () => {
it('returns array of dates representing a timeframe based on provided length and date', () => {
const date = new Date(2018, 0, 1);
describe('getTimeframeWindowFrom', () => {
it('returns array of date objects upto provided length start with provided startDate', () => {
const startDate = new Date(2018, 0, 1);
const mockTimeframe = [
new Date(2017, 9, 1),
new Date(2017, 10, 1),
new Date(2017, 11, 1),
new Date(2018, 0, 1),
new Date(2018, 1, 1),
new Date(2018, 2, 31),
new Date(2018, 2, 1),
new Date(2018, 3, 1),
new Date(2018, 4, 31),
];
const timeframe = datetimeUtility.getTimeframeWindow(6, date);
expect(timeframe.length).toBe(6);
const timeframe = datetimeUtility.getTimeframeWindowFrom(startDate, 5);
expect(timeframe.length).toBe(5);
timeframe.forEach((timeframeItem, index) => {
expect(timeframeItem.getFullYear() === mockTimeframe[index].getFullYear()).toBeTruthy();
expect(timeframeItem.getMonth() === mockTimeframe[index].getMonth()).toBeTruthy();
console.log(timeframeItem);
expect(timeframeItem.getFullYear() === mockTimeframe[index].getFullYear()).toBe(true);
expect(timeframeItem.getMonth() === mockTimeframe[index].getMonth()).toBe(true);
expect(timeframeItem.getDate() === mockTimeframe[index].getDate()).toBeTruthy();
});
});