aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJakub Mandula <[email protected]>2017-02-26 14:04:31 +0000
committerJakub Mandula <[email protected]>2017-02-26 14:04:31 +0000
commitbf646e30e1981e094aaea89b82d68933b575a8df (patch)
treedbe7c0e541f54e3bcdda9be14cb1533b2eda8be0 /lib
parent232a2a58d70935c57d311b5ba237347f703c2131 (diff)
downloadfaker-bf646e30e1981e094aaea89b82d68933b575a8df.tar.xz
faker-bf646e30e1981e094aaea89b82d68933b575a8df.zip
replace String.prototype.repeat with proper helper
Diffstat (limited to 'lib')
-rw-r--r--lib/helpers.js36
1 files changed, 19 insertions, 17 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)