Skip to content

Instantly share code, notes, and snippets.

@erickeno
Last active June 7, 2016 14:14
Show Gist options
  • Save erickeno/52f7c2e6a756491ed81222561b73f4e2 to your computer and use it in GitHub Desktop.
Save erickeno/52f7c2e6a756491ed81222561b73f4e2 to your computer and use it in GitHub Desktop.
esnextbin sketch

This a simple gist to demostrate how to use date-fns to check the days of the week. Also, using invarant to throw errors.

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' );
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"invariant": "2.2.1",
"date-fns": "1.3.0"
}
}
'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