Skip to content

Commit

Permalink
Migrate isWithinInterval to TS (#2394)
Browse files Browse the repository at this point in the history
Co-authored-by: Sasha Koss <koss@nocorp.me>
  • Loading branch information
a-korzun and kossnocorp committed Apr 14, 2021
1 parent 2860a54 commit 8c6238e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/isWithinInterval/index.js → src/isWithinInterval/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import toDate from '../toDate/index'
import requiredArgs from '../_lib/requiredArgs/index'
import { Interval } from '../types'

/**
* @name isWithinInterval
Expand Down Expand Up @@ -74,13 +75,12 @@ import requiredArgs from '../_lib/requiredArgs/index'
* // For date equal to interval end:
* isWithinInterval(date, { start: date, end }) // => true
*/
export default function isWithinInterval(dirtyDate, dirtyInterval) {
export default function isWithinInterval(dirtyDate: Date | number, interval: Interval): boolean {
requiredArgs(2, arguments)

var interval = dirtyInterval || {}
var time = toDate(dirtyDate).getTime()
var startTime = toDate(interval.start).getTime()
var endTime = toDate(interval.end).getTime()
const time = toDate(dirtyDate).getTime()
const startTime = toDate(interval.start).getTime()
const endTime = toDate(interval.end).getTime()

// Throw an exception if start date is after end date or if any date is `Invalid Date`
if (!(startTime <= endTime)) {
Expand Down
30 changes: 16 additions & 14 deletions src/isWithinInterval/test.js → src/isWithinInterval/test.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,104 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import assert from 'assert'
import isWithinInterval from '.'

describe('isWithinInterval', function() {
it('returns true if the given date in within the given interval', function() {
var result = isWithinInterval(new Date(2014, 9 /* Oct */, 31), {
const result = isWithinInterval(new Date(2014, 9 /* Oct */, 31), {
start: new Date(2014, 8 /* Sep */, 1),
end: new Date(2014, 11 /* Dec */, 31)
})
assert(result === true)
})

it('returns true if the given date has same time as the left boundary of the interval', function() {
var result = isWithinInterval(new Date(2014, 8 /* Sep */, 1), {
const result = isWithinInterval(new Date(2014, 8 /* Sep */, 1), {
start: new Date(2014, 8 /* Sep */, 1),
end: new Date(2014, 11 /* Dec */, 31)
})
assert(result === true)
})

it('returns true if the given date has same time as the right boundary of the interval', function() {
var result = isWithinInterval(new Date(2014, 11 /* Dec */, 31), {
const result = isWithinInterval(new Date(2014, 11 /* Dec */, 31), {
start: new Date(2014, 8 /* Sep */, 1),
end: new Date(2014, 11 /* Dec */, 31)
})
assert(result === true)
})

it('returns true if the given date and the both boundaries are the same', function() {
var result = isWithinInterval(new Date(2014, 11 /* Dec */, 31), {
const result = isWithinInterval(new Date(2014, 11 /* Dec */, 31), {
start: new Date(2014, 11 /* Dec */, 31),
end: new Date(2014, 11 /* Dec */, 31)
})
assert(result === true)
})

it('returns false if the given date is outside of the interval', function() {
var result = isWithinInterval(new Date(2014, 1 /* Feb */, 11), {
const result = isWithinInterval(new Date(2014, 1 /* Feb */, 11), {
start: new Date(2014, 8 /* Sep */, 1),
end: new Date(2014, 11 /* Dec */, 31)
})
assert(result === false)
})

it('accepts a timestamp', function() {
var result = isWithinInterval(new Date(2014, 9 /* Oct */, 31).getTime(), {
const result = isWithinInterval(new Date(2014, 9 /* Oct */, 31).getTime(), {
start: new Date(2014, 8 /* Sep */, 1).getTime(),
end: new Date(2014, 11 /* Dec */, 31).getTime()
})
assert(result === true)
})

it('throws an exception if the start date is after the end date', function() {
var block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
const block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
start: new Date(2014, 11 /* Dec */, 31),
end: new Date(2014, 8 /* Sep */, 1)
})
assert.throws(block, RangeError)
})

it('throws an exception if the interval is undefined', function() {
var block = isWithinInterval.bind(
// @ts-expect-error
const block = isWithinInterval.bind(
null,
new Date(2014, 9 /* Oct */, 31),
// $ExpectedMistake
undefined
)
assert.throws(block, RangeError)
assert.throws(block, TypeError)
})

it('returns false if the given date is `Invalid Date`', function() {
var result = isWithinInterval(new Date(NaN), {
const result = isWithinInterval(new Date(NaN), {
start: new Date(2014, 8 /* Sep */, 1),
end: new Date(2014, 11 /* Dec */, 31)
})
assert(result === false)
})

it('throws an exception if the start date is `Invalid Date`', function() {
var block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
const block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
start: new Date(NaN),
end: new Date(2014, 8 /* Sep */, 1)
})
assert.throws(block, RangeError)
})

it('throws an exception if the end date is `Invalid Date`', function() {
var block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
const block = isWithinInterval.bind(null, new Date(2014, 9 /* Oct */, 31), {
start: new Date(2014, 11 /* Dec */, 31),
end: new Date(NaN)
})
assert.throws(block, RangeError)
})

it('throws TypeError exception if passed less than 2 arguments', function() {
// @ts-expect-error
assert.throws(isWithinInterval.bind(null), TypeError)
// @ts-expect-error
assert.throws(isWithinInterval.bind(null, 1), TypeError)
})
})

0 comments on commit 8c6238e

Please sign in to comment.