From 0ff96fb50221ff5bdbb7311aace50c9cd4bdab0d Mon Sep 17 00:00:00 2001 From: James Newell Date: Wed, 2 Jul 2014 11:06:50 +1000 Subject: [PATCH] initial commit --- .gitignore | 3 ++ README.md | 41 +++++++++++++++++++++ component.json | 10 +++++ index.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 8 ++++ test/index_test.js | 69 ++++++++++++++++++++++++++++++++++ 6 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 component.json create mode 100644 index.js create mode 100644 package.json create mode 100644 test/index_test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84e4c71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build +component +node_modules \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1ac28f5 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# date-validators + +Re-usable date validation methods. + +See the list of accepted formats at [moment's documentation](http://momentjs.com/docs/#/parsing/string-format/). + +## Installation + + component install nib-health-funds/date-validators + +## Methods + +### .valid(format : string) + +Returns a method that will return whether a value is a string, adheres to the specified format and parts are in a valid range for a date (e.g. 13 is an invalid month). + +### .lessThan(date : Date|moment, format : string) + +Returns a method that will return whether a value is valid and less than the specified date. + +### .greaterThan(date : Date|moment, format : string) + +Returns a method that will return whether a value is valid and greater than the specified date. + +## Usage + + var + now = new Date(), + validators = require('date-validators') + ; + + validators.valid('YYYY-MM-DD')('asdfsadf'); //false + validators.valid('YYYY-MM-DD')('01/01/2015'); //false + validators.valid('YYYY-MM-DD')('2015-01-01'); //true + + validators.lessThan(now, 'YYYY-MM-DD')('2015-01-01'); //false (at least for *now*) + validators.lessThan(now, 'YYYY-MM-DD')('2012-01-01'); //true + + validators.greaterThan(now, 'YYYY-MM-DD')('2012-01-01'); //false + validators.greaterThan(now, 'YYYY-MM-DD')('2015-01-01'); //true (at least for *now*) + \ No newline at end of file diff --git a/component.json b/component.json new file mode 100644 index 0000000..1c946a2 --- /dev/null +++ b/component.json @@ -0,0 +1,10 @@ +{ + "name": "date-validators", + "description": "Re-usable date validation methods.", + "scripts": [ + "index.js" + ], + "dependencies": { + "moment/moment": "2.7.0" + } +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..9ad86c5 --- /dev/null +++ b/index.js @@ -0,0 +1,92 @@ +var moment = require('moment'); + +/** + * Parse the value into a date according to the specified format + * @param {string} value + * @param {string} format + * @returns {Date|null} + */ +function parse(value, format) { + format = format || 'YYYY-MM-DD'; + + //ensure the value is a string + if (typeof value !== 'string') { + return null; + } + + return moment(value, format, true); +} + +module.exports = { + + /** + * Get whether the value is a valid date + * @param {string} format + * @returns {function(*):boolean} + */ + valid: function(format) { + return function(value) { + var date = parse(value, format); + return date && date.isValid(); + }; + }, + + /** + * Get whether the value is less than the specified date + * @param {Date} date The date + * @param {string} format The date format of the value + * @returns {function(*):boolean} + */ + lessThan: function(date, format) { + + //convert the date into a moment + if (typeof date === 'string' || date instanceof Date) { + date = moment(date); + } + + return function(value) { + + //parse the value + var value = parse(value, format); + + //ensure the date is valid + if (!(date && date.isValid())) { + return false; + } + + //if the moment is later than the moment you are passing to moment.fn.diff, the return value will be negative. + var diff = date.diff(value); + return (diff > 0); + } + }, + + /** + * Get whether the value is greater than the specified date + * @param {Date} date The date + * @param {string} format The date format of the value + * @returns {function(*):boolean} + */ + greaterThan: function(date, format) { + + //convert the date into a moment + if (typeof date === 'string' || date instanceof Date) { + date = moment(date); + } + + return function(value) { + + //parse the value + var value = parse(value, format); + + //ensure the date is valid + if (!(value && value.isValid())) { + return false; + } + + //if the moment is later than the moment you are passing to moment.fn.diff, the return value will be negative. + var diff = date.diff(value); + return (diff < 0); + } + } + +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9e769a6 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "name": "nib-health-fund-date-validators", + "description": "Re-usable date validation methods.", + "version": "0.0.1", + "dependencies": { + "moment": "2.7.0" + } +} diff --git a/test/index_test.js b/test/index_test.js new file mode 100644 index 0000000..8468b18 --- /dev/null +++ b/test/index_test.js @@ -0,0 +1,69 @@ +var assert = require('assert'); +var moment = require('moment'); +var validators = require('../index'); + +describe('#date', function() { + + describe('#valid', function() { + + var validator = validators.valid('YYYY-MM-DD'); + + it('should return false', function() { + assert(!validator(null)); + assert(!validator(true)); + assert(!validator(false)); + assert(!validator(1)); + assert(!validator({})); + assert(!validator({ value: new Date()})); + assert(!validator('sdfa')); + assert(!validator('22/01/2012')); + }); + + it('should return true', function() { + assert(validator('1900-01-01')); + assert(validator('1948-11-11')); + assert(validator('2014-07-02')); + }); + + }); + + describe('#lessThan', function() { + + var validator = validators.lessThan(moment('2015-01-01'), 'YYYY-MM-DD'); + + it('should return false', function() { + assert(!validator('2015-01-01')); + assert(!validator('2015-01-02')); + assert(!validator('2015-12-31')); + assert(!validator('2016-01-01')); + + }); + + it('should return true', function() { + assert(validator('2014-12-31')); + assert(validator('2014-01-01')); + assert(validator('2013-05-10')); + }); + + }); + + describe('#greaterThan', function() { + + var validator1 = validators.greaterThan(moment('2015-01-01'), 'YYYY-MM-DD'); + + it('should return false', function() { + assert(!validator1('2015-01-01')); + assert(!validator1('2014-12-31')); + assert(!validator1('2014-01-01')); + assert(!validator1('2013-05-10')); + }); + + it('should return true', function() { + assert(validator1('2015-01-02')); + assert(validator1('2015-12-31')); + assert(validator1('2016-01-01')); + }); + + }); + +}); \ No newline at end of file