diff options
| author | Tyler <[email protected]> | 2017-10-15 13:42:17 -0700 |
|---|---|---|
| committer | Tyler <[email protected]> | 2017-10-15 13:42:17 -0700 |
| commit | ef6bec454298f9582ba84dcdd661001586401e60 (patch) | |
| tree | 87110a1b52be166782bd94c5df557fd033ffebee | |
| parent | 16ccfa16489be1533297158ee9f1ab5dab1a3b0f (diff) | |
| download | faker-ef6bec454298f9582ba84dcdd661001586401e60.tar.xz faker-ef6bec454298f9582ba84dcdd661001586401e60.zip | |
add random.alpaha with tests
| -rw-r--r-- | lib/random.js | 31 | ||||
| -rw-r--r-- | test/random.unit.js | 20 |
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/random.js b/lib/random.js index 8dccf376..1f408fe0 100644 --- a/lib/random.js +++ b/lib/random.js @@ -219,6 +219,37 @@ function Random (faker, seed) { return faker.random.arrayElement(Object.keys(faker.locales)); }; + /** + * alpha. returns lower/upper alpha characters based count and upcase options + * + * @method faker.random.alpha + * @param {mixed} options // defaults to { count: 1, upcase: false } + */ + this.alpha = function alpha(options) { + if (typeof options === "undefined") { + options = { + count: 1 + } + } else if (typeof options === "number") { + options = { + count: options, + } + } else if (typeof options.count === "undefined") { + options.count = 1 + } + + if (typeof options.upcase === "undefined") { + options.upcase = false; + } + + var wholeString = ""; + for(var i = 0; i < options.count; i++) { + wholeString += faker.random.arrayElement(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]); + } + + return options.upcase ? wholeString.toUpperCase() : wholeString; + }; + /** * alphaNumeric * diff --git a/test/random.unit.js b/test/random.unit.js index e10d36be..3f7a50a8 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -164,6 +164,26 @@ describe("random.js", function () { }); }); + describe('alpha', function() { + var alpha = faker.random.alpha; + + it('should return single letter when no count provided', function() { + assert.ok(alpha().length === 1); + }) + + it('should return lowercase letter when no upcase option provided', function() { + assert.ok(alpha().match(/[a-z]/)); + }) + + it('should return uppercase when upcase option is true', function() { + assert.ok(alpha({ upcase: true }).match(/[A-Z]/)); + }) + + it('should generate many random letters', function() { + assert.ok(alpha(5).length === 5); + }) + }) + describe('alphaNumeric', function() { var alphaNumeric = faker.random.alphaNumeric; |
