Skip to content

Commit

Permalink
Bug fixes and latest version.
Browse files Browse the repository at this point in the history
  • Loading branch information
FotoVerite committed Jul 22, 2014
2 parents e47f38e + 503e198 commit 6bed149
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 30 deletions.
19 changes: 12 additions & 7 deletions examples/js/faker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions faker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions lib/date.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var faker = require('../index');
var faker = require("../index");

var date = {

Expand All @@ -7,16 +7,16 @@ var date = {

var past = date.getTime();
past -= faker.random.number(years) * 365 * 3600 * 1000; // some time from now to N years ago, in milliseconds
date.setTime(past)
date.setTime(past);

return date.toJSON();
},

future: function (years, refDate) {
var date = (refDate) ? new Date(Date.parse(refDate)) : new Date();
var future = date.getTime();
future += faker.random.number(years) * 365 * 3600 * 1000; // some time from now to N years later, in milliseconds
date.setTime(future)
future += faker.random.number(years) * 365 * 3600 * 1000 + 1000; // some time from now to N years later, in milliseconds
date.setTime(future);

return date.toJSON();
},
Expand All @@ -33,8 +33,8 @@ var date = {
recent: function (days) {
var date = new Date();
var future = date.getTime();
future -= faker.random.number(days) * 3600 * 1000; // some time from now to N days ago, in milliseconds
date.setTime(future)
future -= faker.random.number(days) * 24 * 60 * 60 * 1000; // some time from now to N days ago, in milliseconds
date.setTime(future);

return date.toJSON();
}
Expand Down
8 changes: 6 additions & 2 deletions lib/random.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var definitions = require('./definitions');

var random = {
// returns a single random number based on a range
// returns a single random number based on a max number or range
number: function (range) {
if(Array.isArray(range)) {
var min = range[0];
var max = range[1];
return Math.floor((Math.random() * max) + min);
}
return Math.floor(Math.random() * range);
},

Expand Down
12 changes: 6 additions & 6 deletions minfaker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "faker",
"description": "Generate massive amounts of fake contextual data",
"version": "1.0.0",
"version": "1.0.1",
"contributors": [
"Marak Squires <[email protected]>",
"Matthew Bergman <[email protected]>"
Expand Down
10 changes: 9 additions & 1 deletion test/date.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ describe("date.js", function () {
assert.ok(Date.parse(date) > new Date());
});

it("returns a future date when N = 0", function () {

var refDate = new Date();
var date = Date.parse(faker.Date.future(0), refDate.toJSON());

assert.ok(date > refDate); // date should be after the date given, but before the current time
});

it("returns a date N years after the date given", function () {

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)
Expand All @@ -46,7 +54,7 @@ describe("date.js", function () {

var date = faker.Date.recent(30);

assert.ok(Date.parse(date) < new Date());
assert.ok(Date.parse(date) <= new Date());
});

});
Expand Down
22 changes: 22 additions & 0 deletions test/random.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if (typeof module !== 'undefined') {
var assert = require('assert');
var sinon = require('sinon');
var faker = require('../index');
}

describe("random.js", function () {
describe("number", function() {
it("returns a random number given a maximum value", function() {
var max = 10;
assert.ok(faker.random.number(max) < max);
});
it("returns a random number between a range", function() {
var range = [1, 10];
var min = range[0];
var max = range[1]
var randomNumber = faker.random.number(range);
assert.ok( randomNumber >= min);
assert.ok( randomNumber <= max);
});
});
});

0 comments on commit 6bed149

Please sign in to comment.