Skip to content

Commit

Permalink
Merge pull request #3 from seegno/enhancement/add-date-diff-less-and-…
Browse files Browse the repository at this point in the history
…greater-than-asserts

Add date diff less and greater than asserts
  • Loading branch information
fixe committed Dec 1, 2014
2 parents 2838148 + a7d4350 commit efb4058
Show file tree
Hide file tree
Showing 5 changed files with 633 additions and 1 deletion.
73 changes: 73 additions & 0 deletions lib/asserts/date-diff-greater-than-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

/**
* Module dependencies.
*/

var _ = require('lodash');
var DateAssert = require('./date-assert');
var Violation = require('validator.js').Violation;
var moment = require('moment');

/**
* Export `DateDiffGreaterThanAssert`.
*/

module.exports = function(threshold, options) {

/**
* Class name.
*/

this.__class__ = 'DateDiffGreaterThan';

/**
* Check if `threshold` is defined.
*/

if ('undefined' === typeof threshold) {
throw new Error('A threshold value is required.');
}

/**
* Threshold.
*/

this.threshold = threshold;

/**
* Options.
*/

this.options = _.extend(this, {
absolute: false,
asFloat: false,
fromDate: null,
unit: 'milliseconds'
}, options);

/**
* Validation algorithm.
*/

this.validate = function(value) {
try {
new DateAssert().validate(value);
} catch (e) {
throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
}

var diff = moment(this.fromDate || Date.now()).diff(value, this.unit, this.asFloat);

if (this.absolute) {
diff = Math.abs(diff);
}

if (diff <= this.threshold) {
throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit, diff: diff });
}

return true;
};

return this;
};
73 changes: 73 additions & 0 deletions lib/asserts/date-diff-less-than-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

/**
* Module dependencies.
*/

var _ = require('lodash');
var DateAssert = require('./date-assert');
var Violation = require('validator.js').Violation;
var moment = require('moment');

/**
* Export `DateDiffLessThanAssert`.
*/

module.exports = function(threshold, options) {

/**
* Class name.
*/

this.__class__ = 'DateDiffLessThan';

/**
* Check if `threshold` is defined.
*/

if ('undefined' === typeof threshold) {
throw new Error('A threshold value is required.');
}

/**
* Threshold.
*/

this.threshold = threshold;

/**
* Options.
*/

this.options = _.extend(this, {
absolute: false,
asFloat: false,
fromDate: null,
unit: 'milliseconds'
}, options);

/**
* Validation algorithm.
*/

this.validate = function(value) {
try {
new DateAssert().validate(value);
} catch (e) {
throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
}

var diff = moment(this.fromDate || Date.now()).diff(value, this.unit, this.asFloat);

if (this.absolute) {
diff = Math.abs(diff);
}

if (diff >= this.threshold) {
throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, diff: diff, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
}

return true;
};

return this;
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"bignumber.js": "^1.4.1",
"iso-3166-1-data": "0.0.2",
"lodash": "^2.4.1",
"moment": "^2.8.4",
"provinces": "^0.2.0",
"require-dir": "^0.1.0",
"to-pascal-case": "0.0.2",
Expand All @@ -56,6 +57,7 @@
"devDependencies": {
"github-changes": "0.0.14",
"mocha": "^2.0.1",
"should": "^4.2.1"
"should": "^4.2.1",
"sinon": "^1.12.1"
}
}
Loading

0 comments on commit efb4058

Please sign in to comment.