Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: epeli/underscore.string
...
head fork: epeli/underscore.string
Checking mergeability… Don't worry, you can still create the pull request.
  • 15 commits
  • 21 files changed
  • 0 commit comments
  • 4 contributors
Commits on Oct 10, 2012
@lfac-pt lfac-pt Implement count without String's split
split creates an array that is (in this case) immediately thrown away. 
This produces unnecessary garbage and its (arguably) faster (see indexOf 1.2): 
http://jsperf.com/count-string-occurrence-in-string/10

The downside is that substr can no longer be a RegExp, but this was not
intended (at least according with the docs).
e1defd1
@lfac-pt lfac-pt Fixes issue when substr is a number
Now we make sure that substr is a string.
4a82f83
Commits on Oct 16, 2012
@rwz rwz Updated test suite
Updated underscore 1.4.2
1d7d2a4
@rwz rwz Merge pull request #150 from lfac-pt/master
Implement count without split
f7caf43
@rwz rwz Small codestyle changes for count method bb9eefa
@rwz rwz Fixed test suite 4989f17
@rwz rwz Underscore.string tries all available integration methods in order
Fixed #148
9df4736
Commits on Oct 28, 2012
@alsotang alsotang a little fix 7480bf9
Commits on Nov 02, 2012
@rwz rwz Escape HTML uses ' instead of '
Closes #153
1f986aa
Commits on Nov 06, 2012
@wyuenho wyuenho allow the use of an empty string as a thousands separator in numberFo…
…rmat()
c15d314
Commits on Nov 10, 2012
@rwz rwz Merge pull request #152 from alsotang/master
just a little fix
be8a976
@rwz rwz Merge pull request #154 from wyuenho/master
allow the use of an empty string as a thousands separator in numberFormat()
3ca1faa
Commits on Nov 22, 2012
@rwz rwz Fixed toNumber with funny strings as an argument
Closes #157
146f72f
Commits on Dec 02, 2012
@rwz rwz More sane classify behavior
closes #158
68ad492
@rwz rwz v 2.3.1 577f0c7
View
21 README.markdown
@@ -132,19 +132,19 @@ _.swapCase('hELLO')
=> 'Hello'
```
-**includes** _.includes(string, substring)
-
-Tests if string contains a substring.
+**include** available only through _.str object, because Underscore has function with the same name.
```javascript
-_.includes("foobar", "ob")
+_.str.include("foobar", "ob")
=> true
```
-**include** available only through _.str object, because Underscore has function with the same name.
+(removed) **includes** _.includes(string, substring)
+
+Tests if string contains a substring.
```javascript
-_.str.include("foobar", "ob")
+_.includes("foobar", "ob")
=> true
```
@@ -545,7 +545,7 @@ The same as `toSentence`, but adjusts delimeters to use [Serial comma](http://en
```javascript
_.toSentenceSerial(['jQuery', 'Mootools'])
-=> 'Mootools and Prototype';
+=> 'jQuery and Mootools';
_.toSentenceSerial(['jQuery', 'Mootools', 'Prototype'])
=> 'jQuery, Mootools, and Prototype'
@@ -630,6 +630,13 @@ _ = _.string
## Changelog ##
+### 2.3.1 ###
+
+* Changed integration logic, now trying everything in order
+* Fixed classify method to chew some unexpected input
+* Fixed toNumber method failing to recognize '0.0' as a proper number
+
+
### 2.3.0 ###
* Added `numberformat` method
View
4 Rakefile
@@ -17,7 +17,7 @@ task :test do
result1 = system %{phantomjs ./test/run-qunit.js "test/test.html"}
puts "Running Underscore test suite."
- result2 = system %{phantomjs ./test/run-qunit.js "test/test_underscore/test.html"}
-
+ result2 = system %{phantomjs ./test/run-qunit.js "test/test_underscore/index.html"}
+
exit(result1 && result2 ? 0 : 1)
end
View
2  dist/underscore.string.min.js
@@ -1 +1 @@
-!function(e,t){"use strict";var n=t.prototype.trim,r=t.prototype.trimRight,i=t.prototype.trimLeft,s=function(e){return e*1||0},o=function(e,t){if(t<1)return"";var n="";while(t>0)t&1&&(n+=e),t>>=1,e+=e;return n},u=[].slice,a=function(e){return e==null?"\\s":e.source?e.source:"["+p.escapeRegExp(e)+"]"},f={lt:"<",gt:">",quot:'"',apos:"'",amp:"&"},l={};for(var c in f)l[f[c]]=c;var h=function(){function e(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()}var n=o,r=function(){return r.cache.hasOwnProperty(arguments[0])||(r.cache[arguments[0]]=r.parse(arguments[0])),r.format.call(null,r.cache[arguments[0]],arguments)};return r.format=function(r,i){var s=1,o=r.length,u="",a,f=[],l,c,p,d,v,m;for(l=0;l<o;l++){u=e(r[l]);if(u==="string")f.push(r[l]);else if(u==="array"){p=r[l];if(p[2]){a=i[s];for(c=0;c<p[2].length;c++){if(!a.hasOwnProperty(p[2][c]))throw new Error(h('[_.sprintf] property "%s" does not exist',p[2][c]));a=a[p[2][c]]}}else p[1]?a=i[p[1]]:a=i[s++];if(/[^s]/.test(p[8])&&e(a)!="number")throw new Error(h("[_.sprintf] expecting number but found %s",e(a)));switch(p[8]){case"b":a=a.toString(2);break;case"c":a=t.fromCharCode(a);break;case"d":a=parseInt(a,10);break;case"e":a=p[7]?a.toExponential(p[7]):a.toExponential();break;case"f":a=p[7]?parseFloat(a).toFixed(p[7]):parseFloat(a);break;case"o":a=a.toString(8);break;case"s":a=(a=t(a))&&p[7]?a.substring(0,p[7]):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(p[8])&&p[3]&&a>=0?"+"+a:a,v=p[4]?p[4]=="0"?"0":p[4].charAt(1):" ",m=p[6]-t(a).length,d=p[6]?n(v,m):"",f.push(p[5]?a+d:d+a)}}return f.join("")},r.cache={},r.parse=function(e){var t=e,n=[],r=[],i=0;while(t){if((n=/^[^\x25]+/.exec(t))!==null)r.push(n[0]);else if((n=/^\x25{2}/.exec(t))!==null)r.push("%");else{if((n=/^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(t))===null)throw new Error("[_.sprintf] huh?");if(n[2]){i|=1;var s=[],o=n[2],u=[];if((u=/^([a-z_][a-z_\d]*)/i.exec(o))===null)throw new Error("[_.sprintf] huh?");s.push(u[1]);while((o=o.substring(u[0].length))!=="")if((u=/^\.([a-z_][a-z_\d]*)/i.exec(o))!==null)s.push(u[1]);else{if((u=/^\[(\d+)\]/.exec(o))===null)throw new Error("[_.sprintf] huh?");s.push(u[1])}n[2]=s}else i|=2;if(i===3)throw new Error("[_.sprintf] mixing positional and named placeholders is not (yet) supported");r.push(n)}t=t.substring(n[0].length)}return r},r}(),p={VERSION:"2.3.0",isBlank:function(e){return e==null&&(e=""),/^\s*$/.test(e)},stripTags:function(e){return e==null?"":t(e).replace(/<\/?[^>]+>/g,"")},capitalize:function(e){return e=e==null?"":t(e),e.charAt(0).toUpperCase()+e.slice(1)},chop:function(e,n){return e==null?[]:(e=t(e),n=~~n,n>0?e.match(new RegExp(".{1,"+n+"}","g")):[e])},clean:function(e){return p.strip(e).replace(/\s+/g," ")},count:function(e,n){return e==null||n==null?0:t(e).split(n).length-1},chars:function(e){return e==null?[]:t(e).split("")},swapCase:function(e){return e==null?"":t(e).replace(/\S/g,function(e){return e===e.toUpperCase()?e.toLowerCase():e.toUpperCase()})},escapeHTML:function(e){return e==null?"":t(e).replace(/[&<>"']/g,function(e){return"&"+l[e]+";"})},unescapeHTML:function(e){return e==null?"":t(e).replace(/\&([^;]+);/g,function(e,n){var r;return n in f?f[n]:(r=n.match(/^#x([\da-fA-F]+)$/))?t.fromCharCode(parseInt(r[1],16)):(r=n.match(/^#(\d+)$/))?t.fromCharCode(~~r[1]):e})},escapeRegExp:function(e){return e==null?"":t(e).replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1")},splice:function(e,t,n,r){var i=p.chars(e);return i.splice(~~t,~~n,r),i.join("")},insert:function(e,t,n){return p.splice(e,t,0,n)},include:function(e,n){return n===""?!0:e==null?!1:t(e).indexOf(n)!==-1},join:function(){var e=u.call(arguments),t=e.shift();return t==null&&(t=""),e.join(t)},lines:function(e){return e==null?[]:t(e).split("\n")},reverse:function(e){return p.chars(e).reverse().join("")},startsWith:function(e,n){return n===""?!0:e==null||n==null?!1:(e=t(e),n=t(n),e.length>=n.length&&e.slice(0,n.length)===n)},endsWith:function(e,n){return n===""?!0:e==null||n==null?!1:(e=t(e),n=t(n),e.length>=n.length&&e.slice(e.length-n.length)===n)},succ:function(e){return e==null?"":(e=t(e),e.slice(0,-1)+t.fromCharCode(e.charCodeAt(e.length-1)+1))},titleize:function(e){return e==null?"":t(e).replace(/(?:^|\s)\S/g,function(e){return e.toUpperCase()})},camelize:function(e){return p.trim(e).replace(/[-_\s]+(.)?/g,function(e,t){return t.toUpperCase()})},underscored:function(e){return p.trim(e).replace(/([a-z\d])([A-Z]+)/g,"$1_$2").replace(/[-\s]+/g,"_").toLowerCase()},dasherize:function(e){return p.trim(e).replace(/([A-Z])/g,"-$1").replace(/[-_\s]+/g,"-").toLowerCase()},classify:function(e){return p.titleize(t(e).replace(/_/g," ")).replace(/\s/g,"")},humanize:function(e){return p.capitalize(p.underscored(e).replace(/_id$/,"").replace(/_/g," "))},trim:function(e,r){return e==null?"":!r&&n?n.call(e):(r=a(r),t(e).replace(new RegExp("^"+r+"+|"+r+"+$","g"),""))},ltrim:function(e,n){return e==null?"":!n&&i?i.call(e):(n=a(n),t(e).replace(new RegExp("^"+n+"+"),""))},rtrim:function(e,n){return e==null?"":!n&&r?r.call(e):(n=a(n),t(e).replace(new RegExp(n+"+$"),""))},truncate:function(e,n,r){return e==null?"":(e=t(e),r=r||"...",n=~~n,e.length>n?e.slice(0,n)+r:e)},prune:function(e,n,r){if(e==null)return"";e=t(e),n=~~n,r=r!=null?t(r):"...";if(e.length<=n)return e;var i=function(e){return e.toUpperCase()!==e.toLowerCase()?"A":" "},s=e.slice(0,n+1).replace(/.(?=\W*\w*$)/g,i);return s.slice(s.length-2).match(/\w\w/)?s=s.replace(/\s*\S+$/,""):s=p.rtrim(s.slice(0,s.length-1)),(s+r).length>e.length?e:e.slice(0,s.length)+r},words:function(e,t){return p.isBlank(e)?[]:p.trim(e,t).split(t||/\s+/)},pad:function(e,n,r,i){e=e==null?"":t(e),n=~~n;var s=0;r?r.length>1&&(r=r.charAt(0)):r=" ";switch(i){case"right":return s=n-e.length,e+o(r,s);case"both":return s=n-e.length,o(r,Math.ceil(s/2))+e+o(r,Math.floor(s/2));default:return s=n-e.length,o(r,s)+e}},lpad:function(e,t,n){return p.pad(e,t,n)},rpad:function(e,t,n){return p.pad(e,t,n,"right")},lrpad:function(e,t,n){return p.pad(e,t,n,"both")},sprintf:h,vsprintf:function(e,t){return t.unshift(e),h.apply(null,t)},toNumber:function(e,n){if(e==null||e=="")return 0;e=t(e);var r=s(s(e).toFixed(~~n));return r===0&&!e.match(/^0+$/)?Number.NaN:r},numberFormat:function(e,t,n,r){if(isNaN(e)||e==null)return"";e=e.toFixed(~~t),r=r||",";var i=e.split("."),s=i[0],o=i[1]?(n||".")+i[1]:"";return s.replace(/(\d)(?=(?:\d{3})+$)/g,"$1"+r)+o},strRight:function(e,n){if(e==null)return"";e=t(e),n=n!=null?t(n):n;var r=n?e.indexOf(n):-1;return~r?e.slice(r+n.length,e.length):e},strRightBack:function(e,n){if(e==null)return"";e=t(e),n=n!=null?t(n):n;var r=n?e.lastIndexOf(n):-1;return~r?e.slice(r+n.length,e.length):e},strLeft:function(e,n){if(e==null)return"";e=t(e),n=n!=null?t(n):n;var r=n?e.indexOf(n):-1;return~r?e.slice(0,r):e},strLeftBack:function(e,t){if(e==null)return"";e+="",t=t!=null?""+t:t;var n=e.lastIndexOf(t);return~n?e.slice(0,n):e},toSentence:function(e,t,n,r){t=t||", ",n=n||" and ";var i=e.slice(),s=i.pop();return e.length>2&&r&&(n=p.rtrim(t)+n),i.length?i.join(t)+n+s:s},toSentenceSerial:function(){var e=u.call(arguments);return e[3]=!0,p.toSentence.apply(p,e)},slugify:function(e){if(e==null)return"";var n="ąàáäâãåæćęèéëêìíïîłńòóöôõøùúüûñçżź",r="aaaaaaaaceeeeeiiiilnoooooouuuunczz",i=new RegExp(a(n),"g");return e=t(e).toLowerCase().replace(i,function(e){var t=n.indexOf(e);return r.charAt(t)||"-"}),p.dasherize(e.replace(/[^\w\s-]/g,""))},surround:function(e,t){return[t,e,t].join("")},quote:function(e){return p.surround(e,'"')},exports:function(){var e={};for(var t in this){if(!this.hasOwnProperty(t)||t.match(/^(?:include|contains|reverse)$/))continue;e[t]=this[t]}return e},repeat:function(e,n,r){if(e==null)return"";n=~~n;if(r==null)return o(t(e),n);for(var i=[];n>0;i[--n]=e);return i.join(r)},levenshtein:function(e,n){if(e==null&&n==null)return 0;if(e==null)return t(n).length;if(n==null)return t(e).length;e=t(e),n=t(n);var r=[],i,s;for(var o=0;o<=n.length;o++)for(var u=0;u<=e.length;u++)o&&u?e.charAt(u-1)===n.charAt(o-1)?s=i:s=Math.min(r[u],r[u-1],i)+1:s=o+u,i=r[u],r[u]=s;return r.pop()}};p.strip=p.trim,p.lstrip=p.ltrim,p.rstrip=p.rtrim,p.center=p.lrpad,p.rjust=p.lpad,p.ljust=p.rpad,p.contains=p.include,p.q=p.quote,typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(module.exports=p),exports._s=p):typeof define=="function"&&define.amd?define("underscore.string",[],function(){return p}):(e._=e._||{},e._.string=e._.str=p)}(this,String);
+!function(e,t){"use strict";var n=t.prototype.trim,r=t.prototype.trimRight,i=t.prototype.trimLeft,s=function(e){return e*1||0},o=function(e,t){if(t<1)return"";var n="";while(t>0)t&1&&(n+=e),t>>=1,e+=e;return n},u=[].slice,a=function(e){return e==null?"\\s":e.source?e.source:"["+p.escapeRegExp(e)+"]"},f={lt:"<",gt:">",quot:'"',amp:"&",apos:"'"},l={};for(var c in f)l[f[c]]=c;l["'"]="#39";var h=function(){function e(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()}var n=o,r=function(){return r.cache.hasOwnProperty(arguments[0])||(r.cache[arguments[0]]=r.parse(arguments[0])),r.format.call(null,r.cache[arguments[0]],arguments)};return r.format=function(r,i){var s=1,o=r.length,u="",a,f=[],l,c,p,d,v,m;for(l=0;l<o;l++){u=e(r[l]);if(u==="string")f.push(r[l]);else if(u==="array"){p=r[l];if(p[2]){a=i[s];for(c=0;c<p[2].length;c++){if(!a.hasOwnProperty(p[2][c]))throw new Error(h('[_.sprintf] property "%s" does not exist',p[2][c]));a=a[p[2][c]]}}else p[1]?a=i[p[1]]:a=i[s++];if(/[^s]/.test(p[8])&&e(a)!="number")throw new Error(h("[_.sprintf] expecting number but found %s",e(a)));switch(p[8]){case"b":a=a.toString(2);break;case"c":a=t.fromCharCode(a);break;case"d":a=parseInt(a,10);break;case"e":a=p[7]?a.toExponential(p[7]):a.toExponential();break;case"f":a=p[7]?parseFloat(a).toFixed(p[7]):parseFloat(a);break;case"o":a=a.toString(8);break;case"s":a=(a=t(a))&&p[7]?a.substring(0,p[7]):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(p[8])&&p[3]&&a>=0?"+"+a:a,v=p[4]?p[4]=="0"?"0":p[4].charAt(1):" ",m=p[6]-t(a).length,d=p[6]?n(v,m):"",f.push(p[5]?a+d:d+a)}}return f.join("")},r.cache={},r.parse=function(e){var t=e,n=[],r=[],i=0;while(t){if((n=/^[^\x25]+/.exec(t))!==null)r.push(n[0]);else if((n=/^\x25{2}/.exec(t))!==null)r.push("%");else{if((n=/^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(t))===null)throw new Error("[_.sprintf] huh?");if(n[2]){i|=1;var s=[],o=n[2],u=[];if((u=/^([a-z_][a-z_\d]*)/i.exec(o))===null)throw new Error("[_.sprintf] huh?");s.push(u[1]);while((o=o.substring(u[0].length))!=="")if((u=/^\.([a-z_][a-z_\d]*)/i.exec(o))!==null)s.push(u[1]);else{if((u=/^\[(\d+)\]/.exec(o))===null)throw new Error("[_.sprintf] huh?");s.push(u[1])}n[2]=s}else i|=2;if(i===3)throw new Error("[_.sprintf] mixing positional and named placeholders is not (yet) supported");r.push(n)}t=t.substring(n[0].length)}return r},r}(),p={VERSION:"2.3.0",isBlank:function(e){return e==null&&(e=""),/^\s*$/.test(e)},stripTags:function(e){return e==null?"":t(e).replace(/<\/?[^>]+>/g,"")},capitalize:function(e){return e=e==null?"":t(e),e.charAt(0).toUpperCase()+e.slice(1)},chop:function(e,n){return e==null?[]:(e=t(e),n=~~n,n>0?e.match(new RegExp(".{1,"+n+"}","g")):[e])},clean:function(e){return p.strip(e).replace(/\s+/g," ")},count:function(e,n){if(e==null||n==null)return 0;e=t(e),n=t(n);var r=0,i=0,s=n.length;for(;;){i=e.indexOf(n,i);if(i===-1)break;r++,i+=s}return r},chars:function(e){return e==null?[]:t(e).split("")},swapCase:function(e){return e==null?"":t(e).replace(/\S/g,function(e){return e===e.toUpperCase()?e.toLowerCase():e.toUpperCase()})},escapeHTML:function(e){return e==null?"":t(e).replace(/[&<>"']/g,function(e){return"&"+l[e]+";"})},unescapeHTML:function(e){return e==null?"":t(e).replace(/\&([^;]+);/g,function(e,n){var r;return n in f?f[n]:(r=n.match(/^#x([\da-fA-F]+)$/))?t.fromCharCode(parseInt(r[1],16)):(r=n.match(/^#(\d+)$/))?t.fromCharCode(~~r[1]):e})},escapeRegExp:function(e){return e==null?"":t(e).replace(/([.*+?^=!:${}()|[\]\/\\])/g,"\\$1")},splice:function(e,t,n,r){var i=p.chars(e);return i.splice(~~t,~~n,r),i.join("")},insert:function(e,t,n){return p.splice(e,t,0,n)},include:function(e,n){return n===""?!0:e==null?!1:t(e).indexOf(n)!==-1},join:function(){var e=u.call(arguments),t=e.shift();return t==null&&(t=""),e.join(t)},lines:function(e){return e==null?[]:t(e).split("\n")},reverse:function(e){return p.chars(e).reverse().join("")},startsWith:function(e,n){return n===""?!0:e==null||n==null?!1:(e=t(e),n=t(n),e.length>=n.length&&e.slice(0,n.length)===n)},endsWith:function(e,n){return n===""?!0:e==null||n==null?!1:(e=t(e),n=t(n),e.length>=n.length&&e.slice(e.length-n.length)===n)},succ:function(e){return e==null?"":(e=t(e),e.slice(0,-1)+t.fromCharCode(e.charCodeAt(e.length-1)+1))},titleize:function(e){return e==null?"":t(e).replace(/(?:^|\s)\S/g,function(e){return e.toUpperCase()})},camelize:function(e){return p.trim(e).replace(/[-_\s]+(.)?/g,function(e,t){return t.toUpperCase()})},underscored:function(e){return p.trim(e).replace(/([a-z\d])([A-Z]+)/g,"$1_$2").replace(/[-\s]+/g,"_").toLowerCase()},dasherize:function(e){return p.trim(e).replace(/([A-Z])/g,"-$1").replace(/[-_\s]+/g,"-").toLowerCase()},classify:function(e){return p.titleize(t(e).replace(/_/g," ")).replace(/\s/g,"")},humanize:function(e){return p.capitalize(p.underscored(e).replace(/_id$/,"").replace(/_/g," "))},trim:function(e,r){return e==null?"":!r&&n?n.call(e):(r=a(r),t(e).replace(new RegExp("^"+r+"+|"+r+"+$","g"),""))},ltrim:function(e,n){return e==null?"":!n&&i?i.call(e):(n=a(n),t(e).replace(new RegExp("^"+n+"+"),""))},rtrim:function(e,n){return e==null?"":!n&&r?r.call(e):(n=a(n),t(e).replace(new RegExp(n+"+$"),""))},truncate:function(e,n,r){return e==null?"":(e=t(e),r=r||"...",n=~~n,e.length>n?e.slice(0,n)+r:e)},prune:function(e,n,r){if(e==null)return"";e=t(e),n=~~n,r=r!=null?t(r):"...";if(e.length<=n)return e;var i=function(e){return e.toUpperCase()!==e.toLowerCase()?"A":" "},s=e.slice(0,n+1).replace(/.(?=\W*\w*$)/g,i);return s.slice(s.length-2).match(/\w\w/)?s=s.replace(/\s*\S+$/,""):s=p.rtrim(s.slice(0,s.length-1)),(s+r).length>e.length?e:e.slice(0,s.length)+r},words:function(e,t){return p.isBlank(e)?[]:p.trim(e,t).split(t||/\s+/)},pad:function(e,n,r,i){e=e==null?"":t(e),n=~~n;var s=0;r?r.length>1&&(r=r.charAt(0)):r=" ";switch(i){case"right":return s=n-e.length,e+o(r,s);case"both":return s=n-e.length,o(r,Math.ceil(s/2))+e+o(r,Math.floor(s/2));default:return s=n-e.length,o(r,s)+e}},lpad:function(e,t,n){return p.pad(e,t,n)},rpad:function(e,t,n){return p.pad(e,t,n,"right")},lrpad:function(e,t,n){return p.pad(e,t,n,"both")},sprintf:h,vsprintf:function(e,t){return t.unshift(e),h.apply(null,t)},toNumber:function(e,n){if(e==null||e=="")return 0;e=t(e);var r=s(s(e).toFixed(~~n));return r===0&&!e.match(/^0+$/)?Number.NaN:r},numberFormat:function(e,t,n,r){if(isNaN(e)||e==null)return"";e=e.toFixed(~~t),r=typeof r=="string"?r:",";var i=e.split("."),s=i[0],o=i[1]?(n||".")+i[1]:"";return s.replace(/(\d)(?=(?:\d{3})+$)/g,"$1"+r)+o},strRight:function(e,n){if(e==null)return"";e=t(e),n=n!=null?t(n):n;var r=n?e.indexOf(n):-1;return~r?e.slice(r+n.length,e.length):e},strRightBack:function(e,n){if(e==null)return"";e=t(e),n=n!=null?t(n):n;var r=n?e.lastIndexOf(n):-1;return~r?e.slice(r+n.length,e.length):e},strLeft:function(e,n){if(e==null)return"";e=t(e),n=n!=null?t(n):n;var r=n?e.indexOf(n):-1;return~r?e.slice(0,r):e},strLeftBack:function(e,t){if(e==null)return"";e+="",t=t!=null?""+t:t;var n=e.lastIndexOf(t);return~n?e.slice(0,n):e},toSentence:function(e,t,n,r){t=t||", ",n=n||" and ";var i=e.slice(),s=i.pop();return e.length>2&&r&&(n=p.rtrim(t)+n),i.length?i.join(t)+n+s:s},toSentenceSerial:function(){var e=u.call(arguments);return e[3]=!0,p.toSentence.apply(p,e)},slugify:function(e){if(e==null)return"";var n="ąàáäâãåæćęèéëêìíïîłńòóöôõøùúüûñçżź",r="aaaaaaaaceeeeeiiiilnoooooouuuunczz",i=new RegExp(a(n),"g");return e=t(e).toLowerCase().replace(i,function(e){var t=n.indexOf(e);return r.charAt(t)||"-"}),p.dasherize(e.replace(/[^\w\s-]/g,""))},surround:function(e,t){return[t,e,t].join("")},quote:function(e){return p.surround(e,'"')},exports:function(){var e={};for(var t in this){if(!this.hasOwnProperty(t)||t.match(/^(?:include|contains|reverse)$/))continue;e[t]=this[t]}return e},repeat:function(e,n,r){if(e==null)return"";n=~~n;if(r==null)return o(t(e),n);for(var i=[];n>0;i[--n]=e);return i.join(r)},levenshtein:function(e,n){if(e==null&&n==null)return 0;if(e==null)return t(n).length;if(n==null)return t(e).length;e=t(e),n=t(n);var r=[],i,s;for(var o=0;o<=n.length;o++)for(var u=0;u<=e.length;u++)o&&u?e.charAt(u-1)===n.charAt(o-1)?s=i:s=Math.min(r[u],r[u-1],i)+1:s=o+u,i=r[u],r[u]=s;return r.pop()}};p.strip=p.trim,p.lstrip=p.ltrim,p.rstrip=p.rtrim,p.center=p.lrpad,p.rjust=p.lpad,p.ljust=p.rpad,p.contains=p.include,p.q=p.quote,typeof exports!="undefined"&&(typeof module!="undefined"&&module.exports&&(module.exports=p),exports._s=p),typeof define=="function"&&define.amd&&define("underscore.string",[],function(){return p}),e._=e._||{},e._.string=e._.str=p}(this,String);
View
68 lib/underscore.string.js
@@ -3,7 +3,7 @@
// Underscore.string is freely distributable under the terms of the MIT license.
// Documentation: https://github.com/epeli/underscore.string
// Some code is borrowed from MooTools and Alexandru Marasteanu.
-// Version '2.3.0'
+// Version '2.3.1'
!function(root, String){
'use strict';
@@ -41,12 +41,13 @@
lt: '<',
gt: '>',
quot: '"',
- apos: "'",
- amp: '&'
+ amp: '&',
+ apos: "'"
};
var reversedEscapeChars = {};
- for(var key in escapeChars){ reversedEscapeChars[escapeChars[key]] = key; }
+ for(var key in escapeChars) reversedEscapeChars[escapeChars[key]] = key;
+ reversedEscapeChars["'"] = '#39';
// sprintf() for JavaScript 0.7-beta1
// http://www.diveintojavascript.com/projects/javascript-sprintf
@@ -176,7 +177,7 @@
var _s = {
- VERSION: '2.3.0',
+ VERSION: '2.3.1',
isBlank: function(str){
if (str == null) str = '';
@@ -206,7 +207,22 @@
count: function(str, substr){
if (str == null || substr == null) return 0;
- return String(str).split(substr).length - 1;
+
+ str = String(str);
+ substr = String(substr);
+
+ var count = 0,
+ pos = 0,
+ length = substr.length;
+
+ while (true) {
+ pos = str.indexOf(substr, pos);
+ if (pos === -1) break;
+ count++;
+ pos += length;
+ }
+
+ return count;
},
chars: function(str) {
@@ -320,7 +336,7 @@
},
classify: function(str){
- return _s.titleize(String(str).replace(/_/g, ' ')).replace(/\s/g, '');
+ return _s.titleize(String(str).replace(/[\W_]/g, ' ')).replace(/\s/g, '');
},
humanize: function(str){
@@ -429,17 +445,17 @@
},
toNumber: function(str, decimals) {
- if (str == null || str == '') return 0;
- str = String(str);
- var num = parseNumber(parseNumber(str).toFixed(~~decimals));
- return num === 0 && !str.match(/^0+$/) ? Number.NaN : num;
+ if (!str) return 0;
+ str = _s.trim(str);
+ if (!str.match(/^-?\d+(?:\.\d+)?$/)) return NaN;
+ return parseNumber(parseNumber(str).toFixed(~~decimals));
},
numberFormat : function(number, dec, dsep, tsep) {
if (isNaN(number) || number == null) return '';
number = number.toFixed(~~dec);
- tsep = tsep || ',';
+ tsep = typeof tsep == 'string' ? tsep : ',';
var parts = number.split('.'), fnums = parts[0],
decimals = parts[1] ? (dsep || '.') + parts[1] : '';
@@ -576,25 +592,23 @@
_s.contains = _s.include;
_s.q = _s.quote;
+ // Exporting
+
// CommonJS module is defined
if (typeof exports !== 'undefined') {
- if (typeof module !== 'undefined' && module.exports) {
- // Export module
+ if (typeof module !== 'undefined' && module.exports)
module.exports = _s;
- }
- exports._s = _s;
- } else if (typeof define === 'function' && define.amd) {
- // Register as a named module with AMD.
- define('underscore.string', [], function() {
- return _s;
- });
-
- } else {
- // Integrate with Underscore.js if defined
- // or create our own underscore object.
- root._ = root._ || {};
- root._.string = root._.str = _s;
+ exports._s = _s;
}
+ // Register as a named module with AMD.
+ if (typeof define === 'function' && define.amd)
+ define('underscore.string', [], function(){ return _s; });
+
+
+ // Integrate with Underscore.js if defined
+ // or create our own underscore object.
+ root._ = root._ || {};
+ root._.string = root._.str = _s;
}(this, String);
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "underscore.string",
- "version": "2.3.0",
+ "version": "2.3.1",
"description": "String manipulation extensions for Underscore.js javascript library.",
"homepage": "http://epeli.github.com/underscore.string/",
"contributors": [
View
806 test/strings.js
@@ -6,145 +6,145 @@ $(document).ready(function() {
module('String extensions');
test('Strings: trim', function() {
- equals(_.trim(123), '123', 'Non string');
- equals(_(' foo').trim(), 'foo');
- equals(_('foo ').trim(), 'foo');
- equals(_(' foo ').trim(), 'foo');
- equals(_(' foo ').trim(), 'foo');
- equals(_(' foo ').trim(' '), 'foo', 'Manually set whitespace');
- equals(_('\t foo \t ').trim(/\s/), 'foo', 'Manually set RegExp /\\s+/');
+ equal(_.trim(123), '123', 'Non string');
+ equal(_(' foo').trim(), 'foo');
+ equal(_('foo ').trim(), 'foo');
+ equal(_(' foo ').trim(), 'foo');
+ equal(_(' foo ').trim(), 'foo');
+ equal(_(' foo ').trim(' '), 'foo', 'Manually set whitespace');
+ equal(_('\t foo \t ').trim(/\s/), 'foo', 'Manually set RegExp /\\s+/');
- equals(_('ffoo').trim('f'), 'oo');
- equals(_('ooff').trim('f'), 'oo');
- equals(_('ffooff').trim('f'), 'oo');
+ equal(_('ffoo').trim('f'), 'oo');
+ equal(_('ooff').trim('f'), 'oo');
+ equal(_('ffooff').trim('f'), 'oo');
- equals(_('_-foobar-_').trim('_-'), 'foobar');
+ equal(_('_-foobar-_').trim('_-'), 'foobar');
- equals(_('http://foo/').trim('/'), 'http://foo');
- equals(_('c:\\').trim('\\'), 'c:');
+ equal(_('http://foo/').trim('/'), 'http://foo');
+ equal(_('c:\\').trim('\\'), 'c:');
- equals(_(123).trim(), '123');
- equals(_(123).trim(3), '12');
- equals(_('').trim(), '', 'Trim empty string should return empty string');
- equals(_(null).trim(), '', 'Trim null should return empty string');
- equals(_(undefined).trim(), '', 'Trim undefined should return empty string');
+ equal(_(123).trim(), '123');
+ equal(_(123).trim(3), '12');
+ equal(_('').trim(), '', 'Trim empty string should return empty string');
+ equal(_(null).trim(), '', 'Trim null should return empty string');
+ equal(_(undefined).trim(), '', 'Trim undefined should return empty string');
});
test('String: levenshtein', function() {
- equals(_.levenshtein('Godfather', 'Godfather'), 0);
- equals(_.levenshtein('Godfather', 'Godfathe'), 1);
- equals(_.levenshtein('Godfather', 'odfather'), 1);
- equals(_.levenshtein('Godfather', 'Gdfthr'), 3);
- equals(_.levenshtein('seven', 'eight'), 5);
- equals(_.levenshtein('123', 123), 0);
- equals(_.levenshtein(321, '321'), 0);
- equals(_.levenshtein('lol', null), 3);
- equals(_.levenshtein('lol'), 3);
- equals(_.levenshtein(null, 'lol'), 3);
- equals(_.levenshtein(undefined, 'lol'), 3);
- equals(_.levenshtein(), 0);
+ equal(_.levenshtein('Godfather', 'Godfather'), 0);
+ equal(_.levenshtein('Godfather', 'Godfathe'), 1);
+ equal(_.levenshtein('Godfather', 'odfather'), 1);
+ equal(_.levenshtein('Godfather', 'Gdfthr'), 3);
+ equal(_.levenshtein('seven', 'eight'), 5);
+ equal(_.levenshtein('123', 123), 0);
+ equal(_.levenshtein(321, '321'), 0);
+ equal(_.levenshtein('lol', null), 3);
+ equal(_.levenshtein('lol'), 3);
+ equal(_.levenshtein(null, 'lol'), 3);
+ equal(_.levenshtein(undefined, 'lol'), 3);
+ equal(_.levenshtein(), 0);
});
test('Strings: ltrim', function() {
- equals(_(' foo').ltrim(), 'foo');
- equals(_(' foo').ltrim(), 'foo');
- equals(_('foo ').ltrim(), 'foo ');
- equals(_(' foo ').ltrim(), 'foo ');
- equals(_('').ltrim(), '', 'ltrim empty string should return empty string');
- equals(_(null).ltrim(), '', 'ltrim null should return empty string');
- equals(_(undefined).ltrim(), '', 'ltrim undefined should return empty string');
+ equal(_(' foo').ltrim(), 'foo');
+ equal(_(' foo').ltrim(), 'foo');
+ equal(_('foo ').ltrim(), 'foo ');
+ equal(_(' foo ').ltrim(), 'foo ');
+ equal(_('').ltrim(), '', 'ltrim empty string should return empty string');
+ equal(_(null).ltrim(), '', 'ltrim null should return empty string');
+ equal(_(undefined).ltrim(), '', 'ltrim undefined should return empty string');
- equals(_('ffoo').ltrim('f'), 'oo');
- equals(_('ooff').ltrim('f'), 'ooff');
- equals(_('ffooff').ltrim('f'), 'ooff');
+ equal(_('ffoo').ltrim('f'), 'oo');
+ equal(_('ooff').ltrim('f'), 'ooff');
+ equal(_('ffooff').ltrim('f'), 'ooff');
- equals(_('_-foobar-_').ltrim('_-'), 'foobar-_');
+ equal(_('_-foobar-_').ltrim('_-'), 'foobar-_');
- equals(_(123).ltrim(1), '23');
+ equal(_(123).ltrim(1), '23');
});
test('Strings: rtrim', function() {
- equals(_('http://foo/').rtrim('/'), 'http://foo', 'clean trailing slash');
- equals(_(' foo').rtrim(), ' foo');
- equals(_('foo ').rtrim(), 'foo');
- equals(_('foo ').rtrim(), 'foo');
- equals(_('foo bar ').rtrim(), 'foo bar');
- equals(_(' foo ').rtrim(), ' foo');
+ equal(_('http://foo/').rtrim('/'), 'http://foo', 'clean trailing slash');
+ equal(_(' foo').rtrim(), ' foo');
+ equal(_('foo ').rtrim(), 'foo');
+ equal(_('foo ').rtrim(), 'foo');
+ equal(_('foo bar ').rtrim(), 'foo bar');
+ equal(_(' foo ').rtrim(), ' foo');
- equals(_('ffoo').rtrim('f'), 'ffoo');
- equals(_('ooff').rtrim('f'), 'oo');
- equals(_('ffooff').rtrim('f'), 'ffoo');
+ equal(_('ffoo').rtrim('f'), 'ffoo');
+ equal(_('ooff').rtrim('f'), 'oo');
+ equal(_('ffooff').rtrim('f'), 'ffoo');
- equals(_('_-foobar-_').rtrim('_-'), '_-foobar');
+ equal(_('_-foobar-_').rtrim('_-'), '_-foobar');
- equals(_(123).rtrim(3), '12');
- equals(_('').rtrim(), '', 'rtrim empty string should return empty string');
- equals(_(null).rtrim(), '', 'rtrim null should return empty string');
+ equal(_(123).rtrim(3), '12');
+ equal(_('').rtrim(), '', 'rtrim empty string should return empty string');
+ equal(_(null).rtrim(), '', 'rtrim null should return empty string');
});
test('Strings: capitalize', function() {
- equals(_('fabio').capitalize(), 'Fabio', 'First letter is upper case');
- equals(_.capitalize('fabio'), 'Fabio', 'First letter is upper case');
- equals(_.capitalize('FOO'), 'FOO', 'Other letters unchanged');
- equals(_(123).capitalize(), '123', 'Non string');
- equals(_.capitalize(''), '', 'Capitalizing empty string returns empty string');
- equals(_.capitalize(null), '', 'Capitalizing null returns empty string');
- equals(_.capitalize(undefined), '', 'Capitalizing undefined returns empty string');
+ equal(_('fabio').capitalize(), 'Fabio', 'First letter is upper case');
+ equal(_.capitalize('fabio'), 'Fabio', 'First letter is upper case');
+ equal(_.capitalize('FOO'), 'FOO', 'Other letters unchanged');
+ equal(_(123).capitalize(), '123', 'Non string');
+ equal(_.capitalize(''), '', 'Capitalizing empty string returns empty string');
+ equal(_.capitalize(null), '', 'Capitalizing null returns empty string');
+ equal(_.capitalize(undefined), '', 'Capitalizing undefined returns empty string');
});
test('Strings: join', function() {
- equals(_.join('', 'foo', 'bar'), 'foobar', 'basic join');
- equals(_.join('', 1, 'foo', 2), '1foo2', 'join numbers and strings');
- equals(_.join(' ','foo', 'bar'), 'foo bar', 'join with spaces');
- equals(_.join('1', '2', '2'), '212', 'join number strings');
- equals(_.join(1, 2, 2), '212', 'join numbers');
- equals(_.join('','foo', null), 'foo', 'join null with string returns string');
- equals(_.join(null,'foo', 'bar'), 'foobar', 'join strings with null returns string');
- equals(_(' ').join('foo', 'bar'), 'foo bar', 'join object oriented');
+ equal(_.join('', 'foo', 'bar'), 'foobar', 'basic join');
+ equal(_.join('', 1, 'foo', 2), '1foo2', 'join numbers and strings');
+ equal(_.join(' ','foo', 'bar'), 'foo bar', 'join with spaces');
+ equal(_.join('1', '2', '2'), '212', 'join number strings');
+ equal(_.join(1, 2, 2), '212', 'join numbers');
+ equal(_.join('','foo', null), 'foo', 'join null with string returns string');
+ equal(_.join(null,'foo', 'bar'), 'foobar', 'join strings with null returns string');
+ equal(_(' ').join('foo', 'bar'), 'foo bar', 'join object oriented');
});
test('Strings: reverse', function() {
- equals(_.str.reverse('foo'), 'oof' );
- equals(_.str.reverse('foobar'), 'raboof' );
- equals(_.str.reverse('foo bar'), 'rab oof' );
- equals(_.str.reverse('saippuakauppias'), 'saippuakauppias' );
- equals(_.str.reverse(123), '321', 'Non string');
- equals(_.str.reverse(123.45), '54.321', 'Non string');
- equals(_.str.reverse(''), '', 'reversing empty string returns empty string' );
- equals(_.str.reverse(null), '', 'reversing null returns empty string' );
- equals(_.str.reverse(undefined), '', 'reversing undefined returns empty string' );
+ equal(_.str.reverse('foo'), 'oof' );
+ equal(_.str.reverse('foobar'), 'raboof' );
+ equal(_.str.reverse('foo bar'), 'rab oof' );
+ equal(_.str.reverse('saippuakauppias'), 'saippuakauppias' );
+ equal(_.str.reverse(123), '321', 'Non string');
+ equal(_.str.reverse(123.45), '54.321', 'Non string');
+ equal(_.str.reverse(''), '', 'reversing empty string returns empty string' );
+ equal(_.str.reverse(null), '', 'reversing null returns empty string' );
+ equal(_.str.reverse(undefined), '', 'reversing undefined returns empty string' );
});
test('Strings: clean', function() {
- equals(_(' foo bar ').clean(), 'foo bar');
- equals(_(123).clean(), '123');
- equals(_('').clean(), '', 'claning empty string returns empty string');
- equals(_(null).clean(), '', 'claning null returns empty string');
- equals(_(undefined).clean(), '', 'claning undefined returns empty string');
+ equal(_(' foo bar ').clean(), 'foo bar');
+ equal(_(123).clean(), '123');
+ equal(_('').clean(), '', 'claning empty string returns empty string');
+ equal(_(null).clean(), '', 'claning null returns empty string');
+ equal(_(undefined).clean(), '', 'claning undefined returns empty string');
});
test('Strings: sprintf', function() {
// Should be very tested function already. Thanks to
// http://www.diveintojavascript.com/projects/sprintf-for-javascript
- equals(_.sprintf('Hello %s', 'me'), 'Hello me', 'basic');
- equals(_('Hello %s').sprintf('me'), 'Hello me', 'object');
- equals(_('hello %s').chain().sprintf('me').capitalize().value(), 'Hello me', 'Chaining works');
- equals(_.sprintf('%.1f', 1.22222), '1.2', 'round');
- equals(_.sprintf('%.1f', 1.17), '1.2', 'round 2');
- equals(_.sprintf('%(id)d - %(name)s', {id: 824, name: 'Hello World'}), '824 - Hello World', 'Named replacements work');
- equals(_.sprintf('%(args[0].id)d - %(args[1].name)s', {args: [{id: 824}, {name: 'Hello World'}]}), '824 - Hello World', 'Named replacements with arrays work');
+ equal(_.sprintf('Hello %s', 'me'), 'Hello me', 'basic');
+ equal(_('Hello %s').sprintf('me'), 'Hello me', 'object');
+ equal(_('hello %s').chain().sprintf('me').capitalize().value(), 'Hello me', 'Chaining works');
+ equal(_.sprintf('%.1f', 1.22222), '1.2', 'round');
+ equal(_.sprintf('%.1f', 1.17), '1.2', 'round 2');
+ equal(_.sprintf('%(id)d - %(name)s', {id: 824, name: 'Hello World'}), '824 - Hello World', 'Named replacements work');
+ equal(_.sprintf('%(args[0].id)d - %(args[1].name)s', {args: [{id: 824}, {name: 'Hello World'}]}), '824 - Hello World', 'Named replacements with arrays work');
});
test('Strings: vsprintf', function() {
- equals(_.vsprintf('Hello %s', ['me']), 'Hello me', 'basic');
- equals(_('Hello %s').vsprintf(['me']), 'Hello me', 'object');
- equals(_('hello %s').chain().vsprintf(['me']).capitalize().value(), 'Hello me', 'Chaining works');
- equals(_.vsprintf('%.1f', [1.22222]), '1.2', 'round');
- equals(_.vsprintf('%.1f', [1.17]), '1.2', 'round 2');
- equals(_.vsprintf('%(id)d - %(name)s', [{id: 824, name: 'Hello World'}]), '824 - Hello World', 'Named replacement works');
- equals(_.vsprintf('%(args[0].id)d - %(args[1].name)s', [{args: [{id: 824}, {name: 'Hello World'}]}]), '824 - Hello World', 'Named replacement with arrays works');
+ equal(_.vsprintf('Hello %s', ['me']), 'Hello me', 'basic');
+ equal(_('Hello %s').vsprintf(['me']), 'Hello me', 'object');
+ equal(_('hello %s').chain().vsprintf(['me']).capitalize().value(), 'Hello me', 'Chaining works');
+ equal(_.vsprintf('%.1f', [1.22222]), '1.2', 'round');
+ equal(_.vsprintf('%.1f', [1.17]), '1.2', 'round 2');
+ equal(_.vsprintf('%(id)d - %(name)s', [{id: 824, name: 'Hello World'}]), '824 - Hello World', 'Named replacement works');
+ equal(_.vsprintf('%(args[0].id)d - %(args[1].name)s', [{args: [{id: 824}, {name: 'Hello World'}]}]), '824 - Hello World', 'Named replacement with arrays works');
});
test('Strings: startsWith', function() {
@@ -187,160 +187,162 @@ $(document).ready(function() {
});
test('String: clean', function(){
- equals(_.clean(' foo bar '), 'foo bar');
- equals(_.clean(''), '');
- equals(_.clean(null), '');
- equals(_.clean(1), '1');
+ equal(_.clean(' foo bar '), 'foo bar');
+ equal(_.clean(''), '');
+ equal(_.clean(null), '');
+ equal(_.clean(1), '1');
});
test('String: count', function(){
- equals(_('Hello world').count('l'), 3);
- equals(_('Hello world').count('Hello'), 1);
- equals(_('Hello world').count('foo'), 0);
- equals(_('x.xx....x.x').count('x'), 5);
- equals(_('').count('x'), 0);
- equals(_(null).count('x'), 0);
- equals(_(undefined).count('x'), 0);
- equals(_(12345).count(1), 1);
- equals(_(11345).count(1), 2);
+ equal(_('Hello world').count('l'), 3);
+ equal(_('Hello world').count('Hello'), 1);
+ equal(_('Hello world').count('foo'), 0);
+ equal(_('x.xx....x.x').count('x'), 5);
+ equal(_('').count('x'), 0);
+ equal(_(null).count('x'), 0);
+ equal(_(undefined).count('x'), 0);
+ equal(_(12345).count(1), 1);
+ equal(_(11345).count(1), 2);
});
test('String: insert', function(){
- equals(_('Hello ').insert(6, 'Jessy'), 'Hello Jessy');
- equals(_('Hello ').insert(100, 'Jessy'), 'Hello Jessy');
- equals(_('').insert(100, 'Jessy'), 'Jessy');
- equals(_(null).insert(100, 'Jessy'), 'Jessy');
- equals(_(undefined).insert(100, 'Jessy'), 'Jessy');
- equals(_(12345).insert(6, 'Jessy'), '12345Jessy');
+ equal(_('Hello ').insert(6, 'Jessy'), 'Hello Jessy');
+ equal(_('Hello ').insert(100, 'Jessy'), 'Hello Jessy');
+ equal(_('').insert(100, 'Jessy'), 'Jessy');
+ equal(_(null).insert(100, 'Jessy'), 'Jessy');
+ equal(_(undefined).insert(100, 'Jessy'), 'Jessy');
+ equal(_(12345).insert(6, 'Jessy'), '12345Jessy');
});
test('String: splice', function(){
- equals(_('https://edtsech@bitbucket.org/edtsech/underscore.strings').splice(30, 7, 'epeli'),
+ equal(_('https://edtsech@bitbucket.org/edtsech/underscore.strings').splice(30, 7, 'epeli'),
'https://edtsech@bitbucket.org/epeli/underscore.strings');
- equals(_.splice(12345, 1, 2, 321), '132145', 'Non strings');
+ equal(_.splice(12345, 1, 2, 321), '132145', 'Non strings');
});
test('String: succ', function(){
- equals(_('a').succ(), 'b');
- equals(_('A').succ(), 'B');
- equals(_('+').succ(), ',');
- equals(_(1).succ(), '2');
+ equal(_('a').succ(), 'b');
+ equal(_('A').succ(), 'B');
+ equal(_('+').succ(), ',');
+ equal(_(1).succ(), '2');
});
test('String: titleize', function(){
- equals(_('the titleize string method').titleize(), 'The Titleize String Method');
- equals(_('the titleize string method').titleize(), 'The Titleize String Method');
- equals(_('').titleize(), '', 'Titleize empty string returns empty string');
- equals(_(null).titleize(), '', 'Titleize null returns empty string');
- equals(_(undefined).titleize(), '', 'Titleize undefined returns empty string');
- equals(_('let\'s have some fun').titleize(), 'Let\'s Have Some Fun');
- equals(_(123).titleize(), '123');
+ equal(_('the titleize string method').titleize(), 'The Titleize String Method');
+ equal(_('the titleize string method').titleize(), 'The Titleize String Method');
+ equal(_('').titleize(), '', 'Titleize empty string returns empty string');
+ equal(_(null).titleize(), '', 'Titleize null returns empty string');
+ equal(_(undefined).titleize(), '', 'Titleize undefined returns empty string');
+ equal(_('let\'s have some fun').titleize(), 'Let\'s Have Some Fun');
+ equal(_(123).titleize(), '123');
});
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');
- equals(_('').camelize(), '', 'Camelize empty string returns empty string');
- equals(_(null).camelize(), '', 'Camelize null returns empty string');
- equals(_(undefined).camelize(), '', 'Camelize undefined returns empty string');
- equals(_(123).camelize(), '123');
+ equal(_('the_camelize_string_method').camelize(), 'theCamelizeStringMethod');
+ equal(_('-the-camelize-string-method').camelize(), 'TheCamelizeStringMethod');
+ equal(_('the camelize string method').camelize(), 'theCamelizeStringMethod');
+ equal(_(' the camelize string method').camelize(), 'theCamelizeStringMethod');
+ equal(_('the camelize string method').camelize(), 'theCamelizeStringMethod');
+ equal(_('').camelize(), '', 'Camelize empty string returns empty string');
+ equal(_(null).camelize(), '', 'Camelize null returns empty string');
+ equal(_(undefined).camelize(), '', 'Camelize undefined returns empty string');
+ equal(_(123).camelize(), '123');
});
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');
- equals(_('').underscored(), '');
- equals(_(null).underscored(), '');
- equals(_(undefined).underscored(), '');
- equals(_(123).underscored(), '123');
+ equal(_('the-underscored-string-method').underscored(), 'the_underscored_string_method');
+ equal(_('theUnderscoredStringMethod').underscored(), 'the_underscored_string_method');
+ equal(_('TheUnderscoredStringMethod').underscored(), 'the_underscored_string_method');
+ equal(_(' the underscored string method').underscored(), 'the_underscored_string_method');
+ equal(_('').underscored(), '');
+ equal(_(null).underscored(), '');
+ equal(_(undefined).underscored(), '');
+ equal(_(123).underscored(), '123');
});
test('String: dasherize', function(){
- equals(_('the_dasherize_string_method').dasherize(), 'the-dasherize-string-method');
- equals(_('TheDasherizeStringMethod').dasherize(), '-the-dasherize-string-method');
- equals(_('thisIsATest').dasherize(), 'this-is-a-test');
- equals(_('this Is A Test').dasherize(), 'this-is-a-test');
- equals(_('thisIsATest123').dasherize(), 'this-is-a-test123');
- equals(_('123thisIsATest').dasherize(), '123this-is-a-test');
- equals(_('the dasherize string method').dasherize(), 'the-dasherize-string-method');
- equals(_('the dasherize string method ').dasherize(), 'the-dasherize-string-method');
- equals(_('téléphone').dasherize(), 'téléphone');
- equals(_('foo$bar').dasherize(), 'foo$bar');
- equals(_('').dasherize(), '');
- equals(_(null).dasherize(), '');
- equals(_(undefined).dasherize(), '');
- equals(_(123).dasherize(), '123');
+ equal(_('the_dasherize_string_method').dasherize(), 'the-dasherize-string-method');
+ equal(_('TheDasherizeStringMethod').dasherize(), '-the-dasherize-string-method');
+ equal(_('thisIsATest').dasherize(), 'this-is-a-test');
+ equal(_('this Is A Test').dasherize(), 'this-is-a-test');
+ equal(_('thisIsATest123').dasherize(), 'this-is-a-test123');
+ equal(_('123thisIsATest').dasherize(), '123this-is-a-test');
+ equal(_('the dasherize string method').dasherize(), 'the-dasherize-string-method');
+ equal(_('the dasherize string method ').dasherize(), 'the-dasherize-string-method');
+ equal(_('téléphone').dasherize(), 'téléphone');
+ equal(_('foo$bar').dasherize(), 'foo$bar');
+ equal(_('').dasherize(), '');
+ equal(_(null).dasherize(), '');
+ equal(_(undefined).dasherize(), '');
+ equal(_(123).dasherize(), '123');
});
test('String: camelize', function(){
- equals(_.camelize('-moz-transform'), 'MozTransform');
- equals(_.camelize('webkit-transform'), 'webkitTransform');
- equals(_.camelize('under_scored'), 'underScored');
- equals(_.camelize(' with spaces'), 'withSpaces');
- equals(_('').camelize(), '');
- equals(_(null).camelize(), '');
- equals(_(undefined).camelize(), '');
+ equal(_.camelize('-moz-transform'), 'MozTransform');
+ equal(_.camelize('webkit-transform'), 'webkitTransform');
+ equal(_.camelize('under_scored'), 'underScored');
+ equal(_.camelize(' with spaces'), 'withSpaces');
+ equal(_('').camelize(), '');
+ equal(_(null).camelize(), '');
+ equal(_(undefined).camelize(), '');
});
test('String: join', function(){
- equals(_.join(1, 2, 3, 4), '21314');
- equals(_.join('|', 'foo', 'bar', 'baz'), 'foo|bar|baz');
- equals(_.join('',2,3,null), '23');
- equals(_.join(null,2,3), '23');
+ equal(_.join(1, 2, 3, 4), '21314');
+ equal(_.join('|', 'foo', 'bar', 'baz'), 'foo|bar|baz');
+ equal(_.join('',2,3,null), '23');
+ equal(_.join(null,2,3), '23');
});
test('String: classify', function(){
- equals(_.classify(1), '1');
- equals(_('some_class_name').classify(), 'SomeClassName');
+ equal(_.classify(1), '1');
+ equal(_('some_class_name').classify(), 'SomeClassName');
+ equal(_('my wonderfull class_name').classify(), 'MyWonderfullClassName');
+ equal(_('my wonderfull.class.name').classify(), 'MyWonderfullClassName');
});
test('String: humanize', function(){
- equals(_('the_humanize_string_method').humanize(), 'The humanize string method');
- equals(_('ThehumanizeStringMethod').humanize(), 'Thehumanize string method');
- equals(_('the humanize string method').humanize(), 'The humanize string method');
- equals(_('the humanize_id string method_id').humanize(), 'The humanize id string method');
- equals(_('the humanize string method ').humanize(), 'The humanize string method');
- equals(_(' capitalize dash-CamelCase_underscore trim ').humanize(), 'Capitalize dash camel case underscore trim');
- equals(_(123).humanize(), '123');
- equals(_('').humanize(), '');
- equals(_(null).humanize(), '');
- equals(_(undefined).humanize(), '');
+ equal(_('the_humanize_string_method').humanize(), 'The humanize string method');
+ equal(_('ThehumanizeStringMethod').humanize(), 'Thehumanize string method');
+ equal(_('the humanize string method').humanize(), 'The humanize string method');
+ equal(_('the humanize_id string method_id').humanize(), 'The humanize id string method');
+ equal(_('the humanize string method ').humanize(), 'The humanize string method');
+ equal(_(' capitalize dash-CamelCase_underscore trim ').humanize(), 'Capitalize dash camel case underscore trim');
+ equal(_(123).humanize(), '123');
+ equal(_('').humanize(), '');
+ equal(_(null).humanize(), '');
+ equal(_(undefined).humanize(), '');
});
test('String: truncate', function(){
- equals(_('Hello world').truncate(6, 'read more'), 'Hello read more');
- equals(_('Hello world').truncate(5), 'Hello...');
- equals(_('Hello').truncate(10), 'Hello');
- equals(_('').truncate(10), '');
- equals(_(null).truncate(10), '');
- equals(_(undefined).truncate(10), '');
- equals(_(1234567890).truncate(5), '12345...');
+ equal(_('Hello world').truncate(6, 'read more'), 'Hello read more');
+ equal(_('Hello world').truncate(5), 'Hello...');
+ equal(_('Hello').truncate(10), 'Hello');
+ equal(_('').truncate(10), '');
+ equal(_(null).truncate(10), '');
+ equal(_(undefined).truncate(10), '');
+ equal(_(1234567890).truncate(5), '12345...');
});
test('String: prune', function(){
- equals(_('Hello, cruel world').prune(6, ' read more'), 'Hello read more');
- equals(_('Hello, world').prune(5, 'read a lot more'), 'Hello, world');
- equals(_('Hello, world').prune(5), 'Hello...');
- equals(_('Hello, world').prune(8), 'Hello...');
- equals(_('Hello, cruel world').prune(15), 'Hello, cruel...');
- equals(_('Hello world').prune(22), 'Hello world');
- equals(_('Привет, жестокий мир').prune(6, ' read more'), 'Привет read more');
- equals(_('Привет, мир').prune(6, 'read a lot more'), 'Привет, мир');
- equals(_('Привет, мир').prune(6), 'Привет...');
- equals(_('Привет, мир').prune(8), 'Привет...');
- equals(_('Привет, жестокий мир').prune(16), 'Привет, жестокий...');
- equals(_('Привет, мир').prune(22), 'Привет, мир');
- equals(_('alksjd!!!!!!....').prune(100, ''), 'alksjd!!!!!!....');
- equals(_(123).prune(10), '123');
- equals(_(123).prune(1, 321), '321');
- equals(_('').prune(5), '');
- equals(_(null).prune(5), '');
- equals(_(undefined).prune(5), '');
+ equal(_('Hello, cruel world').prune(6, ' read more'), 'Hello read more');
+ equal(_('Hello, world').prune(5, 'read a lot more'), 'Hello, world');
+ equal(_('Hello, world').prune(5), 'Hello...');
+ equal(_('Hello, world').prune(8), 'Hello...');
+ equal(_('Hello, cruel world').prune(15), 'Hello, cruel...');
+ equal(_('Hello world').prune(22), 'Hello world');
+ equal(_('Привет, жестокий мир').prune(6, ' read more'), 'Привет read more');
+ equal(_('Привет, мир').prune(6, 'read a lot more'), 'Привет, мир');
+ equal(_('Привет, мир').prune(6), 'Привет...');
+ equal(_('Привет, мир').prune(8), 'Привет...');
+ equal(_('Привет, жестокий мир').prune(16), 'Привет, жестокий...');
+ equal(_('Привет, мир').prune(22), 'Привет, мир');
+ equal(_('alksjd!!!!!!....').prune(100, ''), 'alksjd!!!!!!....');
+ equal(_(123).prune(10), '123');
+ equal(_(123).prune(1, 321), '321');
+ equal(_('').prune(5), '');
+ equal(_(null).prune(5), '');
+ equal(_(undefined).prune(5), '');
});
test('String: isBlank', function(){
@@ -356,38 +358,39 @@ $(document).ready(function() {
});
test('String: escapeRegExp', function(){
- equals(_.escapeRegExp(/hello(?=\sworld)/.source), 'hello\\(\\?\\=\\\\sworld\\)', 'with lookahead');
- equals(_.escapeRegExp(/hello(?!\shell)/.source), 'hello\\(\\?\\!\\\\shell\\)', 'with negative lookahead');
+ equal(_.escapeRegExp(/hello(?=\sworld)/.source), 'hello\\(\\?\\=\\\\sworld\\)', 'with lookahead');
+ equal(_.escapeRegExp(/hello(?!\shell)/.source), 'hello\\(\\?\\!\\\\shell\\)', 'with negative lookahead');
});
test('String: escapeHTML', function(){
- equals(_('<div>Blah & "blah" & \'blah\'</div>').escapeHTML(),
- '&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &apos;blah&apos;&lt;/div&gt;');
- equals(_('&lt;').escapeHTML(), '&amp;lt;');
- equals(_(5).escapeHTML(), '5');
- equals(_('').escapeHTML(), '');
- equals(_(null).escapeHTML(), '');
- equals(_(undefined).escapeHTML(), '');
+ equal(_('<div>Blah & "blah" & \'blah\'</div>').escapeHTML(),
+ '&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &#39;blah&#39;&lt;/div&gt;');
+ equal(_('&lt;').escapeHTML(), '&amp;lt;');
+ equal(_(5).escapeHTML(), '5');
+ equal(_('').escapeHTML(), '');
+ equal(_(null).escapeHTML(), '');
+ equal(_(undefined).escapeHTML(), '');
});
test('String: unescapeHTML', function(){
- equals(_('&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &apos;blah&apos;&lt;/div&gt;').unescapeHTML(),
+ equal(_('&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &apos;blah&#39;&lt;/div&gt;').unescapeHTML(),
'<div>Blah & "blah" & \'blah\'</div>');
- equals(_('&amp;lt;').unescapeHTML(), '&lt;');
- equals(_('&#39;').unescapeHTML(), '\'');
- equals(_('&#0039;').unescapeHTML(), '\'');
- equals(_('&#x4a;').unescapeHTML(), 'J');
- equals(_('&#x04A;').unescapeHTML(), 'J');
- equals(_('&#X4A;').unescapeHTML(), '&#X4A;');
- equals(_('&_#39;').unescapeHTML(), '&_#39;');
- equals(_('&#39_;').unescapeHTML(), '&#39_;');
- equals(_('&amp;#38;').unescapeHTML(), '&#38;');
- equals(_('&#38;amp;').unescapeHTML(), '&amp;');
- equals(_('').unescapeHTML(), '');
- equals(_(null).unescapeHTML(), '');
- equals(_(undefined).unescapeHTML(), '');
- equals(_(5).unescapeHTML(), '5');
- // equals(_(undefined).unescapeHTML(), '');
+ equal(_('&amp;lt;').unescapeHTML(), '&lt;');
+ equal(_('&apos;').unescapeHTML(), '\'');
+ equal(_('&#39;').unescapeHTML(), '\'');
+ equal(_('&#0039;').unescapeHTML(), '\'');
+ equal(_('&#x4a;').unescapeHTML(), 'J');
+ equal(_('&#x04A;').unescapeHTML(), 'J');
+ equal(_('&#X4A;').unescapeHTML(), '&#X4A;');
+ equal(_('&_#39;').unescapeHTML(), '&_#39;');
+ equal(_('&#39_;').unescapeHTML(), '&#39_;');
+ equal(_('&amp;#38;').unescapeHTML(), '&#38;');
+ equal(_('&#38;amp;').unescapeHTML(), '&amp;');
+ equal(_('').unescapeHTML(), '');
+ equal(_(null).unescapeHTML(), '');
+ equal(_(undefined).unescapeHTML(), '');
+ equal(_(5).unescapeHTML(), '5');
+ // equal(_(undefined).unescapeHTML(), '');
});
test('String: words', function() {
@@ -404,230 +407,235 @@ $(document).ready(function() {
});
test('String: chars', function() {
- equals(_('Hello').chars().length, 5);
- equals(_(123).chars().length, 3);
- equals(_('').chars().length, 0);
- equals(_(null).chars().length, 0);
- equals(_(undefined).chars().length, 0);
+ equal(_('Hello').chars().length, 5);
+ equal(_(123).chars().length, 3);
+ equal(_('').chars().length, 0);
+ equal(_(null).chars().length, 0);
+ equal(_(undefined).chars().length, 0);
});
test('String: swapCase', function(){
- equals(_('AaBbCcDdEe').swapCase(), 'aAbBcCdDeE');
- equals(_('Hello World').swapCase(), 'hELLO wORLD');
- equals(_('').swapCase(), '');
- equals(_(null).swapCase(), '');
- equals(_(undefined).swapCase(), '');
+ equal(_('AaBbCcDdEe').swapCase(), 'aAbBcCdDeE');
+ equal(_('Hello World').swapCase(), 'hELLO wORLD');
+ equal(_('').swapCase(), '');
+ equal(_(null).swapCase(), '');
+ equal(_(undefined).swapCase(), '');
});
test('String: lines', function() {
- equals(_('Hello\nWorld').lines().length, 2);
- equals(_('Hello World').lines().length, 1);
- equals(_(123).lines().length, 1);
- equals(_('').lines().length, 1);
- equals(_(null).lines().length, 0);
- equals(_(undefined).lines().length, 0);
+ equal(_('Hello\nWorld').lines().length, 2);
+ equal(_('Hello World').lines().length, 1);
+ equal(_(123).lines().length, 1);
+ equal(_('').lines().length, 1);
+ equal(_(null).lines().length, 0);
+ equal(_(undefined).lines().length, 0);
});
test('String: pad', function() {
- equals(_('1').pad(8), ' 1');
- equals(_(1).pad(8), ' 1');
- equals(_('1').pad(8, '0'), '00000001');
- equals(_('1').pad(8, '0', 'left'), '00000001');
- equals(_('1').pad(8, '0', 'right'), '10000000');
- equals(_('1').pad(8, '0', 'both'), '00001000');
- equals(_('foo').pad(8, '0', 'both'), '000foo00');
- equals(_('foo').pad(7, '0', 'both'), '00foo00');
- equals(_('foo').pad(7, '!@$%dofjrofj', 'both'), '!!foo!!');
- equals(_('').pad(2), ' ');
- equals(_(null).pad(2), ' ');
- equals(_(undefined).pad(2), ' ');
+ equal(_('1').pad(8), ' 1');
+ equal(_(1).pad(8), ' 1');
+ equal(_('1').pad(8, '0'), '00000001');
+ equal(_('1').pad(8, '0', 'left'), '00000001');
+ equal(_('1').pad(8, '0', 'right'), '10000000');
+ equal(_('1').pad(8, '0', 'both'), '00001000');
+ equal(_('foo').pad(8, '0', 'both'), '000foo00');
+ equal(_('foo').pad(7, '0', 'both'), '00foo00');
+ equal(_('foo').pad(7, '!@$%dofjrofj', 'both'), '!!foo!!');
+ equal(_('').pad(2), ' ');
+ equal(_(null).pad(2), ' ');
+ equal(_(undefined).pad(2), ' ');
});
test('String: lpad', function() {
- equals(_('1').lpad(8), ' 1');
- equals(_(1).lpad(8), ' 1');
- equals(_('1').lpad(8, '0'), '00000001');
- equals(_('1').lpad(8, '0', 'left'), '00000001');
- equals(_('').lpad(2), ' ');
- equals(_(null).lpad(2), ' ');
- equals(_(undefined).lpad(2), ' ');
+ equal(_('1').lpad(8), ' 1');
+ equal(_(1).lpad(8), ' 1');
+ equal(_('1').lpad(8, '0'), '00000001');
+ equal(_('1').lpad(8, '0', 'left'), '00000001');
+ equal(_('').lpad(2), ' ');
+ equal(_(null).lpad(2), ' ');
+ equal(_(undefined).lpad(2), ' ');
});
test('String: rpad', function() {
- equals(_('1').rpad(8), '1 ');
- equals(_(1).lpad(8), ' 1');
- equals(_('1').rpad(8, '0'), '10000000');
- equals(_('foo').rpad(8, '0'), 'foo00000');
- equals(_('foo').rpad(7, '0'), 'foo0000');
- equals(_('').rpad(2), ' ');
- equals(_(null).rpad(2), ' ');
- equals(_(undefined).rpad(2), ' ');
+ equal(_('1').rpad(8), '1 ');
+ equal(_(1).lpad(8), ' 1');
+ equal(_('1').rpad(8, '0'), '10000000');
+ equal(_('foo').rpad(8, '0'), 'foo00000');
+ equal(_('foo').rpad(7, '0'), 'foo0000');
+ equal(_('').rpad(2), ' ');
+ equal(_(null).rpad(2), ' ');
+ equal(_(undefined).rpad(2), ' ');
});
test('String: lrpad', function() {
- equals(_('1').lrpad(8), ' 1 ');
- equals(_(1).lrpad(8), ' 1 ');
- equals(_('1').lrpad(8, '0'), '00001000');
- equals(_('foo').lrpad(8, '0'), '000foo00');
- equals(_('foo').lrpad(7, '0'), '00foo00');
- equals(_('foo').lrpad(7, '!@$%dofjrofj'), '!!foo!!');
- equals(_('').lrpad(2), ' ');
- equals(_(null).lrpad(2), ' ');
- equals(_(undefined).lrpad(2), ' ');
+ equal(_('1').lrpad(8), ' 1 ');
+ equal(_(1).lrpad(8), ' 1 ');
+ equal(_('1').lrpad(8, '0'), '00001000');
+ equal(_('foo').lrpad(8, '0'), '000foo00');
+ equal(_('foo').lrpad(7, '0'), '00foo00');
+ equal(_('foo').lrpad(7, '!@$%dofjrofj'), '!!foo!!');
+ equal(_('').lrpad(2), ' ');
+ equal(_(null).lrpad(2), ' ');
+ equal(_(undefined).lrpad(2), ' ');
});
test('String: toNumber', function() {
- deepEqual(_('not a number').toNumber(), Number.NaN);
- equals(_(0).toNumber(), 0);
- equals(_('0').toNumber(), 0);
- equals(_('0000').toNumber(), 0);
- equals(_('2.345').toNumber(), 2);
- equals(_('2.345').toNumber(NaN), 2);
- equals(_('2.345').toNumber(2), 2.35);
- equals(_('2.344').toNumber(2), 2.34);
- equals(_('2').toNumber(2), 2.00);
- equals(_(2).toNumber(2), 2.00);
- equals(_(-2).toNumber(), -2);
- equals(_('-2').toNumber(), -2);
- equals(_('').toNumber(), 0);
- equals(_(null).toNumber(), 0);
- equals(_(undefined).toNumber(), 0);
+ deepEqual(_('not a number').toNumber(), NaN);
+ equal(_(0).toNumber(), 0);
+ equal(_('0').toNumber(), 0);
+ equal(_('0.0').toNumber(), 0);
+ equal(_('0.1').toNumber(), 0);
+ equal(_('0.1').toNumber(1), 0.1);
+ equal(_(' 0.1 ').toNumber(1), 0.1);
+ equal(_('0000').toNumber(), 0);
+ equal(_('2.345').toNumber(), 2);
+ equal(_('2.345').toNumber(NaN), 2);
+ equal(_('2.345').toNumber(2), 2.35);
+ equal(_('2.344').toNumber(2), 2.34);
+ equal(_('2').toNumber(2), 2.00);
+ equal(_(2).toNumber(2), 2.00);
+ equal(_(-2).toNumber(), -2);
+ equal(_('-2').toNumber(), -2);
+ equal(_('').toNumber(), 0);
+ equal(_(null).toNumber(), 0);
+ equal(_(undefined).toNumber(), 0);
});
test('String: numberFormat', function() {
- equals(_.numberFormat(9000), '9,000');
- equals(_.numberFormat(9000, 0), '9,000');
- equals(_.numberFormat(90000, 2), '90,000.00');
- equals(_.numberFormat(1000.754), '1,001');
- equals(_.numberFormat(1000.754, 2), '1,000.75');
- equals(_.numberFormat(1000.754, 0, ',', '.'), '1.001');
- equals(_.numberFormat(1000.754, 2, ',', '.'), '1.000,75');
- equals(_.numberFormat(1000000.754, 2, ',', '.'), '1.000.000,75');
- equals(_.numberFormat(1000000000), '1,000,000,000');
- equals(_.numberFormat(100000000), '100,000,000');
- equals(_.numberFormat('not number'), '');
- equals(_.numberFormat(), '');
- equals(_.numberFormat(null, '.', ','), '');
- equals(_.numberFormat(undefined, '.', ','), '');
- equals(_.numberFormat(new Number(5000)), '5,000');
+ equal(_.numberFormat(9000), '9,000');
+ equal(_.numberFormat(9000, 0), '9,000');
+ equal(_.numberFormat(9000, 0, '', ''), '9000');
+ equal(_.numberFormat(90000, 2), '90,000.00');
+ equal(_.numberFormat(1000.754), '1,001');
+ equal(_.numberFormat(1000.754, 2), '1,000.75');
+ equal(_.numberFormat(1000.754, 0, ',', '.'), '1.001');
+ equal(_.numberFormat(1000.754, 2, ',', '.'), '1.000,75');
+ equal(_.numberFormat(1000000.754, 2, ',', '.'), '1.000.000,75');
+ equal(_.numberFormat(1000000000), '1,000,000,000');
+ equal(_.numberFormat(100000000), '100,000,000');
+ equal(_.numberFormat('not number'), '');
+ equal(_.numberFormat(), '');
+ equal(_.numberFormat(null, '.', ','), '');
+ equal(_.numberFormat(undefined, '.', ','), '');
+ equal(_.numberFormat(new Number(5000)), '5,000');
});
test('String: strRight', function() {
- equals(_('This_is_a_test_string').strRight('_'), 'is_a_test_string');
- equals(_('This_is_a_test_string').strRight('string'), '');
- equals(_('This_is_a_test_string').strRight(), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strRight(''), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strRight('-'), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strRight(''), 'This_is_a_test_string');
- equals(_('').strRight('foo'), '');
- equals(_(null).strRight('foo'), '');
- equals(_(undefined).strRight('foo'), '');
- equals(_(12345).strRight(2), '345');
+ equal(_('This_is_a_test_string').strRight('_'), 'is_a_test_string');
+ equal(_('This_is_a_test_string').strRight('string'), '');
+ equal(_('This_is_a_test_string').strRight(), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strRight(''), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strRight('-'), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strRight(''), 'This_is_a_test_string');
+ equal(_('').strRight('foo'), '');
+ equal(_(null).strRight('foo'), '');
+ equal(_(undefined).strRight('foo'), '');
+ equal(_(12345).strRight(2), '345');
});
test('String: strRightBack', function() {
- equals(_('This_is_a_test_string').strRightBack('_'), 'string');
- equals(_('This_is_a_test_string').strRightBack('string'), '');
- equals(_('This_is_a_test_string').strRightBack(), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strRightBack(''), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strRightBack('-'), 'This_is_a_test_string');
- equals(_('').strRightBack('foo'), '');
- equals(_(null).strRightBack('foo'), '');
- equals(_(undefined).strRightBack('foo'), '');
- equals(_(12345).strRightBack(2), '345');
+ equal(_('This_is_a_test_string').strRightBack('_'), 'string');
+ equal(_('This_is_a_test_string').strRightBack('string'), '');
+ equal(_('This_is_a_test_string').strRightBack(), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strRightBack(''), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strRightBack('-'), 'This_is_a_test_string');
+ equal(_('').strRightBack('foo'), '');
+ equal(_(null).strRightBack('foo'), '');
+ equal(_(undefined).strRightBack('foo'), '');
+ equal(_(12345).strRightBack(2), '345');
});
test('String: strLeft', function() {
- equals(_('This_is_a_test_string').strLeft('_'), 'This');
- equals(_('This_is_a_test_string').strLeft('This'), '');
- equals(_('This_is_a_test_string').strLeft(), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strLeft(''), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strLeft('-'), 'This_is_a_test_string');
- equals(_('').strLeft('foo'), '');
- equals(_(null).strLeft('foo'), '');
- equals(_(undefined).strLeft('foo'), '');
- equals(_(123454321).strLeft(3), '12');
+ equal(_('This_is_a_test_string').strLeft('_'), 'This');
+ equal(_('This_is_a_test_string').strLeft('This'), '');
+ equal(_('This_is_a_test_string').strLeft(), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strLeft(''), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strLeft('-'), 'This_is_a_test_string');
+ equal(_('').strLeft('foo'), '');
+ equal(_(null).strLeft('foo'), '');
+ equal(_(undefined).strLeft('foo'), '');
+ equal(_(123454321).strLeft(3), '12');
});
test('String: strLeftBack', function() {
- equals(_('This_is_a_test_string').strLeftBack('_'), 'This_is_a_test');
- equals(_('This_is_a_test_string').strLeftBack('This'), '');
- equals(_('This_is_a_test_string').strLeftBack(), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strLeftBack(''), 'This_is_a_test_string');
- equals(_('This_is_a_test_string').strLeftBack('-'), 'This_is_a_test_string');
- equals(_('').strLeftBack('foo'), '');
- equals(_(null).strLeftBack('foo'), '');
- equals(_(undefined).strLeftBack('foo'), '');
- equals(_(123454321).strLeftBack(3), '123454');
+ equal(_('This_is_a_test_string').strLeftBack('_'), 'This_is_a_test');
+ equal(_('This_is_a_test_string').strLeftBack('This'), '');
+ equal(_('This_is_a_test_string').strLeftBack(), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strLeftBack(''), 'This_is_a_test_string');
+ equal(_('This_is_a_test_string').strLeftBack('-'), 'This_is_a_test_string');
+ equal(_('').strLeftBack('foo'), '');
+ equal(_(null).strLeftBack('foo'), '');
+ equal(_(undefined).strLeftBack('foo'), '');
+ equal(_(123454321).strLeftBack(3), '123454');
});
test('Strings: stripTags', function() {
- equals(_('a <a href="#">link</a>').stripTags(), 'a link');
- equals(_('a <a href="#">link</a><script>alert("hello world!")</scr'+'ipt>').stripTags(), 'a linkalert("hello world!")');
- equals(_('<html><body>hello world</body></html>').stripTags(), 'hello world');
- equals(_(123).stripTags(), '123');
- equals(_('').stripTags(), '');
- equals(_(null).stripTags(), '');
- equals(_(undefined).stripTags(), '');
+ equal(_('a <a href="#">link</a>').stripTags(), 'a link');
+ equal(_('a <a href="#">link</a><script>alert("hello world!")</scr'+'ipt>').stripTags(), 'a linkalert("hello world!")');
+ equal(_('<html><body>hello world</body></html>').stripTags(), 'hello world');
+ equal(_(123).stripTags(), '123');
+ equal(_('').stripTags(), '');
+ equal(_(null).stripTags(), '');
+ equal(_(undefined).stripTags(), '');
});
test('Strings: toSentence', function() {
- equals(_.toSentence(['jQuery']), 'jQuery', 'array with a single element');
- equals(_.toSentence(['jQuery', 'MooTools']), 'jQuery and MooTools', 'array with two elements');
- equals(_.toSentence(['jQuery', 'MooTools', 'Prototype']), 'jQuery, MooTools and Prototype', 'array with three elements');
- equals(_.toSentence(['jQuery', 'MooTools', 'Prototype', 'YUI']), 'jQuery, MooTools, Prototype and YUI', 'array with multiple elements');
- equals(_.toSentence(['jQuery', 'MooTools', 'Prototype'], ',', ' or '), 'jQuery,MooTools or Prototype', 'handles custom separators');
+ equal(_.toSentence(['jQuery']), 'jQuery', 'array with a single element');
+ equal(_.toSentence(['jQuery', 'MooTools']), 'jQuery and MooTools', 'array with two elements');
+ equal(_.toSentence(['jQuery', 'MooTools', 'Prototype']), 'jQuery, MooTools and Prototype', 'array with three elements');
+ equal(_.toSentence(['jQuery', 'MooTools', 'Prototype', 'YUI']), 'jQuery, MooTools, Prototype and YUI', 'array with multiple elements');
+ equal(_.toSentence(['jQuery', 'MooTools', 'Prototype'], ',', ' or '), 'jQuery,MooTools or Prototype', 'handles custom separators');
});
test('Strings: toSentenceSerial', function (){
- equals(_.toSentenceSerial(['jQuery']), 'jQuery');
- equals(_.toSentenceSerial(['jQuery', 'MooTools']), 'jQuery and MooTools');
- equals(_.toSentenceSerial(['jQuery', 'MooTools', 'Prototype']), 'jQuery, MooTools, and Prototype');
+ equal(_.toSentenceSerial(['jQuery']), 'jQuery');
+ equal(_.toSentenceSerial(['jQuery', 'MooTools']), 'jQuery and MooTools');
+ equal(_.toSentenceSerial(['jQuery', 'MooTools', 'Prototype']), 'jQuery, MooTools, and Prototype');
});
test('Strings: slugify', function() {
- equals(_('Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/').slugify(), 'jack-jill-like-numbers-123-and-4-and-silly-characters');
- equals(_('Un éléphant à l\'orée du bois').slugify(), 'un-elephant-a-loree-du-bois');
- equals(_('I know latin characters: á í ó ú ç ã õ ñ ü').slugify(), 'i-know-latin-characters-a-i-o-u-c-a-o-n-u');
- equals(_('I am a word too, even though I am but a single letter: i!').slugify(), 'i-am-a-word-too-even-though-i-am-but-a-single-letter-i');
- equals(_('').slugify(), '');
- equals(_(null).slugify(), '');
- equals(_(undefined).slugify(), '');
+ equal(_('Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/').slugify(), 'jack-jill-like-numbers-123-and-4-and-silly-characters');
+ equal(_('Un éléphant à l\'orée du bois').slugify(), 'un-elephant-a-loree-du-bois');
+ equal(_('I know latin characters: á í ó ú ç ã õ ñ ü').slugify(), 'i-know-latin-characters-a-i-o-u-c-a-o-n-u');
+ equal(_('I am a word too, even though I am but a single letter: i!').slugify(), 'i-am-a-word-too-even-though-i-am-but-a-single-letter-i');
+ equal(_('').slugify(), '');
+ equal(_(null).slugify(), '');
+ equal(_(undefined).slugify(), '');
});
test('Strings: quote', function(){
- equals(_.quote('foo'), '"foo"');
- equals(_.quote('"foo"'), '""foo""');
- equals(_.quote(1), '"1"');
+ equal(_.quote('foo'), '"foo"');
+ equal(_.quote('"foo"'), '""foo""');
+ equal(_.quote(1), '"1"');
// alias
- equals(_.q('foo'), '"foo"');
- equals(_.q(''), '""');
- equals(_.q(null), '""');
- equals(_.q(undefined), '""');
+ equal(_.q('foo'), '"foo"');
+ equal(_.q(''), '""');
+ equal(_.q(null), '""');
+ equal(_.q(undefined), '""');
});
test('Strings: surround', function(){
- equals(_.surround('foo', 'ab'), 'abfooab');
- equals(_.surround(1, 'ab'), 'ab1ab');
- equals(_.surround(1, 2), '212');
- equals(_.surround('foo', 1), '1foo1');
- equals(_.surround('', 1), '11');
- equals(_.surround(null, 1), '11');
- equals(_.surround('foo', ''), 'foo');
- equals(_.surround('foo', null), 'foo');
+ equal(_.surround('foo', 'ab'), 'abfooab');
+ equal(_.surround(1, 'ab'), 'ab1ab');
+ equal(_.surround(1, 2), '212');
+ equal(_.surround('foo', 1), '1foo1');
+ equal(_.surround('', 1), '11');
+ equal(_.surround(null, 1), '11');
+ equal(_.surround('foo', ''), 'foo');
+ equal(_.surround('foo', null), 'foo');
});
test('Strings: repeat', function() {
- equals(_.repeat('foo'), '');
- equals(_.repeat('foo', 3), 'foofoofoo');
- equals(_.repeat('foo', '3'), 'foofoofoo');
- equals(_.repeat(123, 2), '123123');
- equals(_.repeat(1234, 2, '*'), '1234*1234');
- equals(_.repeat(1234, 2, 5), '123451234');
- equals(_.repeat('', 2), '');
- equals(_.repeat(null, 2), '');
- equals(_.repeat(undefined, 2), '');
+ equal(_.repeat('foo'), '');
+ equal(_.repeat('foo', 3), 'foofoofoo');
+ equal(_.repeat('foo', '3'), 'foofoofoo');
+ equal(_.repeat(123, 2), '123123');
+ equal(_.repeat(1234, 2, '*'), '1234*1234');
+ equal(_.repeat(1234, 2, 5), '123451234');
+ equal(_.repeat('', 2), '');
+ equal(_.repeat(null, 2), '');
+ equal(_.repeat(undefined, 2), '');
});
});
View
86 test/test_underscore/arrays.js
@@ -2,7 +2,7 @@ $(document).ready(function() {
module("Arrays");
- test("arrays: first", function() {
+ test("first", function() {
equal(_.first([1,2,3]), 1, 'can pull out the first element of an array');
equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
equal(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');
@@ -14,9 +14,11 @@ $(document).ready(function() {
equal(result.join(','), '1,1', 'works well with _.map');
result = (function() { return _.take([1,2,3], 2); })();
equal(result.join(','), '1,2', 'aliased as take');
+
+ equal(_.first(null), undefined, 'handles nulls');
});
- test("arrays: rest", function() {
+ test("rest", function() {
var numbers = [1, 2, 3, 4];
equal(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');
equal(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');
@@ -25,9 +27,11 @@ $(document).ready(function() {
equal(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
result = _.map([[1,2,3],[1,2,3]], _.rest);
equal(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
+ result = (function(){ return _(arguments).drop(); })(1, 2, 3, 4);
+ equal(result.join(', '), '2, 3, 4', 'aliased as drop and works on arguments object');
});
- test("arrays: initial", function() {
+ test("initial", function() {
equal(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
equal(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
var result = (function(){ return _(arguments).initial(); })(1, 2, 3, 4);
@@ -36,7 +40,7 @@ $(document).ready(function() {
equal(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');
});
- test("arrays: last", function() {
+ test("last", function() {
equal(_.last([1,2,3]), 3, 'can pull out the last element of an array');
equal(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
equal(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
@@ -45,15 +49,17 @@ $(document).ready(function() {
equal(result, 4, 'works on an arguments object');
result = _.map([[1,2,3],[1,2,3]], _.last);
equal(result.join(','), '3,3', 'works well with _.map');
+
+ equal(_.last(null), undefined, 'handles nulls');
});
- test("arrays: compact", function() {
+ test("compact", function() {
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
var result = (function(){ return _(arguments).compact().length; })(0, 1, false, 2, false, 3);
equal(result, 3, 'works on an arguments object');
});
- test("arrays: flatten", function() {
+ test("flatten", function() {
if (window.JSON) {
var list = [1, [2], [3, [[[4]]]]];
equal(JSON.stringify(_.flatten(list)), '[1,2,3,4]', 'can flatten nested arrays');
@@ -63,7 +69,7 @@ $(document).ready(function() {
}
});
- test("arrays: without", function() {
+ test("without", function() {
var list = [1, 2, 1, 0, 3, 1, 4];
equal(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);
@@ -74,7 +80,7 @@ $(document).ready(function() {
ok(_.without(list, list[0]).length == 1, 'ditto.');
});
- test("arrays: uniq", function() {
+ test("uniq", function() {
var list = [1, 2, 1, 3, 1, 4];
equal(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
@@ -91,31 +97,9 @@ $(document).ready(function() {
var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
equal(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
-
- var list = [];
- list[2] = list[3] = null;
- list[8] = 2;
- list[10] = 2;
- list[11] = 5;
- list[14] = 5;
- list[16] = 8;
- list[19] = 8;
- list[26] = list[29] = undefined;
- list[33] = "hi";
-
- var result = _.uniq(list, true);
- if (0 in [undefined]) {
- // According to the JScript ES 3 spec, section 2.1.26, JScript 5.x (IE <=
- // 8) treats `undefined` elements in arrays as elisions.
- deepEqual(result, [null, 2, 5, 8, undefined, "hi"], "Works with sorted sparse arrays");
- equal(result.length, 6, "The resulting array should not be sparse");
- } else {
- deepEqual(result, [null, 2, 5, 8, "hi"], "Works with sorted sparse arrays where `undefined` elements are elided");
- equal(result.length, 5, "The resulting array should not be sparse");
- }
});
- test("arrays: intersection", function() {
+ test("intersection", function() {
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
equal(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
equal(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
@@ -123,7 +107,7 @@ $(document).ready(function() {
equal(result.join(''), 'moe', 'works on an arguments object');
});
- test("arrays: union", function() {
+ test("union", function() {
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
equal(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
@@ -131,7 +115,7 @@ $(document).ready(function() {
equal(result.join(' '), '1 2 3 30 40 1', 'takes the union of a list of nested arrays');
});
- test("arrays: difference", function() {
+ test("difference", function() {
var result = _.difference([1, 2, 3], [2, 30, 40]);
equal(result.join(' '), '1 3', 'takes the difference of two arrays');
@@ -139,13 +123,28 @@ $(document).ready(function() {
equal(result.join(' '), '3 4', 'takes the difference of three arrays');
});
- test('arrays: zip', function() {
+ test('zip', function() {
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
var stooges = _.zip(names, ages, leaders);
equal(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
});
- test("arrays: indexOf", function() {
+ test('object', function() {
+ var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
+ var shouldBe = {moe: 30, larry: 40, curly: 50};
+ ok(_.isEqual(result, shouldBe), 'two arrays zipped together into an object');
+
+ result = _.object([['one', 1], ['two', 2], ['three', 3]]);
+ shouldBe = {one: 1, two: 2, three: 3};
+ ok(_.isEqual(result, shouldBe), 'an array of pairs zipped together into an object');
+
+ var stooges = {moe: 30, larry: 40, curly: 50};
+ ok(_.isEqual(_.object(_.pairs(stooges)), stooges), 'an object converted to pairs and back to an object');
+
+ ok(_.isEqual(_.object(null), {}), 'handles nulls');
+ });
+
+ test("indexOf", function() {
var numbers = [1, 2, 3];
numbers.indexOf = null;
equal(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
@@ -164,19 +163,30 @@ $(document).ready(function() {
numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40;
index = _.indexOf(numbers, num, true);
equal(index, 1, '40 is in the list');
+
+ numbers = [1, 2, 3, 1, 2, 3, 1, 2, 3];
+ index = _.indexOf(numbers, 2, 5);
+ equal(index, 7, 'supports the fromIndex argument');
});
- <