Skip to content

Commit

Permalink
Migrate differenceInBusinessDays function to TS (#2414)
Browse files Browse the repository at this point in the history
Fixed: `differenceInBusinessDays` now returns `NaN` instead of `Invalid Date` when an invalid argument is passed to the function.
  • Loading branch information
maximtop committed May 5, 2021
1 parent 65aa030 commit fc70c22
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
Expand Up @@ -33,22 +33,22 @@ import requiredArgs from '../_lib/requiredArgs/index'
* //=> 136
*/
export default function differenceInBusinessDays(
dirtyDateLeft,
dirtyDateRight
) {
dirtyDateLeft: Date | number,
dirtyDateRight: Date | number
): number {
requiredArgs(2, arguments)

var dateLeft = toDate(dirtyDateLeft)
var dateRight = toDate(dirtyDateRight)
const dateLeft = toDate(dirtyDateLeft)
let dateRight = toDate(dirtyDateRight)

if (!isValid(dateLeft) || !isValid(dateRight)) return new Date(NaN)
if (!isValid(dateLeft) || !isValid(dateRight)) return NaN

var calendarDifference = differenceInCalendarDays(dateLeft, dateRight)
var sign = calendarDifference < 0 ? -1 : 1
const calendarDifference = differenceInCalendarDays(dateLeft, dateRight)
const sign = calendarDifference < 0 ? -1 : 1

var weeks = toInteger(calendarDifference / 7)
const weeks = toInteger(calendarDifference / 7)

var result = weeks * 5
let result = weeks * 5
dateRight = addDays(dateRight, weeks * 7)

// the loop below will run at most 6 times to account for the remaining days that don't makeup a full week
Expand Down
@@ -1,138 +1,139 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import differenceInBusinessDays from '.'

describe('differenceInBusinessDays', function() {
it('returns the number of business days between the given dates, excluding weekends', function() {
var result = differenceInBusinessDays(
describe('differenceInBusinessDays', function () {
it('returns the number of business days between the given dates, excluding weekends', function () {
const result = differenceInBusinessDays(
new Date(2014, 6 /* Jul */, 18),
new Date(2014, 0 /* Jan */, 10)
)
assert(result === 135)
})

it('can handle long ranges', function() {
it('can handle long ranges', function () {
// @ts-ignore
if (typeof this.timeout === 'function') {
// @ts-ignore
this.timeout(500 /* 500 ms test timeout */)
}
var result = differenceInBusinessDays(
const result = differenceInBusinessDays(
new Date(15000, 0 /* Jan */, 1),
new Date(2014, 0 /* Jan */, 1)
)
assert(result === 3387885)
})

it('the same except given first date falls on a weekend', function() {
var result = differenceInBusinessDays(
it('the same except given first date falls on a weekend', function () {
const result = differenceInBusinessDays(
new Date(2019, 6 /* Jul */, 20),
new Date(2019, 6 /* Jul */, 18)
)
assert(result === 2)
})

it('the same except given second date falls on a weekend', function() {
var result = differenceInBusinessDays(
it('the same except given second date falls on a weekend', function () {
const result = differenceInBusinessDays(
new Date(2019, 6 /* Jul */, 23),
new Date(2019, 6 /* Jul */, 20)
)
assert(result === 1)
})

it('the same except both given dates fall on a weekend', function() {
var result = differenceInBusinessDays(
it('the same except both given dates fall on a weekend', function () {
const result = differenceInBusinessDays(
new Date(2019, 6 /* Jul */, 28),
new Date(2019, 6 /* Jul */, 20)
)
assert(result === 5)
})

it('returns a negative number if the time value of the first date is smaller', function() {
var result = differenceInBusinessDays(
it('returns a negative number if the time value of the first date is smaller', function () {
const result = differenceInBusinessDays(
new Date(2014, 0 /* Jan */, 10),
new Date(2014, 6 /* Jul */, 20)
)
assert(result === -135)
})

it('accepts timestamps', function() {
var result = differenceInBusinessDays(
it('accepts timestamps', function () {
const result = differenceInBusinessDays(
new Date(2014, 6, 18).getTime(),
new Date(2014, 0, 10).getTime()
)
assert(result === 135)
})

describe('edge cases', function() {
it('the difference is less than a day, but the given dates are in different calendar days', function() {
var result = differenceInBusinessDays(
describe('edge cases', function () {
it('the difference is less than a day, but the given dates are in different calendar days', function () {
const result = differenceInBusinessDays(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 4, 23, 59)
)
assert(result === 1)
})

it('the same for the swapped dates', function() {
var result = differenceInBusinessDays(
it('the same for the swapped dates', function () {
const result = differenceInBusinessDays(
new Date(2014, 8 /* Sep */, 4, 23, 59),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)
assert(result === -1)
})

it('the time values of the given dates are the same', function() {
var result = differenceInBusinessDays(
it('the time values of the given dates are the same', function () {
const result = differenceInBusinessDays(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 4, 0, 0)
)
assert(result === 1)
})

it('the given dates are the same', function() {
var result = differenceInBusinessDays(
it('the given dates are the same', function () {
const result = differenceInBusinessDays(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero(x) {
function isNegativeZero(x: number) {
return x === 0 && 1 / x < 0
}

var result = differenceInBusinessDays(
const result = differenceInBusinessDays(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
const resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})

it('returns NaN if the first date is `Invalid Date`', function() {
var result = differenceInBusinessDays(
it('returns NaN if the first date is `Invalid Date`', function () {
const result = differenceInBusinessDays(
new Date(NaN),
new Date(2017, 0 /* Jan */, 1)
)
assert(isNaN(result))
})

it('returns NaN if the second date is `Invalid Date`', function() {
var result = differenceInBusinessDays(
it('returns NaN if the second date is `Invalid Date`', function () {
const result = differenceInBusinessDays(
new Date(2017, 0 /* Jan */, 1),
new Date(NaN)
)
assert(isNaN(result))
})

it('returns NaN if the both dates are `Invalid Date`', function() {
var result = differenceInBusinessDays(new Date(NaN), new Date(NaN))
it('returns NaN if the both dates are `Invalid Date`', function () {
const result = differenceInBusinessDays(new Date(NaN), new Date(NaN))
assert(isNaN(result))
})

it('throws TypeError exception if passed less than 2 arguments', function() {
it('throws TypeError exception if passed less than 2 arguments', function () {
assert.throws(differenceInBusinessDays.bind(null), TypeError)
assert.throws(differenceInBusinessDays.bind(null, 1), TypeError)
})
Expand Down

0 comments on commit fc70c22

Please sign in to comment.