Skip to content

Commit

Permalink
Generate a hexaDecimal string
Browse files Browse the repository at this point in the history
  • Loading branch information
ygnr committed Jun 19, 2017
1 parent 6cdb93e commit df29635
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ This will interpolate the format string with the value of methods `name.lastName
* image
* locale
* alphaNumeric
* hexaDecimal
* system
* fileName
* commonFileName
Expand Down
19 changes: 19 additions & 0 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Expand Down
14 changes: 14 additions & 0 deletions test/random.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
})
})
});

0 comments on commit df29635

Please sign in to comment.