Skip to content
Browse files

Handle empty-string input, added tests

  • Loading branch information...
1 parent b4baf18 commit 223f18a28f950011b53600faf390af8dfb3d42c6 mde committed
Showing with 106 additions and 114 deletions.
  1. +99 −113 lib/string.js
  2. +7 −1 test/string.js
View
212 lib/string.js
@@ -95,14 +95,23 @@ string = new (function () {
// that need unescaping
this.needsUnescape = _buildEscapeTest('from');
- // Escape regex control-characters in strings you need to use to build
- // regexes dynamically
+ /**
+ @name string#escapeRegExpChars
+ @public
+ @function
+ @return {String} A string of escaped characters
+ @description Escapes regex control-characters in strings
+ used to build regexes dynamically
+ @param {String} string The string of chars to escape
+ */
this.escapeRegExpChars = (function() {
var specials = [ '^', '/', '.', '*', '+', '?', '|', '(', ')',
'[', ']', '{', '}', '\\' ];
sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
- return function (text) {
- return text.replace(sRE, '\\$1');
+ return function (string) {
+ var str = string || '';
+ str = String(str);
+ return str.replace(sRE, '\\$1');
};
}).call(this);
@@ -115,15 +124,13 @@ string = new (function () {
@param {String} string The string to convert
*/
this.toArray = function(string) {
- if (!string) {
- return;
- }
-
- var arr = []
+ var str = string || ''
+ , arr = []
, i = -1;
+ str = String(str);
- while(++i < string.length) {
- arr.push(string.substr(i, 1));
+ while (++i < str.length) {
+ arr.push(str.substr(i, 1));
}
return arr;
@@ -138,10 +145,9 @@ string = new (function () {
@param {String} string The string to reverse
*/
this.reverse = function(string) {
- if (!string) {
- return;
- }
- return this.toArray(string).reverse().join('');
+ var str = string || '';
+ str = String(str);
+ return this.toArray(str).reverse().join('');
};
/**
@@ -155,12 +161,11 @@ string = new (function () {
@param {String} char The character to trim
*/
this.ltrim = function(string, char) {
- if (!string) {
- return;
- }
+ var str = string || ''
+ , pat = char ? new RegExp('^' + char + '+') : _LTR;
+ str = String(str);
- var pat = char ? new RegExp('^' + char + '+') : _LTR;
- return string.replace(pat, '');
+ return str.replace(pat, '');
};
/**
@@ -174,12 +179,11 @@ string = new (function () {
@param {String} char The character to trim
*/
this.rtrim = function (string, char) {
- if (!string) {
- return;
- }
+ var str = string || ''
+ , pat = char ? new RegExp(char + '+$') : _RTR;
+ str = String(str);
- var pat = char ? new RegExp(char + '+$') : _RTR;
- return string.replace(pat, '');
+ return str.replace(pat, '');
};
// Alias
@@ -196,12 +200,11 @@ string = new (function () {
@param {String} char The character to trim
*/
this.trim = function (string, char) {
- if (!string) {
- return;
- }
+ var str = string || ''
+ , pat = char ? new RegExp('^' + char + '+|' + char + '+$', 'g') : _TR;
+ str = String(str);
- var pat = char ? new RegExp('^' + char + '+|' + char + '+$', 'g') : _TR;
- return string.replace(pat, '');
+ return str.replace(pat, '');
};
/**
@@ -232,14 +235,6 @@ string = new (function () {
};
/**
- * lpad(string<String>, char<String>, width<Number>)
- *
- * Lpad adds `char` to the left of `string` until the length
- * of `string` is more than `width`
- *
- * Examples:
- * lpad("geddy", "&", 7)
- * => "&&geddy"
@name string#lpad
@public
@function
@@ -251,19 +246,18 @@ string = new (function () {
@param {Number} width the width to pad to
*/
this.lpad = function(string, char, width) {
- if (!string) {
- return;
- }
+ var str = string || ''
+ , width;
+ str = String(str);
// Should width be string.length + 1? or the same to be safe
- width = Number(width) || string.length;
+ width = parseInt(width, 10) || str.length;
char = char || ' ';
- var s = string.toString();
- while(s.length < width) {
- s = char + s;
+ while(str.length < width) {
+ str = char + str;
}
- return s;
+ return str;
};
/**
@@ -278,19 +272,18 @@ string = new (function () {
@param {Number} width the width to pad to
*/
this.rpad = function(string, char, width) {
- if (!string) {
- return;
- }
+ var str = string || ''
+ , width;
+ str = String(str);
// Should width be string.length + 1? or the same to be safe
- width = Number(width) || string.length;
+ width = parseInt(width, 10) || str.length;
char = char || ' ';
- var s = string;
- while(s.length < width) {
- s += char;
+ while(str.length < width) {
+ str += char;
}
- return s;
+ return str;
};
/**
@@ -305,19 +298,18 @@ string = new (function () {
@param {Number} width the width to pad to
*/
this.pad = function(string, char, width) {
- if (!string) {
- return;
- }
+ var str = string || ''
+ , width;
+ str = String(str);
// Should width be string.length + 1? or the same to be safe
- width = Number(width) || string.length;
+ width = parseInt(width, 10) || str.length;
char = char || ' ';
- var s = string;
- while(s.length < width) {
- s = char + s + char;
+ while(str.length < width) {
+ str = char + str + char;
}
- return s;
+ return str;
};
/**
@@ -338,11 +330,8 @@ string = new (function () {
@param {Function} callback Callback is called only if a truncation is done
*/
this.truncate = function(string, options, callback) {
- if (!string) {
- return;
- }
-
- var stringLen = string.length
+ var str = string || ''
+ , stringLen
, opts
, stringLenWithOmission
, last
@@ -354,6 +343,9 @@ string = new (function () {
, result
, returnString;
+ str = String(str);
+ stringLen = str.length
+
// If `options` is a number, assume it's the length and
// create a options object with length
if (typeof options === 'number') {
@@ -366,7 +358,7 @@ string = new (function () {
}
// Set `opts` defaults
- opts.length = opts.length || string.length;
+ opts.length = opts.length || stringLen;
opts.omission = opts.omission || opts.ellipsis || '...';
stringLenWithOmission = opts.length - opts.omission.length;
@@ -383,7 +375,7 @@ string = new (function () {
opts.seperator = new RegExp(opts.seperator.source,
'g' + ignoreCase + multiLine);
}
- stringToWorkWith = string.substring(0, stringLenWithOmission + 1)
+ stringToWorkWith = str.substring(0, stringLenWithOmission + 1)
lastIndexOf = -1
nextStop = 0
@@ -395,7 +387,7 @@ string = new (function () {
}
else {
// Seperator is a String
- last = string.lastIndexOf(opts.seperator, stringLenWithOmission);
+ last = str.lastIndexOf(opts.seperator, stringLenWithOmission);
}
// If the above couldn't be found, they'll default to -1 so,
@@ -409,10 +401,10 @@ string = new (function () {
}
if (stringLen < opts.length) {
- return string;
+ return str;
}
else {
- returnString = string.substring(0, last) + opts.omission;
+ returnString = str.substring(0, last) + opts.omission;
returnString += callback ? callback() : '';
return returnString;
}
@@ -436,12 +428,12 @@ string = new (function () {
@param {Function} callback Callback is called only if a truncation is done
*/
this.truncateHTML = function(string, options, callback) {
- if (!string) {
- return;
- }
- var returnString = ''
+ var str = string || ''
+ , returnString = ''
, opts = options;
+ str = String(str);
+
// If `options` is a number assume it's the length and create a options object with length
if (typeof opts === 'number') {
var num = opts;
@@ -456,7 +448,7 @@ string = new (function () {
var pat = /(<[^>]*>)/ // Patter for matching HTML tags
, arr = [] // Holds the HTML tags and content seperately
, truncated = false
- , result = pat.exec(string)
+ , result = pat.exec(str)
, item
, firstPos
, lastPos
@@ -469,18 +461,18 @@ string = new (function () {
if (firstPos !== 0) {
// Should be content not HTML tags
- arr.push(string.substring(0, firstPos));
+ arr.push(str.substring(0, firstPos));
// Slice content from string
- string = string.slice(firstPos);
+ str = str.slice(firstPos);
}
arr.push(result[0]); // Push HTML tags
- string = string.slice(result[0].length);
+ str = str.slice(result[0].length);
// Re-run the pattern on the new string
- result = pat.exec(string);
+ result = pat.exec(str);
}
- if (string !== '') arr.push(string);
+ if (str !== '') arr.push(str);
// Loop through array items appending the tags to the string,
// - and truncating the text then appending it to content
@@ -523,11 +515,10 @@ string = new (function () {
@param {String} string The string to convert
*/
this.nl2br = function(string) {
- if (!string) {
- return;
- }
+ var str = string || '';
+ str = String(str);
- return string.replace(_NL,'<br />');
+ return str.replace(_NL,'<br />');
};
/**
@@ -536,19 +527,18 @@ string = new (function () {
@function
@return {String} The string in a snake_case version
@description Snakeize converts camelCase and CamelCase strings to snake_case strings
- @param {String} str The string to convert to snake_case
+ @param {String} string The string to convert to snake_case
@param {String} separ='_' The seperator to use
*/
this.snakeize = (function () {
// Only create regexes once on initial load
var repl = /([A-Z]+)/g
, lead = /^_/g;
- return function (str, separ) {
- if (!str) {
- return;
- }
- var sep = separ || '_'
+ return function (string, separ) {
+ var str = string || ''
+ , sep = separ || '_'
, leading = separ ? new RegExp('^' + sep, 'g') : lead;
+ str = String(str);
return str.replace(repl, sep + '$1').toLowerCase().
replace(leading, '');
};
@@ -565,7 +555,7 @@ string = new (function () {
@return {String} The string in a camelCase version
@description Camelize takes a string and optional options and
returns a camelCase version of the given `string`
- @param {String} str The string to convert to camelCase
+ @param {String} string The string to convert to camelCase
@param {Object} options
@param {Boolean} [options.initialCap] If initialCap is true the returned
string will have a capitalized first letter
@@ -577,17 +567,16 @@ string = new (function () {
this.camelize = (function () {
// Only create regex once on initial load
var repl = /[-_](\w)/g;
- return function (str, options) {
- var ret
+ return function (string, options) {
+ var str = string || ''
+ , ret
, config = {
initialCap: false
, leadingUnderscore: false
}
, opts = options || {};
- if (!str) {
- return;
- }
+ str = String(str);
// Backward-compat
if (typeof opts == 'boolean') {
@@ -622,11 +611,10 @@ string = new (function () {
@param {String} string The string to decapitalize
*/
this.decapitalize = function (string) {
- if (!string) {
- return;
- }
+ var str = string || '';
+ str = String(str);
- return string.substr(0, 1).toLowerCase() + string.substr(1);
+ return str.substr(0, 1).toLowerCase() + str.substr(1);
};
/**
@@ -638,11 +626,10 @@ string = new (function () {
@param {String} string The string to capitalize
*/
this.capitalize = function (string) {
- if (!string) {
- return;
- }
+ var str = string || '';
+ str = String(str);
- return string.substr(0, 1).toUpperCase() + string.substr(1);
+ return str.substr(0, 1).toUpperCase() + str.substr(1);
};
/**
@@ -652,12 +639,12 @@ string = new (function () {
@return {String} The string in a dashed version
@description Dasherize returns the given `string` converting camelCase and snakeCase
to dashes or replace them with the `replace` character.
- @param {String} s The string to dasherize
+ @param {String} string The string to dasherize
@param {String} replace='-' The character to replace with
*/
- this.dasherize = function(s, replace) {
+ this.dasherize = function(string, replace) {
var repl = replace || '-'
- return this.snakeize(s, repl);
+ return this.snakeize(string, repl);
};
/**
@@ -670,11 +657,10 @@ string = new (function () {
@param {String} string The string to underscorize
*/
this.underscorize = function(string) {
- if (!string) {
- return;
- }
+ var str = string || '';
+ str = String(str);
- return this.dasherize(string, '_');
+ return this.dasherize(str, '_');
};
/*
View
8 test/string.js
@@ -22,7 +22,13 @@ var assert = require('assert')
tests = {
- 'test toArray for string': function() {
+ 'test escapeRegExpCharacters': function () {
+ var expected = '\\^\\/\\.\\*\\+\\?\\|\\(\\)\\[\\]\\{\\}\\\\'
+ actual = string.escapeRegExpChars('^/.*+?|()[]{}\\');
+ assert.equal(expected, actual);
+ }
+
+, 'test toArray for string': function() {
var data = string.toArray('geddy')
, actual = ['g', 'e', 'd', 'd', 'y'];

0 comments on commit 223f18a

Please sign in to comment.
Something went wrong with that request. Please try again.