underscore.string
Advanced tools
Comparing version 1.0.0 to 1.1.3
@@ -1,8 +0,8 @@ | ||
(function(){function j(b){if(b)return e.escapeRegExp(b);return"\\s"}var k=String.prototype.trim,e={isBlank:function(b){return!!b.match(/^\s*$/)},capitalize:function(b){return b.charAt(0).toUpperCase()+b.substring(1).toLowerCase()},chop:function(b,a){a=a||b.length;for(var c=[],f=0;f<b.length;){c.push(b.slice(f,f+a));f+=a}return c},clean:function(b){return e.strip(b.replace(/\s+/g," "))},isContains:function(b,a){return b.indexOf(a)!==-1},count:function(b,a){for(var c=0,f,d=0;d<b.length;){f=b.indexOf(a, | ||
d);f>=0&&c++;d=d+(f>=0?f:0)+a.length}return c},escapeHTML:function(b){return String(b||"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")},unescapeHTML:function(b){return String(b||"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")},escapeRegExp:function(b){return String(b||"").replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1")},insert:function(b,a,c){b=b.split("");b.splice(a,0,c);return b.join("")},join:function(b){b=String(b);for(var a="",c=1;c<arguments.length;c+=1){a+= | ||
String(arguments[c]);if(c!==arguments.length-1)a+=b}return a},reverse:function(b){return Array.prototype.reverse.apply(b.split("")).join("")},splice:function(b,a,c,f){b=b.split("");b.splice(a,c,f);return b.join("")},isStartsWith:function(b,a){return b.length>=a.length&&b.substring(0,a.length)===a},isEndsWith:function(b,a){return b.length>=a.length&&b.substring(b.length-a.length)===a},succ:function(b){var a=b.split("");a.splice(b.length-1,1,String.fromCharCode(b.charCodeAt(b.length-1)+1));return a.join("")}, | ||
titleize:function(b){b=b.split(" ");for(var a,c=0;c<b.length;c++){a=b[c].split("");if(typeof a[0]!=="undefined")a[0]=a[0].toUpperCase();c+1===b.length?b[c]=a.join(""):b[c]=a.join("")+" "}return b.join("")},trim:function(b,a){if(!a&&k)return k.call(b);a=j(a);return b.replace(RegExp("^["+a+"]+|["+a+"]+$","g"),"")},ltrim:function(b,a){a=j(a);return b.replace(RegExp("^["+a+"]+","g"),"")},rtrim:function(b,a){a=j(a);return b.replace(RegExp("["+a+"]+$","g"),"")},truncate:function(b,a,c){c=c||"...";return b.slice(0, | ||
a)+c},sprintf:function(){for(var b=0,a,c=arguments[b++],f=[],d,g,h;c;){if(d=/^[^\x25]+/.exec(c))f.push(d[0]);else if(d=/^\x25{2}/.exec(c))f.push("%");else if(d=/^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(c)){if((a=arguments[d[1]||b++])==null||a==undefined)throw"Too few arguments.";if(/[^s]/.test(d[7])&&typeof a!="number")throw"Expecting number but found "+typeof a;switch(d[7]){case "b":a=a.toString(2);break;case "c":a=String.fromCharCode(a);break;case "d":a=parseInt(a); | ||
break;case "e":a=d[6]?a.toExponential(d[6]):a.toExponential();break;case "f":a=d[6]?parseFloat(a).toFixed(d[6]):parseFloat(a);break;case "o":a=a.toString(8);break;case "s":a=(a=String(a))&&d[6]?a.substring(0,d[6]):a;break;case "u":a=Math.abs(a);break;case "x":a=a.toString(16);break;case "X":a=a.toString(16).toUpperCase()}a=/[def]/.test(d[7])&&d[2]&&a>=0?"+"+a:a;g=d[3]?d[3]=="0"?"0":d[3].charAt(1):" ";h=d[5]-String(a).length-0;if(d[5]){g=g;h=h;for(var l=[];h>0;l[--h]=g);g=l.join("")}else g="";g=g; | ||
f.push(""+(d[4]?a+g:g+a))}else throw"Huh ?!";c=c.substring(d[0].length)}return f.join("")}};e.strip=e.trim;e.lstrip=e.ltrim;e.rstrip=e.rtrim;e.isIncludes=e.isContains;var i=function(b,a){e[a]=function(){typeof console!="undefined"&&typeof console.log!="undefined"&&console.log('Deprication warning: use "'+b+'" instead "'+a+'". "'+a+'" will be removed after "undescore.string" 1.1');return e[b].apply(e,arguments)}};i("isStartsWith","startsWith");i("isEndsWith","endsWith");i("isContains","contains"); | ||
i("isBlank","blank");i("isIncludes","includes");if(typeof window==="undefined"&&typeof module!=="undefined")module.exports=e;else if(typeof this._!=="undefined")this._.mixin(e);else this._=e})(); | ||
(function(){function g(b){if(b)return e.escapeRegExp(b);return"\\s"}var h=String.prototype.trim,e={isBlank:function(b){return!!b.match(/^\s*$/)},capitalize:function(b){return b.charAt(0).toUpperCase()+b.substring(1).toLowerCase()},chop:function(b,a){a=a||b.length;for(var c=[],f=0;f<b.length;)c.push(b.slice(f,f+a)),f+=a;return c},clean:function(b){return e.strip(b.replace(/\s+/g," "))},count:function(b,a){for(var c=0,f,d=0;d<b.length;)f=b.indexOf(a,d),f>=0&&c++,d=d+(f>=0?f:0)+a.length;return c},chars:function(b){return b.split("")}, | ||
escapeHTML:function(b){return String(b||"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")},unescapeHTML:function(b){return String(b||"").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")},escapeRegExp:function(b){return String(b||"").replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1")},insert:function(b,a,c){b=b.split("");b.splice(a,0,c);return b.join("")},includes:function(b,a){return b.indexOf(a)!==-1},join:function(b){b=String(b);for(var a="",c=1;c<arguments.length;c+=1)a+= | ||
String(arguments[c]),c!==arguments.length-1&&(a+=b);return a},lines:function(b){return b.split("\n")},splice:function(b,a,c,f){b=b.split("");b.splice(a,c,f);return b.join("")},startsWith:function(b,a){return b.length>=a.length&&b.substring(0,a.length)===a},endsWith:function(b,a){return b.length>=a.length&&b.substring(b.length-a.length)===a},succ:function(b){var a=b.split("");a.splice(b.length-1,1,String.fromCharCode(b.charCodeAt(b.length-1)+1));return a.join("")},titleize:function(b){b=b.split(" "); | ||
for(var a,c=0;c<b.length;c++)a=b[c].split(""),typeof a[0]!=="undefined"&&(a[0]=a[0].toUpperCase()),c+1===b.length?b[c]=a.join(""):b[c]=a.join("")+" ";return b.join("")},camelize:function(b){return e.trim(b).replace(/(\-|_|\s)+(.)?/g,function(a,b,f){return f?f.toUpperCase():""})},underscored:function(b){return e.trim(b).replace(/([a-z\d])([A-Z]+)/g,"$1_$2").replace(/\-|\s+/g,"_").toLowerCase()},dasherize:function(b){return e.trim(b).replace(/([a-z\d])([A-Z]+)/g,"$1-$2").replace(/^([A-Z]+)/,"-$1").replace(/\_|\s+/g, | ||
"-").toLowerCase()},trim:function(b,a){if(!a&&h)return h.call(b);a=g(a);return b.replace(RegExp("^["+a+"]+|["+a+"]+$","g"),"")},ltrim:function(b,a){a=g(a);return b.replace(RegExp("^["+a+"]+","g"),"")},rtrim:function(b,a){a=g(a);return b.replace(RegExp("["+a+"]+$","g"),"")},truncate:function(b,a,c){return b.slice(0,a)+(c||"...")},words:function(b,a){return b.split(a||" ")},sprintf:function(){for(var b=0,a,c=arguments[b++],f=[],d,e,g;c;){if(d=/^[^\x25]+/.exec(c))f.push(d[0]);else if(d=/^\x25{2}/.exec(c))f.push("%"); | ||
else if(d=/^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(c)){if((a=arguments[d[1]||b++])==null||a==void 0)throw"Too few arguments.";if(/[^s]/.test(d[7])&&typeof a!="number")throw"Expecting number but found "+typeof a;switch(d[7]){case "b":a=a.toString(2);break;case "c":a=String.fromCharCode(a);break;case "d":a=parseInt(a);break;case "e":a=d[6]?a.toExponential(d[6]):a.toExponential();break;case "f":a=d[6]?parseFloat(a).toFixed(d[6]):parseFloat(a);break;case "o":a=a.toString(8); | ||
break;case "s":a=(a=String(a))&&d[6]?a.substring(0,d[6]):a;break;case "u":a=Math.abs(a);break;case "x":a=a.toString(16);break;case "X":a=a.toString(16).toUpperCase()}a=/[def]/.test(d[7])&&d[2]&&a>=0?"+"+a:a;e=d[3]?d[3]=="0"?"0":d[3].charAt(1):" ";g=d[5]-String(a).length-0;if(d[5]){for(var h=[];g>0;h[--g]=e);e=h.join("")}else e="";f.push(""+(d[4]?a+e:e+a))}else throw"Huh ?!";c=c.substring(d[0].length)}return f.join("")}};e.strip=e.trim;e.lstrip=e.ltrim;e.rstrip=e.rtrim;typeof window==="undefined"&& | ||
typeof module!=="undefined"?module.exports=e:typeof this._!=="undefined"?this._.mixin(e):this._=e})(); |
@@ -7,3 +7,3 @@ // Underscore.string | ||
// Version 1.0.0 | ||
// Version 1.1.3 | ||
@@ -54,6 +54,2 @@ (function(){ | ||
isContains: function(str, needle){ | ||
return str.indexOf(needle) !== -1; | ||
}, | ||
count: function(str, substr){ | ||
@@ -69,2 +65,6 @@ var count = 0, index; | ||
chars: function(str) { | ||
return str.split(''); | ||
}, | ||
escapeHTML: function(str) { | ||
@@ -89,2 +89,6 @@ return String(str||'').replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); | ||
includes: function(str, needle){ | ||
return str.indexOf(needle) !== -1; | ||
}, | ||
join: function(sep) { | ||
@@ -104,6 +108,10 @@ // TODO: Could this be faster by converting | ||
reverse: function(str){ | ||
return Array.prototype.reverse.apply(str.split('')).join(''); | ||
lines: function(str) { | ||
return str.split("\n"); | ||
}, | ||
// reverse: function(str){ | ||
// return Array.prototype.reverse.apply(str.split('')).join(''); | ||
// }, | ||
splice: function(str, i, howmany, substr){ | ||
@@ -115,7 +123,7 @@ var arr = str.split(''); | ||
isStartsWith: function(str, starts){ | ||
startsWith: function(str, starts){ | ||
return str.length >= starts.length && str.substring(0, starts.length) === starts; | ||
}, | ||
isEndsWith: function(str, ends){ | ||
endsWith: function(str, ends){ | ||
return str.length >= ends.length && str.substring(str.length - ends.length) === ends; | ||
@@ -141,2 +149,16 @@ }, | ||
camelize: function(str){ | ||
return _s.trim(str).replace(/(\-|_|\s)+(.)?/g, function(match, separator, chr) { | ||
return chr ? chr.toUpperCase() : ''; | ||
}); | ||
}, | ||
underscored: function(str){ | ||
return _s.trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/\-|\s+/g, '_').toLowerCase(); | ||
}, | ||
dasherize: function(str){ | ||
return _s.trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1-$2').replace(/^([A-Z]+)/, '-$1').replace(/\_|\s+/g, '-').toLowerCase(); | ||
}, | ||
trim: function(str, characters){ | ||
@@ -165,2 +187,7 @@ if (!characters && nativeTrim) { | ||
words: function(str, delimiter) { | ||
delimiter = delimiter || " "; | ||
return str.split(delimiter); | ||
}, | ||
/** | ||
@@ -222,23 +249,3 @@ * Credits for this function goes to | ||
_s.rstrip = _s.rtrim; | ||
_s.isIncludes = _s.isContains; | ||
var depricatedAlias = function (funName, oldName) { | ||
_s[oldName] = function () { | ||
if (typeof console != 'undefined' && typeof console.log != 'undefined') | ||
console.log( | ||
'Deprication warning: use "' + funName + '" instead "' + oldName + '". ' + | ||
'"' + oldName + '" will be removed after "undescore.string" 1.1' | ||
); | ||
return _s[funName].apply(_s, arguments); | ||
}; | ||
}; | ||
depricatedAlias('isStartsWith', 'startsWith'); | ||
depricatedAlias('isEndsWith', 'endsWith'); | ||
depricatedAlias('isContains', 'contains'); | ||
depricatedAlias('isBlank', 'blank'); | ||
depricatedAlias('isIncludes', 'includes'); | ||
// CommonJS module is defined | ||
@@ -245,0 +252,0 @@ if (typeof window === 'undefined' && typeof module !== 'undefined') { |
{ | ||
"name": "underscore.string", | ||
"version": "1.0.0", | ||
"version": "1.1.3", | ||
"description": "String manipulation extensions for Underscore.js javascript library.", | ||
@@ -16,2 +16,3 @@ "homepage": "https://github.com/edtsech/underscore.string", | ||
], | ||
"main": "./lib/underscore.string", | ||
"directories": { | ||
@@ -18,0 +19,0 @@ "lib": "./lib" |
@@ -27,8 +27,8 @@ (function() { | ||
JSLitmus.test('isStartsWith', function() { | ||
return _.isStartsWith("foobar", "foo"); | ||
JSLitmus.test('startsWith', function() { | ||
return _.startsWith("foobar", "foo"); | ||
}); | ||
JSLitmus.test('isEndsWith', function() { | ||
return _.isEndsWith("foobar", "xx"); | ||
JSLitmus.test('endsWith', function() { | ||
return _.endsWith("foobar", "xx"); | ||
}); | ||
@@ -71,4 +71,4 @@ | ||
JSLitmus.test('blank', function(){ | ||
return _('').blank(); | ||
JSLitmus.test('isBlank', function(){ | ||
return _('').isBlank(); | ||
}); | ||
@@ -75,0 +75,0 @@ |
@@ -26,8 +26,8 @@ $(document).ready(function() { | ||
test("Strings: reverse", function() { | ||
equals(_.reverse("foo"), "oof" ); | ||
equals(_.reverse("foobar"), "raboof" ); | ||
equals(_.reverse("foo bar"), "rab oof" ); | ||
equals(_.reverse("saippuakauppias"), "saippuakauppias" ); | ||
}); | ||
// test("Strings: reverse", function() { | ||
// equals(_.reverse("foo"), "oof" ); | ||
// equals(_.reverse("foobar"), "raboof" ); | ||
// equals(_.reverse("foo bar"), "rab oof" ); | ||
// equals(_.reverse("saippuakauppias"), "saippuakauppias" ); | ||
// }); | ||
@@ -95,7 +95,2 @@ test("Strings: trim", function() { | ||
test("Strings: isStartsWith", function() { | ||
ok(_("foobar").isStartsWith("foo"), 'foobar starts with foo'); | ||
ok(!_("oobar").isStartsWith("foo"), 'oobar does not start with foo'); | ||
}); | ||
test("Strings: startsWith", function() { | ||
@@ -106,9 +101,2 @@ ok(_("foobar").startsWith("foo"), 'foobar starts with foo'); | ||
test("Strings: isEndsWith", function() { | ||
ok(_("foobar").isEndsWith("bar"), 'foobar ends with bar'); | ||
ok(_.isEndsWith("foobar", "bar"), 'foobar ends with bar'); | ||
ok(_.isEndsWith("00018-0000062.Plone.sdh264.1a7264e6912a91aa4a81b64dc5517df7b8875994.mp4", "mp4"), 'endsWith .mp4'); | ||
ok(!_("fooba").isEndsWith("bar"), 'fooba does not end with bar'); | ||
}); | ||
test("Strings: endsWith", function() { | ||
@@ -121,12 +109,7 @@ ok(_("foobar").endsWith("bar"), 'foobar ends with bar'); | ||
test("Strings: isContains", function() { | ||
ok(_("foobar").isContains("bar"), 'foobar contains bar'); | ||
ok(!_("foobar").isContains("buzz"), 'foobar does not contain buzz'); | ||
test("Strings: includes", function() { | ||
ok(_("foobar").includes("bar"), 'foobar includes bar'); | ||
ok(!_("foobar").includes("buzz"), 'foobar does not includes buzz'); | ||
}); | ||
test("Strings: contains", function() { | ||
ok(_("foobar").contains("bar"), 'foobar contains bar'); | ||
ok(!_("foobar").contains("buzz"), 'foobar does not contain buzz'); | ||
}); | ||
test('String: chop', function(){ | ||
@@ -164,2 +147,24 @@ ok(_('whitespace').chop(2).length === 5, "output ['wh','it','es','pa','ce']"); | ||
test('String: camelize', function(){ | ||
equals(_('the_camelize_string_method').camelize(), 'theCamelizeStringMethod'); | ||
equals(_('-the-camelize-string-method').camelize(), 'TheCamelizeStringMethod'); | ||
equals(_('the camelize string method').camelize(), 'theCamelizeStringMethod'); | ||
equals(_(' the camelize string method').camelize(), 'theCamelizeStringMethod'); | ||
equals(_('the camelize string method').camelize(), 'theCamelizeStringMethod'); | ||
}); | ||
test('String: underscored', function(){ | ||
equals(_('the-underscored-string-method').underscored(), 'the_underscored_string_method'); | ||
equals(_('theUnderscoredStringMethod').underscored(), 'the_underscored_string_method'); | ||
equals(_('TheUnderscoredStringMethod').underscored(), 'the_underscored_string_method'); | ||
equals(_(' the underscored string method').underscored(), 'the_underscored_string_method'); | ||
}); | ||
test('String: dasherize', function(){ | ||
equals(_('the_dasherize_string_method').dasherize(), 'the-dasherize-string-method'); | ||
equals(_('TheDasherizeStringMethod').dasherize(), '-the-dasherize-string-method'); | ||
equals(_('the dasherize string method').dasherize(), 'the-dasherize-string-method'); | ||
equals(_('the dasherize string method ').dasherize(), 'the-dasherize-string-method'); | ||
}); | ||
test('String: truncate', function(){ | ||
@@ -177,9 +182,2 @@ equals(_('Hello world').truncate(6, 'read more'), 'Hello read more'); | ||
test('String: blank', function(){ | ||
ok(_('').blank()); | ||
ok(_(' ').blank()); | ||
ok(_('\n').blank()); | ||
ok(!_('a').blank()); | ||
}); | ||
test('String: escapeHTML', function(){ | ||
@@ -196,2 +194,17 @@ equals(_('<div>Blah blah blah</div>').escapeHTML(), '<div>Blah blah blah</div>'); | ||
}); | ||
test('String: words', function() { | ||
equals(_("I love you!").words().length, 3); | ||
equals(_("I_love_you!").words('_').length, 3); | ||
equals(_("I-love-you!").words(/-/).length, 3); | ||
}); | ||
test('String: chars', function() { | ||
equals(_("Hello").chars().length, 5); | ||
}); | ||
test('String: lines', function() { | ||
equals(_("Hello\nWorld").lines().length, 2); | ||
equals(_("Hello World").lines().length, 1); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
99269
21
1924
281
1