This a simple gist to demostrate how to use date-fns to check the days of the week. Also, using invarant to throw errors.
Last active
June 7, 2016 14:14
-
-
Save erickeno/52f7c2e6a756491ed81222561b73f4e2 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or 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
import format from 'date-fns/format'; | |
import invariant from 'invariant'; | |
// Throw a error if it's not the right Day of the week. | |
function isRightDay(date, actualDay, errorMsg) { | |
const dayVal = format(date, 'dddd'); | |
invariant(dayVal === actualDay, errorMsg); | |
}; | |
// Quick example of how to execute the function | |
// For further options check date-fns module | |
isRightDay(new Date(),'Wednesday', 'Make sure the day is wed' ); |
This file contains hidden or 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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"invariant": "2.2.1", | |
"date-fns": "1.3.0" | |
} | |
} |
This file contains hidden or 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
'use strict'; | |
var _format = require('date-fns/format'); | |
var _format2 = _interopRequireDefault(_format); | |
var _invariant = require('invariant'); | |
var _invariant2 = _interopRequireDefault(_invariant); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
// Throw a error if it's not the right Day of the week. | |
function isRightDay(date, actualDay, errorMsg) { | |
var dayVal = (0, _format2.default)(date, 'dddd'); | |
(0, _invariant2.default)(dayVal === actualDay, errorMsg); | |
}; | |
// Quick example of how to execute the function | |
// For further options check date-fns module | |
isRightDay(new Date(), 'Wednesday', 'Make sure the day is wed'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment