Skip to content

Commit

Permalink
Migrate differenceInCalendarMonths function to TS (#2419)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximtop committed May 5, 2021
1 parent 48d77d9 commit b01ed51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ import requiredArgs from '../_lib/requiredArgs/index'
* //=> 8
*/
export default function differenceInCalendarMonths(
dirtyDateLeft,
dirtyDateRight
) {
dirtyDateLeft: Date | number,
dirtyDateRight: Date | number
): number {
requiredArgs(2, arguments)

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

var yearDiff = dateLeft.getFullYear() - dateRight.getFullYear()
var monthDiff = dateLeft.getMonth() - dateRight.getMonth()
const yearDiff = dateLeft.getFullYear() - dateRight.getFullYear()
const monthDiff = dateLeft.getMonth() - dateRight.getMonth()

return yearDiff * 12 + monthDiff
}
Original file line number Diff line number Diff line change
@@ -1,104 +1,103 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import differenceInCalendarMonths from '.'

describe('differenceInCalendarMonths', function() {
it('returns the number of calendar months between the given dates', function() {
var result = differenceInCalendarMonths(
describe('differenceInCalendarMonths', function () {
it('returns the number of calendar months between the given dates', function () {
const result = differenceInCalendarMonths(
new Date(2012, 6 /* Jul */, 2, 18, 0),
new Date(2011, 6 /* Jul */, 2, 6, 0)
)
assert(result === 12)
})

it('returns a negative number if the time value of the first date is smaller', function() {
var result = differenceInCalendarMonths(
it('returns a negative number if the time value of the first date is smaller', function () {
const result = differenceInCalendarMonths(
new Date(2011, 6 /* Jul */, 2, 6, 0),
new Date(2012, 6 /* Jul */, 2, 18, 0)
)
assert(result === -12)
})

it('accepts timestamps', function() {
var result = differenceInCalendarMonths(
it('accepts timestamps', function () {
const result = differenceInCalendarMonths(
new Date(2014, 7 /* Aug */, 2).getTime(),
new Date(2010, 6 /* Jul */, 2).getTime()
)
assert(result === 49)
})

describe('edge cases', function() {
it('the difference is less than a month, but the given dates are in different calendar months', function() {
var result = differenceInCalendarMonths(
describe('edge cases', function () {
it('the difference is less than a month, but the given dates are in different calendar months', function () {
const result = differenceInCalendarMonths(
new Date(2014, 8 /* Sep */, 1),
new Date(2014, 7 /* Aug */, 31)
)
assert(result === 1)
})

it('the same for the swapped dates', function() {
var result = differenceInCalendarMonths(
it('the same for the swapped dates', function () {
const result = differenceInCalendarMonths(
new Date(2014, 7 /* Aug */, 31),
new Date(2014, 8 /* Sep */, 1)
)
assert(result === -1)
})

it('the days of months of the given dates are the same', function() {
var result = differenceInCalendarMonths(
it('the days of months of the given dates are the same', function () {
const result = differenceInCalendarMonths(
new Date(2014, 8 /* Sep */, 6),
new Date(2014, 7 /* Aug */, 6)
)
assert(result === 1)
})

it('the given dates are the same', function() {
var result = differenceInCalendarMonths(
it('the given dates are the same', function () {
const result = differenceInCalendarMonths(
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): boolean {
return x === 0 && 1 / x < 0
}

var result = differenceInCalendarMonths(
const result = differenceInCalendarMonths(
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 = differenceInCalendarMonths(
it('returns NaN if the first date is `Invalid Date`', function () {
const result = differenceInCalendarMonths(
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 = differenceInCalendarMonths(
it('returns NaN if the second date is `Invalid Date`', function () {
const result = differenceInCalendarMonths(
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 = differenceInCalendarMonths(new Date(NaN), new Date(NaN))
it('returns NaN if the both dates are `Invalid Date`', function () {
const result = differenceInCalendarMonths(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(differenceInCalendarMonths.bind(null), TypeError)
assert.throws(differenceInCalendarMonths.bind(null, 1), TypeError)
})
Expand Down

0 comments on commit b01ed51

Please sign in to comment.