compare-dates
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -5,5 +5,5 @@ 'use strict'; | ||
var isValidDate = function (input) { | ||
return input instanceof Date && !isNaN(input.getTime()); | ||
}; | ||
function isValidDate (input) { | ||
return input instanceof Date && !isNaN(input.getTime()); | ||
} | ||
@@ -13,4 +13,4 @@ const aliases = {}; | ||
function addUnitAlias(unit, shorthand) { | ||
var lowerCase = unit.toLowerCase(); | ||
aliases[lowerCase] = aliases[lowerCase + 's'] = aliases[shorthand] = unit; | ||
var lowerCase = unit.toLowerCase(); | ||
aliases[lowerCase] = aliases[lowerCase + 's'] = aliases[shorthand] = unit; | ||
} | ||
@@ -34,88 +34,88 @@ | ||
addUnitAlias('millisecond', 'ms'); | ||
function normalizeUnits(units) { | ||
return typeof units === 'string' ? aliases[units] || aliases[units.toLowerCase()] : undefined; | ||
return typeof units === 'string' ? aliases[units] || aliases[units.toLowerCase()] : undefined; | ||
} | ||
function setMonth(date, month) { | ||
const dayOfMonth = Math.min(date.getDate(), daysInMonth(date.getFullYear(), month)); | ||
date.setMonth(month, dayOfMonth); | ||
const dayOfMonth = Math.min(date.getDate(), daysInMonth(date.getFullYear(), month)); | ||
date.setMonth(month, dayOfMonth); | ||
} | ||
function daysInMonth(year, month) { | ||
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); | ||
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); | ||
} | ||
function setDayOfWeek(date, input) { | ||
const day = date.getDay(); | ||
date.setDate(date.getDate() + input - day); | ||
const day = date.getDay(); | ||
date.setDate(date.getDate() + input - day); | ||
} | ||
function Duration(duration) { | ||
var years = duration.year || 0, | ||
quarters = duration.quarter || 0, | ||
months = duration.month || 0, | ||
weeks = duration.week || 0, | ||
days = duration.day || 0, | ||
hours = duration.hour || 0, | ||
minutes = duration.minute || 0, | ||
seconds = duration.second || 0, | ||
milliseconds = duration.millisecond || 0; | ||
var years = duration.year || 0, | ||
quarters = duration.quarter || 0, | ||
months = duration.month || 0, | ||
weeks = duration.week || 0, | ||
days = duration.day || 0, | ||
hours = duration.hour || 0, | ||
minutes = duration.minute || 0, | ||
seconds = duration.second || 0, | ||
milliseconds = duration.millisecond || 0; // representation for dateAddRemove | ||
// representation for dateAddRemove | ||
this._milliseconds = +milliseconds + seconds * 1e3 + // 1000 | ||
minutes * 6e4 + // 1000 * 60 | ||
hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978 | ||
// Because of dateAddRemove treats 24 hours as different from a | ||
// day when working around DST, we need to store them separately | ||
this._days = +days + weeks * 7; | ||
// It is impossible translate months into days without knowing | ||
// which months you are are talking about, so we have to store | ||
// it separately. | ||
this._months = +months + quarters * 3 + years * 12; | ||
this._milliseconds = +milliseconds + seconds * 1e3 + // 1000 | ||
minutes * 6e4 + // 1000 * 60 | ||
hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978 | ||
// Because of dateAddRemove treats 24 hours as different from a | ||
// day when working around DST, we need to store them separately | ||
this._days = +days + weeks * 7; // It is impossible translate months into days without knowing | ||
// which months you are are talking about, so we have to store | ||
// it separately. | ||
this._months = +months + quarters * 3 + years * 12; | ||
} | ||
function createDuration(input, key) { | ||
const duration = {}; | ||
if (key) { | ||
duration[normalizeUnits(key)] = input; | ||
} else { | ||
duration.millisecond = input; | ||
} | ||
const duration = {}; | ||
return new Duration(duration); | ||
if (key) { | ||
duration[normalizeUnits(key)] = input; | ||
} else { | ||
duration.millisecond = input; | ||
} | ||
return new Duration(duration); | ||
} | ||
function absRound(number) { | ||
if (number < 0) { | ||
return Math.round(-1 * number) * -1; | ||
} else { | ||
return Math.round(number); | ||
} | ||
if (number < 0) { | ||
return Math.round(-1 * number) * -1; | ||
} else { | ||
return Math.round(number); | ||
} | ||
} | ||
function createAdder(direction) { | ||
return function (date, val, period) { | ||
const duration = createDuration(val, period); | ||
return addSubtract(date, duration, direction); | ||
}; | ||
return function (date, val, period) { | ||
const duration = createDuration(val, period); | ||
return addSubtract(date, duration, direction); | ||
}; | ||
} | ||
function addSubtract(original, duration, isAdding) { | ||
const date = new Date(original.valueOf()); | ||
const milliseconds = duration._milliseconds; | ||
const days = absRound(duration._days); | ||
const months = absRound(duration._months); | ||
const date = new Date(original.valueOf()); | ||
const milliseconds = duration._milliseconds; | ||
const days = absRound(duration._days); | ||
const months = absRound(duration._months); | ||
if (milliseconds) { | ||
date.setTime(date.valueOf() + milliseconds * isAdding); | ||
} | ||
if (days) { | ||
date.setDate(date.getDate() + days * isAdding); | ||
//set(mom, 'Date', get(mom, 'Date') + days * isAdding); | ||
} | ||
if (months) { | ||
setMonth(date, date.getMonth() + months * isAdding); | ||
} | ||
return date; | ||
if (milliseconds) { | ||
date.setTime(date.valueOf() + milliseconds * isAdding); | ||
} | ||
if (days) { | ||
date.setDate(date.getDate() + days * isAdding); //set(mom, 'Date', get(mom, 'Date') + days * isAdding); | ||
} | ||
if (months) { | ||
setMonth(date, date.getMonth() + months * isAdding); | ||
} | ||
return date; | ||
} | ||
@@ -127,202 +127,211 @@ | ||
function startOf(date, units) { | ||
units = normalizeUnits(units); | ||
const modified = new Date(date.valueOf()); | ||
// the following switch intentionally omits break keywords | ||
// to utilize falling through the cases. | ||
switch (units) { | ||
case 'year': | ||
setMonth(modified, 0); | ||
/* falls through */ | ||
case 'quarter': | ||
case 'month': | ||
modified.setDate(1); | ||
/* falls through */ | ||
case 'week': | ||
case 'day': | ||
case 'date': | ||
modified.setHours(0); | ||
/* falls through */ | ||
case 'hour': | ||
modified.setMinutes(0); | ||
/* falls through */ | ||
case 'minute': | ||
modified.setSeconds(0); | ||
/* falls through */ | ||
case 'second': | ||
modified.setMilliseconds(0); | ||
} | ||
units = normalizeUnits(units); | ||
const modified = new Date(date.valueOf()); // the following switch intentionally omits break keywords | ||
// to utilize falling through the cases. | ||
// weeks are a special case | ||
if (units === 'week') { | ||
setDayOfWeek(modified, 1); | ||
} | ||
switch (units) { | ||
case 'year': | ||
setMonth(modified, 0); | ||
// quarters are also special | ||
if (units === 'quarter') { | ||
setMonth(modified, Math.floor(modified.getMonth() / 3) * 3); | ||
} | ||
/* falls through */ | ||
return modified; | ||
case 'quarter': | ||
case 'month': | ||
modified.setDate(1); | ||
/* falls through */ | ||
case 'week': | ||
case 'day': | ||
case 'date': | ||
modified.setHours(0); | ||
/* falls through */ | ||
case 'hour': | ||
modified.setMinutes(0); | ||
/* falls through */ | ||
case 'minute': | ||
modified.setSeconds(0); | ||
/* falls through */ | ||
case 'second': | ||
modified.setMilliseconds(0); | ||
} // weeks are a special case | ||
if (units === 'week') { | ||
setDayOfWeek(modified, 1); | ||
} // quarters are also special | ||
if (units === 'quarter') { | ||
setMonth(modified, Math.floor(modified.getMonth() / 3) * 3); | ||
} | ||
return modified; | ||
} | ||
function endOf(date, units) { | ||
units = normalizeUnits(units); | ||
if (units === undefined || units === 'millisecond') { | ||
return new Date(date.valueOf()); | ||
} | ||
units = normalizeUnits(units); | ||
// 'date' is an alias for 'day', so it should be considered as such. | ||
if (units === 'date') { | ||
units = 'day'; | ||
} | ||
if (units === undefined || units === 'millisecond') { | ||
return new Date(date.valueOf()); | ||
} // 'date' is an alias for 'day', so it should be considered as such. | ||
let modified = startOf(date, units); | ||
modified = add(modified, 1, units); | ||
modified = subtract(modified, 1, 'ms'); | ||
return modified; | ||
if (units === 'date') { | ||
units = 'day'; | ||
} | ||
let modified = startOf(date, units); | ||
modified = add(modified, 1, units); | ||
modified = subtract(modified, 1, 'ms'); | ||
return modified; | ||
} | ||
function isAfter(first, second, units) { | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first > second; | ||
} else { | ||
return second.valueOf() < startOf(first, units).valueOf(); | ||
} | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first > second; | ||
} else { | ||
return second.valueOf() < startOf(first, units).valueOf(); | ||
} | ||
} | ||
function isBefore(first, second, units) { | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
function isBefore(first, second, units) { | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first < second; | ||
} else { | ||
return endOf(first, units).valueOf() < second.valueOf(); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first < second; | ||
} else { | ||
return endOf(first, units).valueOf() < second.valueOf(); | ||
} | ||
} | ||
function isBetween(date, from, to, units, inclusivity) { | ||
inclusivity = inclusivity || '()'; | ||
return (inclusivity[0] === '(' ? isAfter(date, from, units) : !isBefore(date, from, units)) && (inclusivity[1] === ')' ? isBefore(date, to, units) : !isAfter(date, to, units)); | ||
inclusivity = inclusivity || '()'; | ||
return (inclusivity[0] === '(' ? isAfter(date, from, units) : !isBefore(date, from, units)) && (inclusivity[1] === ')' ? isBefore(date, to, units) : !isAfter(date, to, units)); | ||
} | ||
function isSame(date, input, units) { | ||
if (!isValidDate(date) || !isValidDate(input)) { | ||
throw new Error('Invalid date'); | ||
} | ||
function isSame(date, input, units) { | ||
if (!isValidDate(date) || !isValidDate(input)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return date.getTime() === input.getTime(); | ||
} else { | ||
const inputMs = input.valueOf(); | ||
return startOf(date, units).valueOf() <= inputMs && inputMs <= endOf(date, units).valueOf(); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return date.getTime() === input.getTime(); | ||
} else { | ||
const inputMs = input.valueOf(); | ||
return startOf(date, units).valueOf() <= inputMs && inputMs <= endOf(date, units).valueOf(); | ||
} | ||
} | ||
function isSameOrAfter(date, input, units) { | ||
return isSame(date, input, units) || isAfter(date, input, units); | ||
return isSame(date, input, units) || isAfter(date, input, units); | ||
} | ||
function isSameOrBefore(date, input, units) { | ||
return isSame(date, input, units) || isBefore(date, input, units); | ||
return isSame(date, input, units) || isBefore(date, input, units); | ||
} | ||
function pickBy(fn, moments) { | ||
var res, i; | ||
if (!moments.length) { | ||
return new Date(); | ||
} | ||
res = moments[0]; | ||
for (i = 1; i < moments.length; ++i) { | ||
if (!isValidDate(moments[i])) { | ||
throw new Error('Invalid date'); | ||
} else if (fn(moments[i], res)) { | ||
res = moments[i]; | ||
} | ||
} | ||
return res; | ||
} | ||
var res, i; | ||
function min() { | ||
for (var _len = arguments.length, dates = Array(_len), _key = 0; _key < _len; _key++) { | ||
dates[_key] = arguments[_key]; | ||
} | ||
if (!moments.length) { | ||
return new Date(); | ||
} | ||
return pickBy(isBefore, dates); | ||
} | ||
res = moments[0]; | ||
function max() { | ||
for (var _len2 = arguments.length, dates = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
dates[_key2] = arguments[_key2]; | ||
for (i = 1; i < moments.length; ++i) { | ||
if (!isValidDate(moments[i])) { | ||
throw new Error('Invalid date'); | ||
} else if (fn(moments[i], res)) { | ||
res = moments[i]; | ||
} | ||
} | ||
return pickBy(isAfter, dates); | ||
return res; | ||
} | ||
function min(...dates) { | ||
return pickBy(isBefore, dates); | ||
} | ||
function max(...dates) { | ||
return pickBy(isAfter, dates); | ||
} | ||
function diff(first, second, units, asFloat) { | ||
let output; | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
let output; | ||
units = normalizeUnits(units); | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
if (units === 'year' || units === 'month' || units === 'quarter') { | ||
output = monthDiff(first, second); | ||
if (units === 'quarter') { | ||
output = output / 3; | ||
} else if (units === 'year') { | ||
output = output / 12; | ||
} | ||
} else { | ||
const delta = first - second; | ||
output = units === 'second' ? delta / 1e3 : // 1000 | ||
units === 'minute' ? delta / 6e4 : // 1000 * 60 | ||
units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60 | ||
units === 'day' ? delta / 864e5 : // 1000 * 60 * 60 * 24, negate dst | ||
units === 'week' ? delta / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst | ||
delta; | ||
units = normalizeUnits(units); | ||
if (units === 'year' || units === 'month' || units === 'quarter') { | ||
output = monthDiff(first, second); | ||
if (units === 'quarter') { | ||
output = output / 3; | ||
} else if (units === 'year') { | ||
output = output / 12; | ||
} | ||
return asFloat ? output : absFloor(output); | ||
} else { | ||
const delta = first - second; | ||
output = units === 'second' ? delta / 1e3 : // 1000 | ||
units === 'minute' ? delta / 6e4 : // 1000 * 60 | ||
units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60 | ||
units === 'day' ? delta / 864e5 : // 1000 * 60 * 60 * 24, negate dst | ||
units === 'week' ? delta / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst | ||
delta; | ||
} | ||
return asFloat ? output : absFloor(output); | ||
} | ||
function absFloor(number) { | ||
if (number < 0) { | ||
// -0 -> 0 | ||
return Math.ceil(number) || 0; | ||
} else { | ||
return Math.floor(number); | ||
} | ||
if (number < 0) { | ||
// -0 -> 0 | ||
return Math.ceil(number) || 0; | ||
} else { | ||
return Math.floor(number); | ||
} | ||
} | ||
function monthDiff(a, b) { | ||
// difference in months | ||
var wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth()), | ||
// difference in months | ||
var wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth()), | ||
// b is in (anchor - 1 month, anchor + 1 month) | ||
anchor = add(a, wholeMonthDiff, 'months'), | ||
anchor2, | ||
adjust; | ||
// b is in (anchor - 1 month, anchor + 1 month) | ||
anchor = add(a, wholeMonthDiff, 'months'), | ||
anchor2, | ||
adjust; | ||
if (b - anchor < 0) { | ||
anchor2 = add(a, wholeMonthDiff - 1, 'months'); // linear across the month | ||
if (b - anchor < 0) { | ||
anchor2 = add(a, wholeMonthDiff - 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor - anchor2); | ||
} else { | ||
anchor2 = add(a, wholeMonthDiff + 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor2 - anchor); | ||
} | ||
adjust = (b - anchor) / (anchor - anchor2); | ||
} else { | ||
anchor2 = add(a, wholeMonthDiff + 1, 'months'); // linear across the month | ||
//check for negative zero, return zero if negative zero | ||
return -(wholeMonthDiff + adjust) || 0; | ||
adjust = (b - anchor) / (anchor2 - anchor); | ||
} //check for negative zero, return zero if negative zero | ||
return -(wholeMonthDiff + adjust) || 0; | ||
} | ||
exports.min = min; | ||
exports.max = max; | ||
exports.add = add; | ||
exports.diff = diff; | ||
exports.endOf = endOf; | ||
exports.isAfter = isAfter; | ||
@@ -334,6 +343,5 @@ exports.isBefore = isBefore; | ||
exports.isSameOrBefore = isSameOrBefore; | ||
exports.add = add; | ||
exports.max = max; | ||
exports.min = min; | ||
exports.startOf = startOf; | ||
exports.subtract = subtract; | ||
exports.startOf = startOf; | ||
exports.endOf = endOf; | ||
exports.diff = diff; |
@@ -1,4 +0,4 @@ | ||
var isValidDate = function (input) { | ||
return input instanceof Date && !isNaN(input.getTime()); | ||
}; | ||
function isValidDate (input) { | ||
return input instanceof Date && !isNaN(input.getTime()); | ||
} | ||
@@ -8,4 +8,4 @@ const aliases = {}; | ||
function addUnitAlias(unit, shorthand) { | ||
var lowerCase = unit.toLowerCase(); | ||
aliases[lowerCase] = aliases[lowerCase + 's'] = aliases[shorthand] = unit; | ||
var lowerCase = unit.toLowerCase(); | ||
aliases[lowerCase] = aliases[lowerCase + 's'] = aliases[shorthand] = unit; | ||
} | ||
@@ -29,88 +29,88 @@ | ||
addUnitAlias('millisecond', 'ms'); | ||
function normalizeUnits(units) { | ||
return typeof units === 'string' ? aliases[units] || aliases[units.toLowerCase()] : undefined; | ||
return typeof units === 'string' ? aliases[units] || aliases[units.toLowerCase()] : undefined; | ||
} | ||
function setMonth(date, month) { | ||
const dayOfMonth = Math.min(date.getDate(), daysInMonth(date.getFullYear(), month)); | ||
date.setMonth(month, dayOfMonth); | ||
const dayOfMonth = Math.min(date.getDate(), daysInMonth(date.getFullYear(), month)); | ||
date.setMonth(month, dayOfMonth); | ||
} | ||
function daysInMonth(year, month) { | ||
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); | ||
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); | ||
} | ||
function setDayOfWeek(date, input) { | ||
const day = date.getDay(); | ||
date.setDate(date.getDate() + input - day); | ||
const day = date.getDay(); | ||
date.setDate(date.getDate() + input - day); | ||
} | ||
function Duration(duration) { | ||
var years = duration.year || 0, | ||
quarters = duration.quarter || 0, | ||
months = duration.month || 0, | ||
weeks = duration.week || 0, | ||
days = duration.day || 0, | ||
hours = duration.hour || 0, | ||
minutes = duration.minute || 0, | ||
seconds = duration.second || 0, | ||
milliseconds = duration.millisecond || 0; | ||
var years = duration.year || 0, | ||
quarters = duration.quarter || 0, | ||
months = duration.month || 0, | ||
weeks = duration.week || 0, | ||
days = duration.day || 0, | ||
hours = duration.hour || 0, | ||
minutes = duration.minute || 0, | ||
seconds = duration.second || 0, | ||
milliseconds = duration.millisecond || 0; // representation for dateAddRemove | ||
// representation for dateAddRemove | ||
this._milliseconds = +milliseconds + seconds * 1e3 + // 1000 | ||
minutes * 6e4 + // 1000 * 60 | ||
hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978 | ||
// Because of dateAddRemove treats 24 hours as different from a | ||
// day when working around DST, we need to store them separately | ||
this._days = +days + weeks * 7; | ||
// It is impossible translate months into days without knowing | ||
// which months you are are talking about, so we have to store | ||
// it separately. | ||
this._months = +months + quarters * 3 + years * 12; | ||
this._milliseconds = +milliseconds + seconds * 1e3 + // 1000 | ||
minutes * 6e4 + // 1000 * 60 | ||
hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978 | ||
// Because of dateAddRemove treats 24 hours as different from a | ||
// day when working around DST, we need to store them separately | ||
this._days = +days + weeks * 7; // It is impossible translate months into days without knowing | ||
// which months you are are talking about, so we have to store | ||
// it separately. | ||
this._months = +months + quarters * 3 + years * 12; | ||
} | ||
function createDuration(input, key) { | ||
const duration = {}; | ||
if (key) { | ||
duration[normalizeUnits(key)] = input; | ||
} else { | ||
duration.millisecond = input; | ||
} | ||
const duration = {}; | ||
return new Duration(duration); | ||
if (key) { | ||
duration[normalizeUnits(key)] = input; | ||
} else { | ||
duration.millisecond = input; | ||
} | ||
return new Duration(duration); | ||
} | ||
function absRound(number) { | ||
if (number < 0) { | ||
return Math.round(-1 * number) * -1; | ||
} else { | ||
return Math.round(number); | ||
} | ||
if (number < 0) { | ||
return Math.round(-1 * number) * -1; | ||
} else { | ||
return Math.round(number); | ||
} | ||
} | ||
function createAdder(direction) { | ||
return function (date, val, period) { | ||
const duration = createDuration(val, period); | ||
return addSubtract(date, duration, direction); | ||
}; | ||
return function (date, val, period) { | ||
const duration = createDuration(val, period); | ||
return addSubtract(date, duration, direction); | ||
}; | ||
} | ||
function addSubtract(original, duration, isAdding) { | ||
const date = new Date(original.valueOf()); | ||
const milliseconds = duration._milliseconds; | ||
const days = absRound(duration._days); | ||
const months = absRound(duration._months); | ||
const date = new Date(original.valueOf()); | ||
const milliseconds = duration._milliseconds; | ||
const days = absRound(duration._days); | ||
const months = absRound(duration._months); | ||
if (milliseconds) { | ||
date.setTime(date.valueOf() + milliseconds * isAdding); | ||
} | ||
if (days) { | ||
date.setDate(date.getDate() + days * isAdding); | ||
//set(mom, 'Date', get(mom, 'Date') + days * isAdding); | ||
} | ||
if (months) { | ||
setMonth(date, date.getMonth() + months * isAdding); | ||
} | ||
return date; | ||
if (milliseconds) { | ||
date.setTime(date.valueOf() + milliseconds * isAdding); | ||
} | ||
if (days) { | ||
date.setDate(date.getDate() + days * isAdding); //set(mom, 'Date', get(mom, 'Date') + days * isAdding); | ||
} | ||
if (months) { | ||
setMonth(date, date.getMonth() + months * isAdding); | ||
} | ||
return date; | ||
} | ||
@@ -122,200 +122,208 @@ | ||
function startOf(date, units) { | ||
units = normalizeUnits(units); | ||
const modified = new Date(date.valueOf()); | ||
// the following switch intentionally omits break keywords | ||
// to utilize falling through the cases. | ||
switch (units) { | ||
case 'year': | ||
setMonth(modified, 0); | ||
/* falls through */ | ||
case 'quarter': | ||
case 'month': | ||
modified.setDate(1); | ||
/* falls through */ | ||
case 'week': | ||
case 'day': | ||
case 'date': | ||
modified.setHours(0); | ||
/* falls through */ | ||
case 'hour': | ||
modified.setMinutes(0); | ||
/* falls through */ | ||
case 'minute': | ||
modified.setSeconds(0); | ||
/* falls through */ | ||
case 'second': | ||
modified.setMilliseconds(0); | ||
} | ||
units = normalizeUnits(units); | ||
const modified = new Date(date.valueOf()); // the following switch intentionally omits break keywords | ||
// to utilize falling through the cases. | ||
// weeks are a special case | ||
if (units === 'week') { | ||
setDayOfWeek(modified, 1); | ||
} | ||
switch (units) { | ||
case 'year': | ||
setMonth(modified, 0); | ||
// quarters are also special | ||
if (units === 'quarter') { | ||
setMonth(modified, Math.floor(modified.getMonth() / 3) * 3); | ||
} | ||
/* falls through */ | ||
return modified; | ||
case 'quarter': | ||
case 'month': | ||
modified.setDate(1); | ||
/* falls through */ | ||
case 'week': | ||
case 'day': | ||
case 'date': | ||
modified.setHours(0); | ||
/* falls through */ | ||
case 'hour': | ||
modified.setMinutes(0); | ||
/* falls through */ | ||
case 'minute': | ||
modified.setSeconds(0); | ||
/* falls through */ | ||
case 'second': | ||
modified.setMilliseconds(0); | ||
} // weeks are a special case | ||
if (units === 'week') { | ||
setDayOfWeek(modified, 1); | ||
} // quarters are also special | ||
if (units === 'quarter') { | ||
setMonth(modified, Math.floor(modified.getMonth() / 3) * 3); | ||
} | ||
return modified; | ||
} | ||
function endOf(date, units) { | ||
units = normalizeUnits(units); | ||
if (units === undefined || units === 'millisecond') { | ||
return new Date(date.valueOf()); | ||
} | ||
units = normalizeUnits(units); | ||
// 'date' is an alias for 'day', so it should be considered as such. | ||
if (units === 'date') { | ||
units = 'day'; | ||
} | ||
if (units === undefined || units === 'millisecond') { | ||
return new Date(date.valueOf()); | ||
} // 'date' is an alias for 'day', so it should be considered as such. | ||
let modified = startOf(date, units); | ||
modified = add(modified, 1, units); | ||
modified = subtract(modified, 1, 'ms'); | ||
return modified; | ||
if (units === 'date') { | ||
units = 'day'; | ||
} | ||
let modified = startOf(date, units); | ||
modified = add(modified, 1, units); | ||
modified = subtract(modified, 1, 'ms'); | ||
return modified; | ||
} | ||
function isAfter(first, second, units) { | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first > second; | ||
} else { | ||
return second.valueOf() < startOf(first, units).valueOf(); | ||
} | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first > second; | ||
} else { | ||
return second.valueOf() < startOf(first, units).valueOf(); | ||
} | ||
} | ||
function isBefore(first, second, units) { | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
function isBefore(first, second, units) { | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first < second; | ||
} else { | ||
return endOf(first, units).valueOf() < second.valueOf(); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return first < second; | ||
} else { | ||
return endOf(first, units).valueOf() < second.valueOf(); | ||
} | ||
} | ||
function isBetween(date, from, to, units, inclusivity) { | ||
inclusivity = inclusivity || '()'; | ||
return (inclusivity[0] === '(' ? isAfter(date, from, units) : !isBefore(date, from, units)) && (inclusivity[1] === ')' ? isBefore(date, to, units) : !isAfter(date, to, units)); | ||
inclusivity = inclusivity || '()'; | ||
return (inclusivity[0] === '(' ? isAfter(date, from, units) : !isBefore(date, from, units)) && (inclusivity[1] === ')' ? isBefore(date, to, units) : !isAfter(date, to, units)); | ||
} | ||
function isSame(date, input, units) { | ||
if (!isValidDate(date) || !isValidDate(input)) { | ||
throw new Error('Invalid date'); | ||
} | ||
function isSame(date, input, units) { | ||
if (!isValidDate(date) || !isValidDate(input)) { | ||
throw new Error('Invalid date'); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return date.getTime() === input.getTime(); | ||
} else { | ||
const inputMs = input.valueOf(); | ||
return startOf(date, units).valueOf() <= inputMs && inputMs <= endOf(date, units).valueOf(); | ||
} | ||
units = normalizeUnits(units || 'millisecond'); | ||
if (units === 'millisecond') { | ||
return date.getTime() === input.getTime(); | ||
} else { | ||
const inputMs = input.valueOf(); | ||
return startOf(date, units).valueOf() <= inputMs && inputMs <= endOf(date, units).valueOf(); | ||
} | ||
} | ||
function isSameOrAfter(date, input, units) { | ||
return isSame(date, input, units) || isAfter(date, input, units); | ||
return isSame(date, input, units) || isAfter(date, input, units); | ||
} | ||
function isSameOrBefore(date, input, units) { | ||
return isSame(date, input, units) || isBefore(date, input, units); | ||
return isSame(date, input, units) || isBefore(date, input, units); | ||
} | ||
function pickBy(fn, moments) { | ||
var res, i; | ||
if (!moments.length) { | ||
return new Date(); | ||
} | ||
res = moments[0]; | ||
for (i = 1; i < moments.length; ++i) { | ||
if (!isValidDate(moments[i])) { | ||
throw new Error('Invalid date'); | ||
} else if (fn(moments[i], res)) { | ||
res = moments[i]; | ||
} | ||
} | ||
return res; | ||
} | ||
var res, i; | ||
function min() { | ||
for (var _len = arguments.length, dates = Array(_len), _key = 0; _key < _len; _key++) { | ||
dates[_key] = arguments[_key]; | ||
} | ||
if (!moments.length) { | ||
return new Date(); | ||
} | ||
return pickBy(isBefore, dates); | ||
} | ||
res = moments[0]; | ||
function max() { | ||
for (var _len2 = arguments.length, dates = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
dates[_key2] = arguments[_key2]; | ||
for (i = 1; i < moments.length; ++i) { | ||
if (!isValidDate(moments[i])) { | ||
throw new Error('Invalid date'); | ||
} else if (fn(moments[i], res)) { | ||
res = moments[i]; | ||
} | ||
} | ||
return pickBy(isAfter, dates); | ||
return res; | ||
} | ||
function min(...dates) { | ||
return pickBy(isBefore, dates); | ||
} | ||
function max(...dates) { | ||
return pickBy(isAfter, dates); | ||
} | ||
function diff(first, second, units, asFloat) { | ||
let output; | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
let output; | ||
units = normalizeUnits(units); | ||
if (!isValidDate(first) || !isValidDate(second)) { | ||
throw new Error('Invalid date'); | ||
} | ||
if (units === 'year' || units === 'month' || units === 'quarter') { | ||
output = monthDiff(first, second); | ||
if (units === 'quarter') { | ||
output = output / 3; | ||
} else if (units === 'year') { | ||
output = output / 12; | ||
} | ||
} else { | ||
const delta = first - second; | ||
output = units === 'second' ? delta / 1e3 : // 1000 | ||
units === 'minute' ? delta / 6e4 : // 1000 * 60 | ||
units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60 | ||
units === 'day' ? delta / 864e5 : // 1000 * 60 * 60 * 24, negate dst | ||
units === 'week' ? delta / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst | ||
delta; | ||
units = normalizeUnits(units); | ||
if (units === 'year' || units === 'month' || units === 'quarter') { | ||
output = monthDiff(first, second); | ||
if (units === 'quarter') { | ||
output = output / 3; | ||
} else if (units === 'year') { | ||
output = output / 12; | ||
} | ||
return asFloat ? output : absFloor(output); | ||
} else { | ||
const delta = first - second; | ||
output = units === 'second' ? delta / 1e3 : // 1000 | ||
units === 'minute' ? delta / 6e4 : // 1000 * 60 | ||
units === 'hour' ? delta / 36e5 : // 1000 * 60 * 60 | ||
units === 'day' ? delta / 864e5 : // 1000 * 60 * 60 * 24, negate dst | ||
units === 'week' ? delta / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst | ||
delta; | ||
} | ||
return asFloat ? output : absFloor(output); | ||
} | ||
function absFloor(number) { | ||
if (number < 0) { | ||
// -0 -> 0 | ||
return Math.ceil(number) || 0; | ||
} else { | ||
return Math.floor(number); | ||
} | ||
if (number < 0) { | ||
// -0 -> 0 | ||
return Math.ceil(number) || 0; | ||
} else { | ||
return Math.floor(number); | ||
} | ||
} | ||
function monthDiff(a, b) { | ||
// difference in months | ||
var wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth()), | ||
// difference in months | ||
var wholeMonthDiff = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth()), | ||
// b is in (anchor - 1 month, anchor + 1 month) | ||
anchor = add(a, wholeMonthDiff, 'months'), | ||
anchor2, | ||
adjust; | ||
// b is in (anchor - 1 month, anchor + 1 month) | ||
anchor = add(a, wholeMonthDiff, 'months'), | ||
anchor2, | ||
adjust; | ||
if (b - anchor < 0) { | ||
anchor2 = add(a, wholeMonthDiff - 1, 'months'); // linear across the month | ||
if (b - anchor < 0) { | ||
anchor2 = add(a, wholeMonthDiff - 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor - anchor2); | ||
} else { | ||
anchor2 = add(a, wholeMonthDiff + 1, 'months'); | ||
// linear across the month | ||
adjust = (b - anchor) / (anchor2 - anchor); | ||
} | ||
adjust = (b - anchor) / (anchor - anchor2); | ||
} else { | ||
anchor2 = add(a, wholeMonthDiff + 1, 'months'); // linear across the month | ||
//check for negative zero, return zero if negative zero | ||
return -(wholeMonthDiff + adjust) || 0; | ||
adjust = (b - anchor) / (anchor2 - anchor); | ||
} //check for negative zero, return zero if negative zero | ||
return -(wholeMonthDiff + adjust) || 0; | ||
} | ||
export { min, max, isAfter, isBefore, isBetween, isSame, isSameOrAfter, isSameOrBefore, add, subtract, startOf, endOf, diff }; | ||
export { add, diff, endOf, isAfter, isBefore, isBetween, isSame, isSameOrAfter, isSameOrBefore, max, min, startOf, subtract }; |
@@ -5,3 +5,3 @@ { | ||
"repository": "https://github.com/guardian/compare-dates", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"license": "Apache-2.0", | ||
@@ -13,3 +13,3 @@ "main": "dist/compare.cjs.js", | ||
"test": "tap test", | ||
"build": "rollup -c rollup.config.cjs.js && rollup -c rollup.config.es6.js", | ||
"build": "rollup -c", | ||
"prepublish": "npm test && npm run build" | ||
@@ -21,16 +21,15 @@ }, | ||
"devDependencies": { | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", | ||
"babel-plugin-transform-es2015-parameters": "^6.24.1", | ||
"babel-plugin-transform-es2015-spread": "^6.22.0", | ||
"babel-register": "^6.24.1", | ||
"eslint": "^4.2.0", | ||
"rollup": "^0.45.2", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-commonjs": "^8.0.2", | ||
"rollup-plugin-node-resolve": "^3.0.0", | ||
"tap": "^10.7.0" | ||
"@babel/core": "7.4.0", | ||
"@babel/plugin-transform-modules-commonjs": "7.4.0", | ||
"@babel/register": "7.4.0", | ||
"eslint": "^5.15.3", | ||
"rollup": "^1.7.0", | ||
"rollup-plugin-babel": "^4.3.2", | ||
"rollup-plugin-commonjs": "^9.2.1", | ||
"rollup-plugin-node-resolve": "^4.0.1", | ||
"tap": "^12.6.1" | ||
}, | ||
"engines": { | ||
"node": ">= 4.3" | ||
"node": ">= 8.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
21235
532