diff options
| author | Gokulnath Reddy <[email protected]> | 2017-06-19 22:42:54 +1000 |
|---|---|---|
| committer | Gokulnath Reddy <[email protected]> | 2017-06-19 22:42:54 +1000 |
| commit | df296352d46730b2e79a05506c2e416e5fce2c9d (patch) | |
| tree | aa502eaea4d9c06a41b9711105df11c701da136d | |
| parent | 6cdb93efcbcaf222ac061cee5532374f72ea073e (diff) | |
| download | faker-df296352d46730b2e79a05506c2e416e5fce2c9d.tar.xz faker-df296352d46730b2e79a05506c2e416e5fce2c9d.zip | |
Generate a hexaDecimal string
| -rw-r--r-- | Readme.md | 1 | ||||
| -rw-r--r-- | lib/random.js | 19 | ||||
| -rw-r--r-- | test/random.unit.js | 14 |
3 files changed, 34 insertions, 0 deletions
@@ -215,6 +215,7 @@ This will interpolate the format string with the value of methods `name.lastName * image * locale * alphaNumeric + * hexaDecimal * system * fileName * commonFileName diff --git a/lib/random.js b/lib/random.js index 17b74f64..21151213 100644 --- a/lib/random.js +++ b/lib/random.js @@ -210,6 +210,25 @@ function Random (faker, seed) { return wholeString; }; + /** + * hexaDecimal + * + * @method faker.random.hexaDecimal + * @param {number} count defaults to 1 + */ + this.hexaDecimal = function hexaDecimal(count) { + if (typeof count === "undefined") { + count = 1; + } + + var wholeString = ""; + for(var i = 0; i < count; i++) { + wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", "E", "F"]); + } + + return "0x"+wholeString; + }; + return this; } diff --git a/test/random.unit.js b/test/random.unit.js index 133fd887..d04bf280 100644 --- a/test/random.unit.js +++ b/test/random.unit.js @@ -135,4 +135,18 @@ describe("random.js", function () { assert.ok(alphaNumeric(5).length === 5); }) }) + + describe('hexaDecimal', function() { + var hexaDecimal = faker.random.hexaDecimal; + + it('should generate single hex character when no additional argument was provided', function() { + var hex = hexaDecimal(); + assert.ok(hex.match(/^(0x)[0-9a-f]{1}$/i)); + }) + + it('should generate a random hex string', function() { + var hex = hexaDecimal(5); + assert.ok(hex.match(/^(0x)[0-9a-f]+$/i)); + }) + }) }); |
