pretty-easy-date-check
v1.2.4
Published
Check if a supplied value is a valid Date (instanceof Date or a valid string representation of a date that can be turned into one by instantiating Date class).
Downloads
96
Maintainers
Readme
pretty-easy-date-check
What is pretty-easy-date-check?
pretty-easy-date-check is a simple NodeJS module for performing check against a supplied value to determine if it is a valid date (instance of Date class or a valid string representation of a date that can be used as a valid argument to instantiate the Date class!).
If you just want to check if the value is an instance of a Date class, consider using a utility library such as pretty-easy-data-types.
Install
This is a NodeJS module available through the npm registry. Installation is done using the npm install command:
$ npm install pretty-easy-date-check --save
--save flag is used to save the module as a project dependancy in your package.json file.
Usage
After installing the module (localy in your project directory), in order to use it in your file you first need to require it.
let isValidDate = require('pretty-easy-date-check');
or if you use TypeScript
import { default as isValidDate } from 'pretty-easy-date-check';
The module returns a function for you to call and supply it with parameter that you'd like to check if it is a valid Date or a string representation of a valid date (see the examples of valid dates, down below). The function returns a Boolean.
Examples
Valid dates
isValidDate(new Date()); // true
isValidDate('3.10.17'); // true
isValidDate('03.10.2017.'); // true
isValidDate('3,10,17'); // true
isValidDate('03,10,2017'); // true
isValidDate('3/10/17'); // true
isValidDate('03/10/2017'); // true
isValidDate('3-10-17'); // true
isValidDate('03-10-2017'); // true
isValidDate('Fri Mar 10 17'); // true
isValidDate('Fri Mar 10 2017'); // true
isValidDate(new Date().toString()); // true
// Pretty much, everything else should result in a false value
Important :
The module will return a boolean true if the value passed is :
- an instance of Date constructor class or
- valid string representation of date
For more flexible Date data type checks, such as the ones for checking if the value is only an instance of a Date class and not a valid date string, consider including a utility library such as pretty-easy-data-types.
/*
* Only import the checks you will be using,
* instead of including the whole library
*/
const {
isDate, // check for instances of Date class
isError // check for instances of Error class
} = require('pretty-easy-data-types');
const canBeDate = require('pretty-easy-date-check');
/*
* Check if the value is the instance of a Date class (and not a string representing a date)
* and if so, in this example we're just going to use that instance of Date object
* else we're checking if the value is a valid string representation of a date
* and if so, we're going to instantiate a Date class with it
* else we're just going to construct a new instance of Error class
*/
const sampleDate = 'not date';
const date = isDate(sampleDate) ?
sampleDate :
(canBeDate(sampleDate) ? new Date(sampleDate) : new Error('Invalid date!'));
// It is not an instance of Date; it's an Error
if(isError(date)) console.log(date.message);
// It is a valid instance of Date class
else console.log(`Valid date supplied`);
Releases
The module follows the Semantic Versioning standard to communicate what kinds of changes are introduced in the new releases.
Versioning
Patch releases : n.n.X -> Bug fixes, documentation updates, code cleanups, new test cases, optimization stuff and other minor changes that you should probably not be aware of;
Minor releases : n.X.n -> New feature(s) which don't break the existing ones. These ofter refer to minor TypeScript API changes (mainly due to declarations; JavaScript code will not be affected by these changes), code refactoring, some under the sheet changes that you should not worry about too much;
Major releases : X.n.n -> Changes that could possibly introduce the backwards compatibility issues. These are however very rare and could be relevant to you only in the case of an endpoint API change and the way you communicate with the module.
Changelogs
03/30 - v1.2.3
- Tiny improvement, for module consistancy
03/30 - v1.2.3
- TypeScript intelisense and declaration improvements
- Documentation updates
03/29 - v1.2.2
- Documentation updates
03/29 - v1.2.1
- Bugfix
- Documentation updates
03/28 - v1.2.0
- Various improvements
- New test cases
- TypeScript API changes (due to definition updates)
- Documentation updates to reflect the changes
03/20 - v1.1.3
- Support for Node < 1.8 abbandoned
- Modular approach to unit tests
- New test cases
- Documentation updates to reflect the changes
03/16 - v1.1.2
- Documenation updates
03/15 - v1.1.1
- Documentation updates
03/15 - v1.1.0
- Dependancy updates
03/14 - v1.0.5
- Minor improvement
03/13 - v1.0.4
- Typescript declaration conflicts
03/13 - v1.0.3
- Additional test cases provided
03/13 - v1.0.2
- Minor improvement
03/12 - v1.0.1
- Bugfix (falsy values related)
03/10 - v1.0.0
- Initial release
Want to contribute?
Great! Anyone can help make this project better - check out the github repository!
Found a bug?
Please open a an issue.
License
Copyright (c) 2017 Ognjen Jevremović
Licensed under the MIT License.