diff options
| author | Marak <[email protected]> | 2017-06-30 18:04:41 -0400 |
|---|---|---|
| committer | Marak <[email protected]> | 2017-08-20 18:17:27 -0400 |
| commit | 05e5c4442630cedc7c25bcd6721a4d4572b87d90 (patch) | |
| tree | 4efb757542a3d374b7f1ac8ba94bde36bee655a6 /examples/node | |
| parent | 6cdb93efcbcaf222ac061cee5532374f72ea073e (diff) | |
| download | faker-05e5c4442630cedc7c25bcd6721a4d4572b87d90.tar.xz faker-05e5c4442630cedc7c25bcd6721a4d4572b87d90.zip | |
[api] First pass at `faker.unique` module
* Adds unique value API for use with any method
* Using in-memory store for unique values
* Provides maxRetries and maxTime options
* RE: #466 #245 #477
Diffstat (limited to 'examples/node')
| -rw-r--r-- | examples/node/unique-values.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/examples/node/unique-values.js b/examples/node/unique-values.js new file mode 100644 index 00000000..3c346612 --- /dev/null +++ b/examples/node/unique-values.js @@ -0,0 +1,29 @@ +var faker = require('../../index'); + +var emails = {}; +var conflicts = 0; +// emails estimated: 1,055,881 +// full names estimated: 1,185,139 +for (var i = 0; i < 100000; i++) { + + // call function with no arguments + var email = faker.unique(faker.internet.email); + + // or with function arguments as argument array + // var email = faker.unique(faker.internet.email, [null, null, 'marak.com']); + + // or with custom options for maxTime as milliseconds or maxRetries + // var email = faker.unique(faker.internet.email, [null, null, 'marak.com'], { maxRetries: 1, maxTime: 50 }); + + if (typeof emails[email] === 'undefined') { + // found a unique new item + emails[email] = true; + } else { + // found a conflicting item ( should not happen using faker.unique() ) + conflicts++; + } +} +console.log('total conflicts', conflicts); // should be zero using faker.unique() +console.log('total uniques generated', Object.keys(emails).length); + +// console.log(emails);
\ No newline at end of file |
