diff options
| author | Marak <[email protected]> | 2014-11-29 19:40:56 +0530 |
|---|---|---|
| committer | Marak <[email protected]> | 2014-11-29 19:40:56 +0530 |
| commit | 0370b2eedb2fa667716308d42961a3bc48b933a0 (patch) | |
| tree | 0adfe917f4c461dcdf9a22eaaecf6f0ee429688a | |
| parent | 11fefd8006a8a2d63bcb02ad0f2bf213716e34a9 (diff) | |
| parent | 89070a9b5a544801eeacd6f3c5fea83794a42570 (diff) | |
| download | faker-0370b2eedb2fa667716308d42961a3bc48b933a0.tar.xz faker-0370b2eedb2fa667716308d42961a3bc48b933a0.zip | |
Merge pull request #146 from mkawalec/master
[fix] Precision changes precision and max doesn't modify options object
| -rw-r--r-- | lib/random.js | 24 | ||||
| -rw-r--r-- | package.json | 59 | ||||
| -rw-r--r-- | test/random.unit.js | 35 |
3 files changed, 77 insertions, 41 deletions
diff --git a/lib/random.js b/lib/random.js index 70bcaf04..a4bc1858 100644 --- a/lib/random.js +++ b/lib/random.js @@ -6,16 +6,12 @@ var random = { number: function (options) { if (typeof options === "number") { - var options = { + options = { max: options }; } - options = options || { - min: 0, - max: 1, - precision: 1 - }; + options = options || {}; if (typeof options.min === "undefined") { options.min = 0; @@ -24,13 +20,19 @@ var random = { if (typeof options.max === "undefined") { options.max = 1; } - - // by incrementing max by 1, max becomes inclusive of the range - if (options.max > 0) { - options.max++; + if (typeof options.precision === "undefined") { + options.precision = 1; } - var randomNumber = mersenne.rand(options.max, options.min); + // Make the range inclusive of the max value + var max = options.max; + if (max > 0) { + max += options.precision; + } + + var randomNumber = options.precision * Math.floor( + mersenne.rand(max / options.precision, options.min / options.precision)); + return randomNumber; }, diff --git a/package.json b/package.json index ba050329..4bdf20fa 100644 --- a/package.json +++ b/package.json @@ -1,31 +1,32 @@ { - "name": "faker", - "description": "Generate massive amounts of fake contextual data", - "version": "2.1.0", - "contributors": [ - "Marak Squires <[email protected]>", - "Matthew Bergman <[email protected]>" - ], - "repository": { - "type": "git", - "url": "http://github.com/Marak/Faker.js.git" - }, - "scripts": { - "test": "node_modules/.bin/mocha test/*.*.js" - }, - "devDependencies": { - "jshint": "0.9.0", - "istanbul": "0.1.25", - "mocha": "1.8.x", - "node-minify": "*", - "optimist" : "0.3.5", - "sinon": "1.4.2", - "gulp": "3.8.8", - "browserify": "5.11.1", - "gulp-uglify": "1.0.1", - "gulp-rename": "1.2.0", - "gulp-mustache": "0.4.0", - "vinyl-transform": "0.0.1" - }, - "main": "index.js" + "name": "faker", + "description": "Generate massive amounts of fake contextual data", + "version": "2.1.0", + "contributors": [ + "Marak Squires <[email protected]>", + "Matthew Bergman <[email protected]>" + ], + "repository": { + "type": "git", + "url": "http://github.com/Marak/Faker.js.git" + }, + "scripts": { + "test": "node_modules/.bin/mocha test/*.*.js" + }, + "devDependencies": { + "browserify": "5.11.1", + "gulp": "3.8.8", + "gulp-mustache": "0.4.0", + "gulp-rename": "1.2.0", + "gulp-uglify": "1.0.1", + "istanbul": "0.1.25", + "jshint": "0.9.0", + "lodash": "^2.4.1", + "mocha": "1.8.x", + "node-minify": "*", + "optimist": "0.3.5", + "sinon": "1.4.2", + "vinyl-transform": "0.0.1" + }, + "main": "index.js" } diff --git a/test/random.unit.js b/test/random.unit.js index d1112bcd..6071767b 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -1,9 +1,11 @@ if (typeof module !== 'undefined') { var assert = require('assert'); var sinon = require('sinon'); + var _ = require('lodash'); var faker = require('../index'); } + describe("random.js", function () { describe("number", function() { @@ -26,6 +28,37 @@ describe("random.js", function () { assert.ok(randomNumber <= options.max); } }); - }); + it("provides numbers with a given precision", function() { + var options = { min: 0, max: 1.5, precision: 0.5 }; + var results = _.chain(_.range(50)) + .map(function() { + return faker.random.number(options); + }) + .uniq() + .value() + .sort(); + + assert.ok(_.contains(results, 0.5)); + assert.ok(_.contains(results, 1.0)); + + assert.equal(results[0], 0); + assert.equal(_.last(results), 1.5); + + }); + + it("should not modify the input object", function() { + var min = 1; + var max = 2; + var opts = { + min: min, + max: max + }; + + faker.random.number(opts); + + assert.equal(opts.min, min); + assert.equal(opts.max, max); + }); + }); }); |
