From bf646e30e1981e094aaea89b82d68933b575a8df Mon Sep 17 00:00:00 2001 From: Jakub Mandula Date: Sun, 26 Feb 2017 14:04:31 +0000 Subject: replace String.prototype.repeat with proper helper --- lib/helpers.js | 36 +++++++++++++++++++----------------- test/helpers.unit.js | 8 ++++---- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/helpers.js b/lib/helpers.js index 6fa763c8..8f3b5c71 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,18 +1,3 @@ -/** Define the String.prototype.repeat if it is not implemented.... See PR #382 -*/ -if(typeof String.prototype.repeat === "undefined") { - String.prototype.repeat = function(num) { - if(typeof num ==="undefined") { - num = 0; - } - var text = ""; - for(var i = 0; i < num; i++){ - text += this.toString(); - } - return text; - }; -} - /** * * @namespace faker.helpers @@ -127,6 +112,23 @@ var Helpers = function (faker) { return string.replace("L",checkNum); }; + /** string repeat helper, alternative to String.prototype.repeat.... See PR #382 + * + * @method faker.helpers.repeatString + * @param {string} string + * @param {number} num + */ + self.repeatString = function(string,num) { + if(typeof num ==="undefined") { + num = 0; + } + var text = ""; + for(var i = 0; i < num; i++){ + text += string.toString(); + } + return text; + }; + /** * parse string paterns in a similar way to RegExp * @@ -153,14 +155,14 @@ var Helpers = function (faker) { min = tmp; } repetitions = faker.random.number({min:min,max:max}); - string = string.slice(0,token.index)+ token[1].repeat(repetitions) + string.slice(token.index+token[0].length); + string = string.slice(0,token.index) + faker.helpers.repeatString(token[1], repetitions) + string.slice(token.index+token[0].length); token = string.match(RANGE_REP_REG); } // Deal with repeat `{num}` token = string.match(REP_REG); while(token !== null){ repetitions = parseInt(token[2]); - string = string.slice(0,token.index)+ token[1].repeat(repetitions) + string.slice(token.index+token[0].length); + string = string.slice(0,token.index)+ faker.helpers.repeatString(token[1], repetitions) + string.slice(token.index+token[0].length); token = string.match(REP_REG); } // Deal with range `[min-max]` (only works with numbers for now) diff --git a/test/helpers.unit.js b/test/helpers.unit.js index 000e12fe..b440f33e 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -98,15 +98,15 @@ describe("helpers.js", function () { assert.ok(string.length <= 10 && string.length >= 5); assert.ok(string.match(/^\#{5,10}$/)); }); - it("flips teh range when min > max", function () { + it("flips the range when min > max", function () { var string = faker.helpers.regexpStyleStringParse("#{10,5}"); assert.ok(string.length <= 10 && string.length >= 5); assert.ok(string.match(/^\#{5,10}$/)); }); it("repeats string {n} number of times", function () { - assert.ok(faker.helpers.regexpStyleStringParse("%{10}") === "%".repeat(10)); - assert.ok(faker.helpers.regexpStyleStringParse("%{30}") === "%".repeat(30)); - assert.ok(faker.helpers.regexpStyleStringParse("%{5}") === "%".repeat(5)); + assert.ok(faker.helpers.regexpStyleStringParse("%{10}") === faker.helpers.repeatString("%",10)); + assert.ok(faker.helpers.regexpStyleStringParse("%{30}") === faker.helpers.repeatString("%",30)); + assert.ok(faker.helpers.regexpStyleStringParse("%{5}") === faker.helpers.repeatString("%",5)); }); it("creates a numerical range", function () { var string = faker.helpers.regexpStyleStringParse("Hello[0-9]"); -- cgit v1.2.3