Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit a441981

Browse files
Miguel AlmeidaKent C. Dodds
authored andcommitted
feat(isToday): add isToday function (#72)
Add function that tests if a given date is equal to today Closes #71
1 parent f009f81 commit a441981

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import isArray from './is-array'
1111
import validateEmail from './validateEmail'
1212
import hex2rgb from './hex2rgb'
1313
import isNullOrWhitespace from './is-null-or-whitespace'
14+
import isToday from './is-today'
1415
import startsWith from './startsWith'
1516
import removeDuplicates from './remove-duplicates'
1617
import add from './add'
@@ -35,6 +36,7 @@ export {
3536
validateEmail,
3637
hex2rgb,
3738
isNullOrWhitespace,
39+
isToday,
3840
startsWith,
3941
removeDuplicates,
4042
add,

src/is-today.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default isToday
2+
3+
/**
4+
* This method tests if a given date is today
5+
*
6+
* @param {Date} date - date to be evaluated
7+
* @returns {Boolean} - True if date is today, false otherwise
8+
*/
9+
function isToday(date) {
10+
if (!(date instanceof Date)) {
11+
return false
12+
}
13+
const today = new Date()
14+
15+
return today.setHours(0, 0, 0, 0) === date.setHours(0, 0, 0, 0)
16+
}
17+

test/is-today.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import test from 'ava'
2+
import {isToday} from '../src'
3+
4+
test('check today', t => {
5+
const expected = true;
6+
const actual = isToday(new Date());
7+
8+
t.is(actual, expected);
9+
});
10+
11+
test('check previous date', t => {
12+
const today = new Date();
13+
const yesterday = new Date(today.setDate(today.getDate() - 1));
14+
15+
const expected = false;
16+
const actual = isToday(yesterday);
17+
18+
t.is(actual, expected);
19+
});
20+
21+
test('check future date', t => {
22+
const today = new Date();
23+
const tomorrow = new Date(today.setDate(today.getDate() + 1));
24+
25+
const expected = false;
26+
const actual = isToday(tomorrow);
27+
28+
t.is(actual, expected);
29+
});
30+
31+
test('check wrong parameter', t => {
32+
const expected = false;
33+
const actual = isToday('dummy');
34+
35+
t.is(actual, expected);
36+
})

0 commit comments

Comments
 (0)