aboutsummaryrefslogtreecommitdiff
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
parent232a2a58d70935c57d311b5ba237347f703c2131 (diff)
downloadfaker-bf646e30e1981e094aaea89b82d68933b575a8df.tar.xz
faker-bf646e30e1981e094aaea89b82d68933b575a8df.zip
replace String.prototype.repeat with proper helper
-rw-r--r--lib/helpers.js36
-rw-r--r--test/helpers.unit.js8
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]");