-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate isWithinInterval to TS (#2394)
Co-authored-by: Sasha Koss <koss@nocorp.me>
- Loading branch information
1 parent
2860a54
commit 8c6238e
Showing
2 changed files
with
21 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 16 additions & 14 deletions
30
src/isWithinInterval/test.js → src/isWithinInterval/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |