Skip to content

Commit

Permalink
Invalid Date fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leshakoss committed Mar 10, 2017
1 parent 0de233a commit a68a584
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/areIntervalsOverlapping/index.js
Expand Up @@ -13,6 +13,7 @@ import toDate from '../toDate/index.js'
* @param {Options} [options] - the object with options. See [Options]{@link docs/types/Options}
* @returns {Boolean} whether the time intervals are overlapping
* @throws {Error} The start of an interval cannot be after its end
* @throws {Error} Date in interval cannot be `Invalid Date`
*
* @example
* // For overlapping time intervals:
Expand All @@ -36,8 +37,9 @@ export default function areIntervalsOverlapping (dirtyIntervalLeft, dirtyInterva
var rightStartTime = toDate(dirtyIntervalRight.start, dirtyOptions).getTime()
var rightEndTime = toDate(dirtyIntervalRight.end, dirtyOptions).getTime()

if (leftStartTime > leftEndTime || rightStartTime > rightEndTime) {
throw new Error('The start of an interval cannot be after its end')
// Throw an exception if start date is after end date or if any date is `Invalid Date`
if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
throw new Error('Invalid interval')
}

return leftStartTime < rightEndTime && rightStartTime < leftEndTime
Expand Down
12 changes: 12 additions & 0 deletions src/closestIndexTo/index.js
Expand Up @@ -26,13 +26,25 @@ import toDate from '../toDate/index.js'
*/
export default function closestIndexTo (dirtyDateToCompare, dirtyDatesArray, dirtyOptions) {
var dateToCompare = toDate(dirtyDateToCompare, dirtyOptions)

if (isNaN(dateToCompare)) {
return NaN
}

var timeToCompare = dateToCompare.getTime()

var result
var minDistance

dirtyDatesArray.forEach(function (dirtyDate, index) {
var currentDate = toDate(dirtyDate, dirtyOptions)

if (isNaN(currentDate)) {
result = NaN
minDistance = NaN
return
}

var distance = Math.abs(timeToCompare - currentDate.getTime())
if (result === undefined || distance < minDistance) {
result = index
Expand Down
12 changes: 12 additions & 0 deletions src/closestTo/index.js
Expand Up @@ -24,13 +24,25 @@ import toDate from '../toDate/index.js'
*/
export default function closestTo (dirtyDateToCompare, dirtyDatesArray, dirtyOptions) {
var dateToCompare = toDate(dirtyDateToCompare, dirtyOptions)

if (isNaN(dateToCompare)) {
return new Date(NaN)
}

var timeToCompare = dateToCompare.getTime()

var result
var minDistance

dirtyDatesArray.forEach(function (dirtyDate) {
var currentDate = toDate(dirtyDate, dirtyOptions)

if (isNaN(currentDate)) {
result = new Date(NaN)
minDistance = NaN
return
}

var distance = Math.abs(timeToCompare - currentDate.getTime())
if (result === undefined || distance < minDistance) {
result = currentDate
Expand Down
11 changes: 6 additions & 5 deletions src/compareAsc/index.js
Expand Up @@ -37,15 +37,16 @@ import toDate from '../toDate/index.js'
*/
export default function compareAsc (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var timeLeft = dateLeft.getTime()
var dateRight = toDate(dirtyDateRight, dirtyOptions)
var timeRight = dateRight.getTime()

if (timeLeft < timeRight) {
var diff = dateLeft.getTime() - dateRight.getTime()

if (diff < 0) {
return -1
} else if (timeLeft > timeRight) {
} else if (diff > 0) {
return 1
// Return 0 if diff is 0; return NaN if diff is NaN
} else {
return 0
return diff
}
}
11 changes: 6 additions & 5 deletions src/compareDesc/index.js
Expand Up @@ -37,15 +37,16 @@ import toDate from '../toDate/index.js'
*/
export default function compareDesc (dirtyDateLeft, dirtyDateRight, dirtyOptions) {
var dateLeft = toDate(dirtyDateLeft, dirtyOptions)
var timeLeft = dateLeft.getTime()
var dateRight = toDate(dirtyDateRight, dirtyOptions)
var timeRight = dateRight.getTime()

if (timeLeft > timeRight) {
var diff = dateLeft.getTime() - dateRight.getTime()

if (diff > 0) {
return -1
} else if (timeLeft < timeRight) {
} else if (diff < 0) {
return 1
// Return 0 if diff is 0; return NaN if diff is NaN
} else {
return 0
return diff
}
}
4 changes: 4 additions & 0 deletions src/distanceInWords/index.js
Expand Up @@ -97,6 +97,10 @@ export default function distanceInWords (dirtyDateToCompare, dirtyDate, dirtyOpt

var comparison = compareDesc(dirtyDateToCompare, dirtyDate, options)

if (isNaN(comparison)) {
return 'Invalid Date'
}

var locale = options.locale
var localize = enLocale.distanceInWords.localize
if (locale && locale.distanceInWords && locale.distanceInWords.localize) {
Expand Down
4 changes: 4 additions & 0 deletions src/distanceInWordsStrict/index.js
Expand Up @@ -97,6 +97,10 @@ export default function distanceInWordsStrict (dirtyDateToCompare, dirtyDate, di

var comparison = compareDesc(dirtyDateToCompare, dirtyDate, options)

if (isNaN(comparison)) {
return 'Invalid Date'
}

var locale = options.locale
var localize = enLocale.distanceInWords.localize
if (locale && locale.distanceInWords && locale.distanceInWords.localize) {
Expand Down
6 changes: 4 additions & 2 deletions src/eachDayOfInterval/index.js
Expand Up @@ -12,6 +12,7 @@ import toDate from '../toDate/index.js'
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @returns {Date[]} the array with starts of days from the day of the interval start to the day of the interval end
* @throws {Error} The start of an interval cannot be after its end
* @throws {Error} Date in interval cannot be `Invalid Date`
*
* @example
* // Each day between 6 October 2014 and 10 October 2014:
Expand All @@ -33,8 +34,9 @@ export default function eachDayOfInterval (dirtyInterval, dirtyOptions) {

var endTime = endDate.getTime()

if (startDate.getTime() > endTime) {
throw new Error('The start of an interval cannot be after its end')
// Throw an exception if start date is after end date or if any date is `Invalid Date`
if (!(startDate.getTime() <= endTime)) {
throw new Error('Invalid interval')
}

var dates = []
Expand Down
9 changes: 8 additions & 1 deletion src/getDaysInYear/index.js
@@ -1,3 +1,4 @@
import toDate from '../toDate/index.js'
import isLeapYear from '../isLeapYear/index.js'

/**
Expand All @@ -18,5 +19,11 @@ import isLeapYear from '../isLeapYear/index.js'
* //=> 366
*/
export default function getDaysInYear (dirtyDate, dirtyOptions) {
return isLeapYear(dirtyDate, dirtyOptions) ? 366 : 365
var date = toDate(dirtyDate, dirtyOptions)

if (isNaN(date)) {
return NaN
}

return isLeapYear(date, dirtyOptions) ? 366 : 365
}
6 changes: 4 additions & 2 deletions src/getOverlappingDaysInIntervals/index.js
Expand Up @@ -15,6 +15,7 @@ var MILLISECONDS_IN_DAY = 24 * 60 * 60 * 1000
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @returns {Number} the number of days that overlap in two time intervals
* @throws {Error} The start of an interval cannot be after its end
* @throws {Error} Date in interval cannot be `Invalid Date`
*
* @example
* // For overlapping time intervals adds 1 for each started overlapping day:
Expand All @@ -38,8 +39,9 @@ export default function getOverlappingDaysInIntervals (dirtyIntervalLeft, dirtyI
var rightStartTime = toDate(dirtyIntervalRight.start, dirtyOptions).getTime()
var rightEndTime = toDate(dirtyIntervalRight.end, dirtyOptions).getTime()

if (leftStartTime > leftEndTime || rightStartTime > rightEndTime) {
throw new Error('The start of an interval cannot be after its end')
// Throw an exception if start date is after end date or if any date is `Invalid Date`
if (!(leftStartTime <= leftEndTime && rightStartTime <= rightEndTime)) {
throw new Error('Invalid interval')
}

var isOverlapping = leftStartTime < rightEndTime && rightStartTime < leftEndTime
Expand Down
6 changes: 4 additions & 2 deletions src/isWithinInterval/index.js
Expand Up @@ -13,6 +13,7 @@ import toDate from '../toDate/index.js'
* @param {Options} [options] - the object with options. See [Options]{@link docs/Options}
* @returns {Boolean} the date is within the interval
* @throws {Error} The start of an interval cannot be after its end
* @throws {Error} Date in interval cannot be `Invalid Date`
*
* @example
* // For the date within the interval:
Expand All @@ -35,8 +36,9 @@ export default function isWithinInterval (dirtyDate, dirtyInterval, dirtyOptions
var startTime = toDate(dirtyInterval.start, dirtyOptions).getTime()
var endTime = toDate(dirtyInterval.end, dirtyOptions).getTime()

if (startTime > endTime) {
throw new Error('The start of an interval cannot be after its end')
// Throw an exception if start date is after end date or if any date is `Invalid Date`
if (!(startTime <= endTime)) {
throw new Error('Invalid interval')
}

return time >= startTime && time <= endTime
Expand Down
6 changes: 6 additions & 0 deletions src/setYear/index.js
Expand Up @@ -21,6 +21,12 @@ import toDate from '../toDate/index.js'
export default function setYear (dirtyDate, dirtyYear, dirtyOptions) {
var date = toDate(dirtyDate, dirtyOptions)
var year = Number(dirtyYear)

// Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
if (isNaN(date)) {
return new Date(NaN)
}

date.setFullYear(year)
return date
}

0 comments on commit a68a584

Please sign in to comment.