Skip to content

Commit

Permalink
Merge pull request #408 from paolo-tanium/allow-mac-to-specify-separator
Browse files Browse the repository at this point in the history
[api] Allow user to specify separator to be used in mac address
  • Loading branch information
Marak authored Mar 2, 2017
2 parents 8c43fe8 + 5cf0d78 commit 6cdb93e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/internet.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,23 @@ var Internet = function (faker) {
* mac
*
* @method faker.internet.mac
* @param {string} sep
*/
self.mac = function(){
var i, mac = "";
self.mac = function(sep){
var i,
mac = "",
validSep = ':';

// if the client passed in a different separator than `:`,
// we will use it if it is in the list of acceptable separators (dash or no separator)
if (['-', ''].indexOf(sep) !== -1) {
validSep = sep;
}

for (i=0; i < 12; i++) {
mac+= faker.random.number(15).toString(16);
if (i%2==1 && i != 11) {
mac+=":";
mac+=validSep;
}
}
return mac;
Expand Down
18 changes: 18 additions & 0 deletions test/internet.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,23 @@ describe("internet.js", function () {
var mac = faker.internet.mac();
assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
});

it("uses the dash separator if we pass it in as our separator", function () {
var mac = faker.internet.mac('-');
assert.ok(mac.match(/^([a-f0-9]{2}-){5}[a-f0-9]{2}$/));
});

it("uses no separator if we pass in an empty string", function() {
var mac = faker.internet.mac('');
assert.ok(mac.match(/^[a-f0-9]{12}$/));
});

it("uses the default colon (:) if we provide an unacceptable separator", function() {
var mac = faker.internet.mac('!');
assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));

mac = faker.internet.mac('&');
assert.ok(mac.match(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/));
});
});
});

0 comments on commit 6cdb93e

Please sign in to comment.