Commit 2bb73dab authored by Kushal Pandya's avatar Kushal Pandya

Add `newDate` helper, update `getTimeframeWindowFrom`

Adds `newDate` helper to copy date object without keeping reference.
Adds negative length support in `getTimeframeWindowFrom` to go
back in time.
parent 214d741b
......@@ -7,6 +7,14 @@ import { languageCode, s__ } from '../../locale';
window.timeago = timeago;
/**
* This method allows you to create new Date instance from existing
* date instance without keeping the reference.
*
* @param {Date} date
*/
export const newDate = date => (date instanceof Date ? new Date(date.getTime()) : new Date());
/**
* Returns i18n month names array.
* If `abbreviated` is provided, returns abbreviated
......@@ -321,23 +329,35 @@ export const getSundays = date => {
/**
* Returns list of Dates representing a timeframe of months from startDate and length
* This method also supports going back in time when `length` is negative number
*
* @param {Date} startDate
* @param {Date} initialStartDate
* @param {Number} length
*/
export const getTimeframeWindowFrom = (startDate, length) => {
if (!(startDate instanceof Date) || !length) {
export const getTimeframeWindowFrom = (initialStartDate, length) => {
if (!(initialStartDate instanceof Date) || !length) {
return [];
}
const startDate = newDate(initialStartDate);
const moveMonthBy = length > 0 ? 1 : -1;
startDate.setDate(1);
startDate.setHours(0, 0, 0, 0);
// Iterate and set date for the size of length
// and push date reference to timeframe list
const timeframe = new Array(length)
.fill()
.map((val, i) => new Date(startDate.getFullYear(), startDate.getMonth() + i, 1));
const timeframe = new Array(Math.abs(length)).fill().map(() => {
const currentMonth = startDate.getTime();
startDate.setMonth(startDate.getMonth() + moveMonthBy);
return new Date(currentMonth);
});
// Change date of last timeframe item to last date of the month
timeframe[length - 1].setDate(totalDaysInMonth(timeframe[length - 1]));
// when length is positive
if (length > 0) {
timeframe[timeframe.length - 1].setDate(totalDaysInMonth(timeframe[timeframe.length - 1]));
}
return timeframe;
};
......
......@@ -156,7 +156,7 @@ describe('getSundays', () => {
});
describe('getTimeframeWindowFrom', () => {
it('returns array of date objects upto provided length start with provided startDate', () => {
it('returns array of date objects upto provided length (positive number) into the future starting from provided startDate', () => {
const startDate = new Date(2018, 0, 1);
const mockTimeframe = [
new Date(2018, 0, 1),
......@@ -174,6 +174,25 @@ describe('getTimeframeWindowFrom', () => {
expect(timeframeItem.getDate()).toBe(mockTimeframe[index].getDate());
});
});
it('returns array of date objects upto provided length (negative number) into the past starting from provided startDate', () => {
const startDate = new Date(2018, 0, 1);
const mockTimeframe = [
new Date(2018, 0, 1),
new Date(2017, 11, 1),
new Date(2017, 10, 1),
new Date(2017, 9, 1),
new Date(2017, 8, 1),
];
const timeframe = datetimeUtility.getTimeframeWindowFrom(startDate, -5);
expect(timeframe.length).toBe(5);
timeframe.forEach((timeframeItem, index) => {
expect(timeframeItem.getFullYear()).toBe(mockTimeframe[index].getFullYear());
expect(timeframeItem.getMonth()).toBe(mockTimeframe[index].getMonth());
expect(timeframeItem.getDate()).toBe(mockTimeframe[index].getDate());
});
});
});
describe('formatTime', () => {
......@@ -376,3 +395,22 @@ describe('calculateRemainingMilliseconds', () => {
expect(milliseconds).toBe(0);
});
});
describe('newDate', () => {
it('returns new date instance from existing date instance', () => {
const initialDate = new Date(2019, 0, 1);
const copiedDate = datetimeUtility.newDate(initialDate);
expect(copiedDate.getTime()).toBe(initialDate.getTime());
initialDate.setMonth(initialDate.getMonth() + 1);
expect(copiedDate.getTime()).not.toBe(initialDate.getTime());
});
it('returns date instance when provided date param is not of type date or is undefined', () => {
const initialDate = datetimeUtility.newDate();
expect(initialDate instanceof Date).toBe(true);
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment