Skip to content

Commit

Permalink
Merge pull request #487 from atorkhov/patch-2
Browse files Browse the repository at this point in the history
[api] Added faker.date.soon
  • Loading branch information
Marak authored Sep 8, 2017
2 parents af953d8 + 4ff0461 commit 4adcc92
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ This will interpolate the format string with the value of methods `name.lastName
* future
* between
* recent
* soon
* month
* weekday
* fake
Expand Down
22 changes: 21 additions & 1 deletion lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ var _Date = function (faker) {
return date;
};

/**
* soon
*
* @method faker.date.soon
* @param {number} days
*/
self.soon = function (days) {
var date = new Date();
var range = {
min: 1000,
max: (days || 1) * 24 * 3600 * 1000
};

var future = date.getTime();
future += faker.random.number(range); // some time from now to N days later, in milliseconds
date.setTime(future);

return date;
};

/**
* month
*
Expand Down Expand Up @@ -130,4 +150,4 @@ var _Date = function (faker) {

};

module['exports'] = _Date;
module['exports'] = _Date;
10 changes: 10 additions & 0 deletions test/date.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe("date.js", function () {

});

describe("soon()", function () {
it("returns a date N days into the future", function () {

var date = faker.date.soon(30);

assert.ok(date >= new Date());
});

});

describe("between()", function () {
it("returns a random date between the dates given", function () {

Expand Down

0 comments on commit 4adcc92

Please sign in to comment.