From f38741b1ab422e837e38b7cea8238bb5161d1cc9 Mon Sep 17 00:00:00 2001 From: ggattoni <38949097+ggattoni@users.noreply.github.com> Date: Wed, 7 Nov 2018 15:41:40 +0100 Subject: [PATCH 1/5] Corrected bug in input arguments Fixed improper input parameter causing NaN wander around code. Added common base case for performance improvement. Fixed improper input parameter being negative and causing potential improper behaviour. --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 4459afd..d416b4c 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ * Results cache */ +const isNumber = require('is-number'); var res = ''; var cache; @@ -42,10 +43,15 @@ function repeat(str, num) { if (typeof str !== 'string') { throw new TypeError('expected a string'); } + + if (!(isNumber(num) && Number.isInteger(num) && num >= 0)){ + throw new TypeError('expected a positive integer number'); + } // cover common, quick use cases if (num === 1) return str; if (num === 2) return str + str; + if (num === 0) return ""; var max = str.length * num; if (cache !== str || typeof cache === 'undefined') { From 1934dc9148258c45d2c2e2c0f124504a6b7dcaf6 Mon Sep 17 00:00:00 2001 From: ggattoni <38949097+ggattoni@users.noreply.github.com> Date: Wed, 7 Nov 2018 15:46:52 +0100 Subject: [PATCH 2/5] Update package.json Added dependency --- package.json | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index fc0d190..602a3bf 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "Jon Schlinkert (http://twitter.com/jonschlinkert)", "Linus Unnebäck (http://linus.unnebäck.se)", "Thijs Busser (http://tbusser.net)", - "Titus (wooorm.com)" + "Titus (wooorm.com)", + "ggattoni (https://github.com/ggattoni)" ], "repository": "jonschlinkert/repeat-string", "bugs": { @@ -26,6 +27,9 @@ "scripts": { "test": "mocha" }, + "dependencies" : { + "is-number" : "^6.0.0" + }, "devDependencies": { "ansi-cyan": "^0.1.1", "benchmarked": "^0.2.5", @@ -74,4 +78,4 @@ "verb" ] } -} \ No newline at end of file +} From eec9ff30acefaacf7afb31c8f6f923355d46439b Mon Sep 17 00:00:00 2001 From: ggattoni <38949097+ggattoni@users.noreply.github.com> Date: Wed, 7 Nov 2018 23:54:22 +0100 Subject: [PATCH 3/5] Fix for passing tests Now all tests pass. It should be stated somewhere in the documentation that the function repeat can accept a string as argument "num", it is not not very intuitive, and also can leads to problem if the string does not represent an integer. Now in that case a TypeError is thrown. It can also be passed as argument a negative integer and it does not make any sense actually, so now even in that case a TypeError is thrown. Added check for null and undefined cases, now needed due to the new check on argument num --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d416b4c..ba58d2b 100644 --- a/index.js +++ b/index.js @@ -44,8 +44,13 @@ function repeat(str, num) { throw new TypeError('expected a string'); } - if (!(isNumber(num) && Number.isInteger(num) && num >= 0)){ - throw new TypeError('expected a positive integer number'); + // cover null or undefined cases + if (num === null || num === undefined) { + return ""; + } + + if (!(isNumber(num) && Number.isInteger(typeof num !== 'string' ? num : Number(num)) && num >= 0)) { + throw new TypeError('expected a positive integer number '); } // cover common, quick use cases @@ -54,6 +59,7 @@ function repeat(str, num) { if (num === 0) return ""; var max = str.length * num; + if (cache !== str || typeof cache === 'undefined') { cache = str; res = ''; From 5a7545dec02ece6c8ec72f1b1515e733cc1d7ec1 Mon Sep 17 00:00:00 2001 From: ggattoni <38949097+ggattoni@users.noreply.github.com> Date: Thu, 8 Nov 2018 10:44:48 +0100 Subject: [PATCH 4/5] Fixed compatibility with node 0.10/0.12 Now repeat-string module is compatible with node v0.10/0.12 --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ba58d2b..d17ad5a 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ * Results cache */ -const isNumber = require('is-number'); +var isNumber = require('is-number'); var res = ''; var cache; @@ -58,8 +58,7 @@ function repeat(str, num) { if (num === 2) return str + str; if (num === 0) return ""; - var max = str.length * num; - + var max = str.length * num; if (cache !== str || typeof cache === 'undefined') { cache = str; res = ''; From 9d9b85940d80866930f642b31efaa049bffd3b4a Mon Sep 17 00:00:00 2001 From: ggattoni <38949097+ggattoni@users.noreply.github.com> Date: Thu, 8 Nov 2018 11:13:27 +0100 Subject: [PATCH 5/5] Fixed node v0.10 compatibilty error Added fix to missing Number.isInteger method in node v0.10 --- index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/index.js b/index.js index d17ad5a..2c549e4 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,8 @@ var isNumber = require('is-number'); var res = ''; var cache; + + /** * Expose `repeat` */ @@ -39,6 +41,19 @@ module.exports = repeat; * @api public */ +function isInt(input){ + if(!isNumber(input)){ + throw new TypeError("input must be a number. Your input was " + typeof input); + }else{ + return (input % 1) === 0; + } +} + +//prototype modification to allow node v0.10.0 to have access to the isInteger function. +if(! Number.isInteger){ + Number.isInteger = isInt; +} + function repeat(str, num) { if (typeof str !== 'string') { throw new TypeError('expected a string');