Skip to content

Improper argument bug correction #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
* Results cache
*/

var isNumber = require('is-number');
var res = '';
var cache;



/**
* Expose `repeat`
*/
Expand All @@ -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 = '';
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"Jon Schlinkert <[email protected]> (http://twitter.com/jonschlinkert)",
"Linus Unnebäck <[email protected]> (http://linus.unnebäck.se)",
"Thijs Busser <[email protected]> (http://tbusser.net)",
"Titus <[email protected]> (wooorm.com)"
"Titus <[email protected]> (wooorm.com)",
"ggattoni (https://github.com/ggattoni)"
],
"repository": "jonschlinkert/repeat-string",
"bugs": {
Expand All @@ -26,6 +27,9 @@
"scripts": {
"test": "mocha"
},
"dependencies" : {
"is-number" : "^6.0.0"
},
"devDependencies": {
"ansi-cyan": "^0.1.1",
"benchmarked": "^0.2.5",
Expand Down Expand Up @@ -74,4 +78,4 @@
"verb"
]
}
}
}