diff options
| author | Marak <[email protected]> | 2016-02-06 08:15:43 +0530 |
|---|---|---|
| committer | Marak <[email protected]> | 2016-02-06 08:16:24 +0530 |
| commit | 163ce34cda69b585a5da418fe350b6dd3975b141 (patch) | |
| tree | ce8f035d587a71010b3dabddc2a54291d4f67846 | |
| parent | c8bc1d518781e884f3079bb67101173dd3df16ad (diff) | |
| download | faker-163ce34cda69b585a5da418fe350b6dd3975b141.tar.xz faker-163ce34cda69b585a5da418fe350b6dd3975b141.zip | |
[api] Improved random methods
* Adds `random.word`
* Adds `random.words`
* Adds `random.image` shortcut
| -rw-r--r-- | lib/random.js | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/lib/random.js b/lib/random.js index fc7e54d9..33cf02b7 100644 --- a/lib/random.js +++ b/lib/random.js @@ -71,10 +71,67 @@ function Random (faker, seed) { return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders); } - this.boolean =function () { + this.boolean = function () { return !!faker.random.number(1) } + // TODO: have ability to return specific type of word? As in: noun, adjective, verb, etc + this.word = function randomWord (type) { + + var wordMethods = [ + 'commerce.department', + 'commerce.productName', + 'commerce.productAdjective', + 'commerce.productMaterial', + 'commerce.product', + 'commerce.color', + + 'company.catchPhraseAdjective', + 'company.catchPhraseDescriptor', + 'company.catchPhraseNoun', + 'company.bsAdjective', + 'company.bsBuzz', + 'company.bsNoun', + 'address.streetSuffix', + 'address.county', + 'address.country', + 'address.state', + + 'finance.accountName', + 'finance.transactionType', + 'finance.currencyName', + + 'hacker.noun', + 'hacker.verb', + 'hacker.adjective', + 'hacker.ingverb', + 'hacker.abbreviation', + + 'name.jobDescriptor', + 'name.jobArea', + 'name.jobType']; + + // randomly pick from the many faker methods that can generate words + var randomWordMethod = faker.random.arrayElement(wordMethods); + return faker.fake('{{' + randomWordMethod + '}}'); + + } + + this.words = function randomWords (count) { + var words = []; + if (typeof count === "undefined") { + count = faker.random.number({min:1, max: 3}); + } + for (var i = 0; i<count; i++) { + words.push(faker.random.word()); + } + return words.join(' '); + } + + this.image = function randomImage () { + return faker.image.image(); + } + return this; } |
