diff options
| author | Marak <[email protected]> | 2017-09-08 11:40:56 -0400 |
|---|---|---|
| committer | Marak <[email protected]> | 2017-09-08 11:40:56 -0400 |
| commit | 633412cdba17fce9b730872b9dd2f530307aec1b (patch) | |
| tree | 1e5feab44839abbb9990ca0fd4534827f0b8800c | |
| parent | 9ffe7934499fa93f59186499a81856326cb5b953 (diff) | |
| download | faker-633412cdba17fce9b730872b9dd2f530307aec1b.tar.xz faker-633412cdba17fce9b730872b9dd2f530307aec1b.zip | |
[api] Unique compare function now configurable
* Can now use custom compare functions
* Improved comments
| -rw-r--r-- | vendor/unique.js | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/vendor/unique.js b/vendor/unique.js index b8e89466..f1f7425c 100644 --- a/vendor/unique.js +++ b/vendor/unique.js @@ -2,8 +2,15 @@ var unique = {}; // global results store +// currently uniqueness is global to entire faker instance +// this means that faker should currently *never* return duplicate values across all API methods when using `Faker.unique` +// it's possible in the future that some users may want to scope found per function call instead of faker instance var found = {}; +// global exclude list of results +// defaults to nothing excluded +var exclude = []; + // maximum time unique.exec will attempt to run before aborting var maxTime = 5000; @@ -16,6 +23,15 @@ var startTime = new Date().getTime(); // current iteration or retries of unique.exec ( current loop depth ) var currentIterations = 0; +// uniqueness compare function +// default behavior is to check value as key against object hash +var defaultCompare = function(obj, key) { + if (typeof obj[key] === 'undefined') { + return -1; + } + return 0; +}; + // common error handler for messages unique.errorMessage = function (now, code) { console.error('error', code); @@ -30,6 +46,13 @@ unique.exec = function (method, args, opts) { opts = opts || {}; opts.maxTime = opts.maxTime || maxTime; opts.maxRetries = opts.maxRetries || maxRetries; + opts.exclude = opts.exclude || exclude; + opts.compare = opts.compare || defaultCompare; + + // support single exclude argument as string + if (typeof opts.exclude === 'string') { + opts.exclude = [opts.exclude]; + } if (currentIterations > 0) { // console.log('iterating', currentIterations) @@ -45,11 +68,10 @@ unique.exec = function (method, args, opts) { } // execute the provided method to find a potential satifised value - // console.log(args) var result = method.apply(this, args); + // if the result has not been previously found, add it to the found array and return the value as it's unique - if (typeof found[result] === 'undefined') { - //console.log('set email', email) + if (opts.compare(found, result) === -1 && opts.exclude.indexOf(result) === -1) { found[result] = result; currentIterations = 0; return result; |
