-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Newell
committed
Jul 2, 2014
0 parents
commit 0ff96fb
Showing
6 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build | ||
component | ||
node_modules |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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*) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "date-validators", | ||
"description": "Re-usable date validation methods.", | ||
"scripts": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"moment/moment": "2.7.0" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
}); | ||
|
||
}); | ||
|
||
}); |