Skip to content

Commit 3de0691

Browse files
committed
alphaNumeric takes now count as a argument
1 parent 649843a commit 3de0691

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/random.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,20 @@ function Random (faker, seed) {
193193
* alphaNumeric
194194
*
195195
* @method faker.random.alphaNumeric
196+
* @param {number} count defaults to 1
196197
*/
197-
this.alphaNumeric = function alphaNumeric() {
198-
return faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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"]);
199-
}
198+
this.alphaNumeric = function alphaNumeric(count) {
199+
if (typeof count === "undefined") {
200+
count = 1;
201+
}
202+
203+
var wholeString = "";
204+
for(var i = 0; i < count; i++) {
205+
wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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"]);
206+
}
207+
208+
return wholeString;
209+
};
200210

201211
return this;
202212

test/random.unit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,16 @@ describe("random.js", function () {
115115
assert.ok(/^\d+\.\d+\.\d+$/.test(semver));
116116
});
117117
});
118+
119+
describe('alphaNumeric', function() {
120+
var alphaNumeric = faker.random.alphaNumeric;
121+
122+
it('should generate single character when no additional argument was provided', function() {
123+
assert.ok(alphaNumeric().length === 1);
124+
})
125+
126+
it('should generate many random characters', function() {
127+
assert.ok(alphaNumeric(5).length === 5);
128+
})
129+
})
118130
});

0 commit comments

Comments
 (0)