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 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'lib') 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) -- cgit v1.2.3