From c91c30ccbb95a49fadc515690867d65544a186e2 Mon Sep 17 00:00:00 2001 From: Sergio Crisostomo Date: Thu, 24 Aug 2017 08:21:50 +0200 Subject: [PATCH] Date utils improvements and specs --- src/components/date-picker/util.js | 60 +++++++++++++++--------------------------------------------- test/unit/specs/date-picker-utils.spec.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 45 deletions(-) create mode 100644 test/unit/specs/date-picker-utils.spec.js diff --git a/src/components/date-picker/util.js b/src/components/date-picker/util.js index 0edb09f..4932633 100644 --- a/src/components/date-picker/util.js +++ b/src/components/date-picker/util.js @@ -17,19 +17,7 @@ export const parseDate = function(string, format) { }; export const getDayCountOfMonth = function(year, month) { - if (month === 3 || month === 5 || month === 8 || month === 10) { - return 30; - } - - if (month === 1) { - if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) { - return 29; - } else { - return 28; - } - } - - return 31; + return new Date(year, month + 1, 0).getDate(); }; export const getFirstDayOfMonth = function(date) { @@ -38,48 +26,30 @@ export const getFirstDayOfMonth = function(date) { return temp.getDay(); }; -export const prevMonth = function(src) { - const year = src.getFullYear(); - const month = src.getMonth(); - const date = src.getDate(); - - const newYear = month === 0 ? year - 1 : year; - const newMonth = month === 0 ? 11 : month - 1; - - const newMonthDayCount = getDayCountOfMonth(newYear, newMonth); - if (newMonthDayCount < date) { - src.setDate(newMonthDayCount); +export const siblingMonth = function(src, diff) { + const temp = new Date(src); // lets copy it so we don't change the original + const newMonth = temp.getMonth() + diff; + const newMonthDayCount = getDayCountOfMonth(temp.getFullYear(), newMonth); + if (newMonthDayCount < temp.getDate()) { + temp.setDate(newMonthDayCount); } + temp.setMonth(newMonth); - src.setMonth(newMonth); - src.setFullYear(newYear); + return temp; +}; - return new Date(src.getTime()); +export const prevMonth = function(src) { + return siblingMonth(src, -1); }; export const nextMonth = function(src) { - const year = src.getFullYear(); - const month = src.getMonth(); - const date = src.getDate(); - - const newYear = month === 11 ? year + 1 : year; - const newMonth = month === 11 ? 0 : month + 1; - - const newMonthDayCount = getDayCountOfMonth(newYear, newMonth); - if (newMonthDayCount < date) { - src.setDate(newMonthDayCount); - } - - src.setMonth(newMonth); - src.setFullYear(newYear); - - return new Date(src.getTime()); + return siblingMonth(src, 1); }; -export const initTimeDate = function () { +export const initTimeDate = function() { const date = new Date(); date.setHours(0); date.setMinutes(0); date.setSeconds(0); return date; -}; \ No newline at end of file +}; diff --git a/test/unit/specs/date-picker-utils.spec.js b/test/unit/specs/date-picker-utils.spec.js new file mode 100644 index 0000000..496bbf3 --- /dev/null +++ b/test/unit/specs/date-picker-utils.spec.js @@ -0,0 +1,67 @@ +const {prevMonth, nextMonth, getDayCountOfMonth} = require('../../../src/components/date-picker/util.js'); + +// yyyy-mm-dd -> Date +function dateFromString(str) { + str = str.split('-').map(Number); + str[1] = str[1] - 1; + return new Date(...str); +} + +// Date -> yyyy-mm-dd +function dateToString(date) { + return [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-'); +} + +describe('DatePicker utility functions', () => { + const assets = [ + {date: '2030-3-31', prevMonth: '2030-2-28', nextMonth: '2030-4-30', count: 31}, + {date: '2030-3-28', prevMonth: '2030-2-28', nextMonth: '2030-4-28', count: 31}, + {date: '2030-3-1', prevMonth: '2030-2-1', nextMonth: '2030-4-1', count: 31}, + {date: '2030-2-1', prevMonth: '2030-1-1', nextMonth: '2030-3-1', count: 28}, + {date: '2030-1-1', prevMonth: '2029-12-1', nextMonth: '2030-2-1', count: 31}, + {date: '2030-12-31', prevMonth: '2030-11-30', nextMonth: '2031-1-31', count: 31}, + {date: '2030-6-30', prevMonth: '2030-5-30', nextMonth: '2030-7-30', count: 30}, + {date: '2030-5-31', prevMonth: '2030-4-30', nextMonth: '2030-6-30', count: 31}, + {date: '2032-3-31', prevMonth: '2032-2-29', nextMonth: '2032-4-30', count: 31}, + {date: '2032-2-1', prevMonth: '2032-1-1', nextMonth: '2032-3-1', count: 29} + ]; + + it('Should behave as pure functions and not change source date', () => { + const date = new Date(2030, 4, 10); + const original = date.getMonth(); + const foo = prevMonth(date); + + expect(original).to.equal(date.getMonth()); + + const bar = nextMonth(date); + expect(original).to.equal(date.getMonth()); + expect(bar.getMonth() - foo.getMonth()).to.equal(2); + }); + + it('Should calculate the previous month', () => { + for (const asset of assets) { + const date = dateFromString(asset.date); + const previous = prevMonth(date); + + expect(dateToString(previous)).to.equal(asset.prevMonth); + } + }); + + it('Should calculate the next month', () => { + for (const asset of assets) { + const date = dateFromString(asset.date); + const next = nextMonth(date); + + expect(dateToString(next)).to.equal(asset.nextMonth); + } + }); + + it('Should calculate the month length', () => { + for (const asset of assets) { + const date = dateFromString(asset.date); + const monthLength = getDayCountOfMonth(date.getFullYear(), date.getMonth()); + + expect(monthLength).to.equal(asset.count); + } + }); +}); -- libgit2 0.21.4