Skip to content

Commit

Permalink
Merge pull request #158 from dreinert1994/master
Browse files Browse the repository at this point in the history
Fix strlen crashing when argument is not string
  • Loading branch information
chrean authored Nov 23, 2021
2 parents 2a4e6eb + 3ce6ac5 commit b5385ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ exports.options = options;
// see: http://en.wikipedia.org/wiki/ANSI_escape_code
//
exports.strlen = function(str){
str = typeof str === 'string' ? str : String(str);
var stripped = stripAnsi(str);
var split = stripped.split("\n");
return split.reduce(function (memo, s) { return (s.length > memo) ? s.length : memo }, 0);
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Module requirements.
*/

require('should');

var { strlen } = require('../lib/utils');

/**
* Tests.
*/


module.exports = {
'test strlen with string argument': function() {
strlen('hello\nme').should.equal(5);
},
'test strlen with number argument': function() {
strlen(5).should.equal(1);
},
}

0 comments on commit b5385ca

Please sign in to comment.