aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDroogans <[email protected]>2018-10-09 13:42:34 -0700
committerDroogans <[email protected]>2018-10-09 14:01:29 -0700
commit816b658e26badba6a911b7065aa8ca413d848b9d (patch)
tree9f4f482202a12db841b794fac322913a6683f7e3
parent0e6fb645d6907df854ca2f84dfe518e55502646a (diff)
downloadfaker-816b658e26badba6a911b7065aa8ca413d848b9d.tar.xz
faker-816b658e26badba6a911b7065aa8ca413d848b9d.zip
fix(date): Allow for `refDate` in `soon()`, `recent()`
Closes #686
-rw-r--r--lib/date.js14
-rw-r--r--test/date.unit.js26
2 files changed, 34 insertions, 6 deletions
diff --git a/lib/date.js b/lib/date.js
index 1977137b..8fb32bd1 100644
--- a/lib/date.js
+++ b/lib/date.js
@@ -67,9 +67,10 @@ var _Date = function (faker) {
*
* @method faker.date.recent
* @param {number} days
+ * @param {date} refDate
*/
- self.recent = function (days) {
- var date = new Date();
+ self.recent = function (days, refDate) {
+ var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
var range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000
@@ -87,9 +88,10 @@ var _Date = function (faker) {
*
* @method faker.date.soon
* @param {number} days
+ * @param {date} refDate
*/
- self.soon = function (days) {
- var date = new Date();
+ self.soon = function (days, refDate) {
+ var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
var range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000
@@ -145,9 +147,9 @@ var _Date = function (faker) {
return faker.random.arrayElement(source);
};
-
+
return self;
-
+
};
module['exports'] = _Date;
diff --git a/test/date.unit.js b/test/date.unit.js
index bd283d07..c3f4eb6c 100644
--- a/test/date.unit.js
+++ b/test/date.unit.js
@@ -65,6 +65,19 @@ describe("date.js", function () {
assert.ok(date <= new Date());
});
+ it("returns a date N days from the recent past, starting from refDate", function () {
+
+ var days = 30;
+ var refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
+
+ var date = faker.date.recent(days, refDate);
+
+ var lowerBound = new Date(refDate.getTime() - (days * 24 * 60 * 60 * 1000));
+
+ assert.ok(lowerBound <= date, "`recent()` date should not be further back than `n` days ago");
+ assert.ok(date <= refDate, "`recent()` date should not be ahead of the starting date reference");
+ });
+
});
describe("soon()", function () {
@@ -75,6 +88,19 @@ describe("date.js", function () {
assert.ok(date >= new Date());
});
+ it("returns a date N days from the recent future, starting from refDate", function () {
+
+ var days = 30;
+ var refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)
+
+ var date = faker.date.soon(days, refDate);
+
+ var upperBound = new Date(refDate.getTime() + (days * 24 * 60 * 60 * 1000));
+
+ assert.ok(date <= upperBound, "`soon()` date should not be further ahead than `n` days ago");
+ assert.ok(refDate <= date, "`soon()` date should not be behind the starting date reference");
+ });
+
});
describe("between()", function () {