diff options
| author | Marak <[email protected]> | 2018-09-24 10:54:48 -0400 |
|---|---|---|
| committer | Marak <[email protected]> | 2018-09-24 10:55:08 -0400 |
| commit | 4759e2b35c0557fbc17ed38dc5cbc21929de29bc (patch) | |
| tree | 077dea345934ffb7c8f3fe3dc6ffded40115d96a | |
| parent | afb8a7f2ac662ce742e18668d6537f0d42cba6e2 (diff) | |
| download | faker-4759e2b35c0557fbc17ed38dc5cbc21929de29bc.tar.xz faker-4759e2b35c0557fbc17ed38dc5cbc21929de29bc.zip | |
[api] [fix] [refactor] Unique options into class
* #466 #596
* Fixes scope issues with max values
| -rw-r--r-- | lib/unique.js | 26 | ||||
| -rw-r--r-- | test/unique.unit.js | 12 | ||||
| -rw-r--r-- | vendor/unique.js | 45 |
3 files changed, 52 insertions, 31 deletions
diff --git a/lib/unique.js b/lib/unique.js index 4422a942..70d038fa 100644 --- a/lib/unique.js +++ b/lib/unique.js @@ -5,12 +5,34 @@ var uniqueExec = require('../vendor/unique'); */ function Unique (faker) { + // initialize unique module class variables + + // maximum time unique.exec will attempt to run before aborting + var maxTime = 10; + + // maximum retries unique.exec will recurse before abortings ( max loop depth ) + var maxRetries = 10; + + // time the script started + // var startTime = 0; + /** * unique * * @method unique */ - this.unique = uniqueExec.exec; + this.unique = function unique (method, args, opts) { + opts = opts || {}; + opts.startTime = new Date().getTime(); + if (typeof opts.maxTime !== 'number') { + opts.maxTime = maxTime; + } + if (typeof opts.maxRetries !== 'number') { + opts.maxRetries = maxRetries; + } + opts.currentIterations = 0; + return uniqueExec.exec(method, args, opts); + } } -module['exports'] = Unique; +module['exports'] = Unique;
\ No newline at end of file diff --git a/test/unique.unit.js b/test/unique.unit.js index 4edb8220..47b38d9d 100644 --- a/test/unique.unit.js +++ b/test/unique.unit.js @@ -8,8 +8,8 @@ describe("unique.js", function () { describe("unique()", function () { it("is able to call a function with no arguments and return a result", function () { - var result = faker.unique(faker.internet.email); - assert.equal(typeof result, 'string'); + var result = faker.unique(faker.internet.email); + assert.equal(typeof result, 'string'); }); it("is able to call a function with arguments and return a result", function () { @@ -17,7 +17,7 @@ describe("unique.js", function () { assert.ok(result.match(/\@c/)); }); - it("is able to call a function with arguments and return a result", function () { + it("is able to call same function with arguments and return a result", function () { var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email assert.ok(result.match(/\@c/)); }); @@ -32,7 +32,7 @@ describe("unique.js", function () { try { result = faker.unique(faker.internet.protocol, [], { maxTime: 1, maxRetries: 9999, exclude: ['https', 'http'] }); } catch (err) { - assert.equal(err.message.substr(0, 16), 'exceeded maxTime'); + assert.equal(err.message.substr(0, 16), 'Exceeded maxTime'); } }); @@ -41,11 +41,11 @@ describe("unique.js", function () { try { result = faker.unique(faker.internet.protocol, [], { maxTime: 5000, maxRetries: 5, exclude: ['https', 'http'] }); } catch (err) { - assert.equal(err.message.substr(0, 19), 'exceeded maxRetries'); + assert.equal(err.message.substr(0, 19), 'Exceeded maxRetries'); } }); - it("is able to call a function with arguments and return a result", function () { + it("is able to call last function with arguments and return a result", function () { var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email assert.ok(result.match(/\@c/)); }); diff --git a/vendor/unique.js b/vendor/unique.js index 0a0e8072..25b2c11a 100644 --- a/vendor/unique.js +++ b/vendor/unique.js @@ -11,15 +11,6 @@ var found = {}; // defaults to nothing excluded var exclude = []; -// maximum time unique.exec will attempt to run before aborting -var maxTime = 5000; - -// maximum retries unique.exec will recurse before abortings ( max loop depth ) -var maxRetries = 50; - -// time the script started -var startTime = null; - // current iteration or retries of unique.exec ( current loop depth ) var currentIterations = 0; @@ -33,41 +24,49 @@ var defaultCompare = function(obj, key) { }; // common error handler for messages -unique.errorMessage = function (now, code) { +unique.errorMessage = function (now, code, opts) { console.error('error', code); - console.log('found', Object.keys(found).length, 'unique entries before throwing error. \nretried:', currentIterations, '\ntotal time:', now - startTime, 'ms'); - throw new Error(code + ' for uniquness check. may not be able to generate any more unique values with current settings. try adjusting maxTime or maxRetries parameters for faker.unique()') + console.log('found', Object.keys(found).length, 'unique entries before throwing error. \nretried:', currentIterations, '\ntotal time:', now - opts.startTime, 'ms'); + throw new Error(code + ' for uniqueness check \n\nMay not be able to generate any more unique values with current settings. \nTry adjusting maxTime or maxRetries parameters for faker.unique()') }; unique.exec = function (method, args, opts) { + //console.log(currentIterations) - if (currentIterations === 0) { - startTime = new Date().getTime(); - } var now = new Date().getTime(); opts = opts || {}; - opts.maxTime = opts.maxTime || maxTime; - opts.maxRetries = opts.maxRetries || maxRetries; + opts.maxTime = opts.maxTime || 3; + opts.maxRetries = opts.maxRetries || 50; opts.exclude = opts.exclude || exclude; opts.compare = opts.compare || defaultCompare; + if (typeof opts.currentIterations !== 'number') { + opts.currentIterations = 0; + } + + if (typeof opts.startTime === 'undefined') { + opts.startTime = new Date().getTime(); + } + + var startTime = opts.startTime; + // support single exclude argument as string if (typeof opts.exclude === 'string') { opts.exclude = [opts.exclude]; } - if (currentIterations > 0) { + if (opts.currentIterations > 0) { // console.log('iterating', currentIterations) } // console.log(now - startTime) if (now - startTime >= opts.maxTime) { - return unique.errorMessage(now, 'exceeded maxTime'); + return unique.errorMessage(now, 'Exceeded maxTime:' + opts.maxTime, opts); } - if (currentIterations >= opts.maxRetries) { - return unique.errorMessage(now, 'exceeded maxRetries'); + if (opts.currentIterations >= opts.maxRetries) { + return unique.errorMessage(now, 'Exceeded maxRetries:' + opts.maxRetries, opts); } // execute the provided method to find a potential satifised value @@ -76,11 +75,11 @@ unique.exec = function (method, args, opts) { // if the result has not been previously found, add it to the found array and return the value as it's unique if (opts.compare(found, result) === -1 && opts.exclude.indexOf(result) === -1) { found[result] = result; - currentIterations = 0; + opts.currentIterations = 0; return result; } else { // console.log('conflict', result); - currentIterations++; + opts.currentIterations++; return unique.exec(method, args, opts); } }; |
