Merge pull request #1674 from SergioCrisostomo/DRY-optimizations

Date utils improvements and specs
This commit is contained in:
Aresn 2017-08-24 15:48:23 +08:00 committed by GitHub
commit af4e91ca51
2 changed files with 82 additions and 45 deletions

View file

@ -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;
};
};

View file

@ -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);
}
});
});