diff --git a/index.js b/index.js index 4459afd..2c549e4 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,12 @@ * Results cache */ +var isNumber = require('is-number'); var res = ''; var cache; + + /** * Expose `repeat` */ @@ -38,16 +41,39 @@ 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'); } + + // 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 if (num === 1) return str; 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 = ''; 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 +}