diff --git a/README.md b/README.md index 25ff3c9f0..b4e935a37 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ require({ * `_.forEach` should be chainable [[#142](https://github.com/documentcloud/underscore/issues/142), [test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L521-524)] * `_.forEach` should allow exiting iteration early [[#211](https://github.com/documentcloud/underscore/issues/211), [test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L616-635)] * `_.isElement` should use strict equality for its duck type check [[test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L731-740)] - * `_.isEmpty` and `_.size` should support jQuery/MooTools DOM query collections [[#690](https://github.com/documentcloud/underscore/pull/690), [test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L767-772)] + * `_.isEmpty` should support jQuery/MooTools DOM query collections [[#690](https://github.com/documentcloud/underscore/pull/690), [test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L767-772)] * `_.isEqual` should return `true` for like-objects from different documents [[test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L808-828)] * `_.isObject` should avoid V8 bug [#2291](http://code.google.com/p/v8/issues/detail?id=2291) [[#605](https://github.com/documentcloud/underscore/issues/605), [test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L836-848)] * `_.isNaN(new Number(NaN))` should return `true` [[test](https://github.com/bestiejs/lodash/blob/v0.6.1/test/test.js#L856-858)] diff --git a/build.js b/build.js index d6f58f68a..fcc1fed4b 100755 --- a/build.js +++ b/build.js @@ -233,7 +233,7 @@ 'rest': [], 'result': ['isFunction'], 'shuffle': [], - 'size': ['isArguments', 'isFunction', 'keys'], + 'size': ['keys'], 'some': ['identity'], 'sortBy': [], 'sortedIndex': ['bind'], @@ -610,7 +610,7 @@ */ function removeNoArgsClass(source) { return removeVar(source, 'noArgsClass') - // remove `noArgsClass` from `_.clone`, `_.isEqual`, and `_.size` + // remove `noArgsClass` from `_.clone` and `_.isEqual` .replace(/ *\|\| *\(noArgsClass *&&[^)]+?\)\)/g, '') // remove `noArgsClass` from `_.isEqual` .replace(/if *\(noArgsClass[^}]+?}\n/, ''); @@ -1111,7 +1111,7 @@ if (isRemoved(source, 'clone', 'merge')) { source = removeFunction(source, 'isPlainObject'); } - if (isRemoved(source, 'clone', 'isArguments', 'isEmpty', 'isEqual', 'size')) { + if (isRemoved(source, 'clone', 'isArguments', 'isEmpty', 'isEqual')) { source = removeNoArgsClass(source); } if (isRemoved(source, 'isEqual', 'isPlainObject')) { diff --git a/doc/README.md b/doc/README.md index 7b52a8418..251d84d2b 100644 --- a/doc/README.md +++ b/doc/README.md @@ -1912,7 +1912,7 @@ initialize(); Creates a two dimensional array of the given object's key-value pairs, i.e. `[[key1, value1], [key2, value2]]`. #### Arguments -1. `object` *(Object)*: The object to inspect.. +1. `object` *(Object)*: The object to inspect. #### Returns *(Array)*: Returns new array of key-value pairs. @@ -2025,8 +2025,8 @@ _.pluck(stooges, 'name'); Produces a random number between `min` and `max` *(inclusive)*. If only one argument is passed, a number between `0` and the given number will be returned. If no arguments are passed `_.random` will act as `Math.random`. #### Arguments -1. `min` *(Number)*: The minimum possible value -2. `max` *(Number)*: The maximum possible value +1. `min` *(Number)*: The minimum possible value. +2. `max` *(Number)*: The maximum possible value. #### Returns *(Number)*: Returns the random number. diff --git a/lodash.js b/lodash.js index c53cc90b7..902e456e5 100644 --- a/lodash.js +++ b/lodash.js @@ -1920,45 +1920,6 @@ 'bottom': '}' }); - /** - * Gets the size of `value` by returning `value.length` if `value` is an - * array, string, or `arguments` object. If `value` is an object, size is - * determined by returning the number of own enumerable properties it has. - * - * @static - * @memberOf _ - * @category Objects - * @param {Array|Object|String} value The value to inspect. - * @returns {Number} Returns `value.length` or number of own enumerable properties. - * @example - * - * _.size([1, 2]); - * // => 2 - * - * _.size({ 'one': 1, 'two': 2, 'three': 3 }); - * // => 3 - * - * _.size('curly'); - * // => 5 - */ - function size(value) { - if (!value) { - return 0; - } - var className = toString.call(value), - length = value.length; - - // return `value.length` for `arguments` objects, arrays, strings, and DOM - // query collections of libraries like jQuery and MooTools - // http://code.google.com/p/fbug/source/browse/branches/firebug1.9/content/firebug/chrome/reps.js?r=12614#653 - // http://trac.webkit.org/browser/trunk/Source/WebCore/inspector/InjectedScriptSource.js?rev=125186#L609 - if (arrayLikeClasses[className] || (noArgsClass && isArguments(value)) || - (className == objectClass && length > -1 && length === length >>> 0 && isFunction(value.splice))) { - return length; - } - return keys(value).length; - } - /** * Creates an array composed of the own enumerable property values of `object`. * @@ -2365,6 +2326,34 @@ 'inLoop': '!' + filterIteratorOptions.inLoop }); + /** + * Gets the size of the `collection` by returning `collection.length` for arrays + * and array-like objects or the number of own enumerable properties for objects. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array|Object|String} collection The collection to inspect. + * @returns {Number} Returns `collection.length` or number of own enumerable properties. + * @example + * + * _.size([1, 2]); + * // => 2 + * + * _.size({ 'one': 1, 'two': 2, 'three': 3 }); + * // => 3 + * + * _.size('curly'); + * // => 5 + */ + function size(collection) { + if (!collection) { + return 0; + } + var length = collection.length; + return length > -1 && length === length >>> 0 ? length : keys(collection).length; + } + /** * Checks if the `callback` returns a truthy value for **any** element of a * `collection`. The function returns as soon as it finds passing value, and @@ -3833,8 +3822,8 @@ * @static * @memberOf _ * @category Utilities - * @param {Number} min The minimum possible value - * @param {Number} max The maximum possible value + * @param {Number} min The minimum possible value. + * @param {Number} max The maximum possible value. * @returns {Number} Returns the random number. * @example * diff --git a/lodash.min.js b/lodash.min.js index 729cdbc4f..d5b64d207 100644 --- a/lodash.min.js +++ b/lodash.min.js @@ -22,7 +22,7 @@ q.push,ht=U.propertyIsEnumerable,pt=q.slice,dt=U.toString,vt=tt.test(vt=pt.bind) ,i:"u[i]=A",e:"}}"},tn={j:"[]",i:"c(A,i,d)&&u.push(A)"},nn={q:"if(y)c=k(c,y)"},rn={i:{l:Gt.i}},sn={j:"",f:"if(!d)return[]",d:{b:"u=Array(l)",l:"u="+(zt?"Array(l)":"[]")},i:{b:"u[i]=c(A,i,d)",l:"u"+(zt?"[o]=":".push")+"(c(A,i,d))"}},on={r:i,a:"n,c,y",j:"{}",q:"var S=typeof c=='function';if(!S){var t=e.apply(E,arguments)}else if(y)c=k(c,y)",i:"if(S?!c(A,i,n):N(t,i)<0)u[i]=A"};jt&&(b=function(e){return!!e&&!!lt.call(e,"callee")});var un=gt||function(e){return dt.call(e)==Tt};w(/x/)&&(w=function(e){return"[object Function]"== dt.call(e)}),E(Kt)||(E=function(e,t){var n=i;if(!e||"object"!=typeof e||!t&&b(e))return n;var r=e.constructor;return(!qt||"function"==typeof e.toString||"string"!=typeof (e+""))&&(!w(r)||r instanceof r)?Ht?(cn(e,function(t,r){return n=!lt.call(e,r),i}),n===i):(cn(e,function(e,t){n=t}),n===i||lt.call(e,n)):n});var an=a({a:"n",j:"[]",i:"u.push(i)"}),fn=a(en,{i:"if(u[i]==null)"+en.i}),ln=a(en),cn=a(Gt,nn,rn,{r:i}),hn=a(Gt,nn,rn),pn=a({r:i,a:"n",j:"[]",i:"if(T(A))u.push(i)",e:"u.sort()"}),dn=a({a:"n" ,j:"{}",i:"u[A]=i"}),vn=a({a:"A",j:"true",q:"var H=z.call(A),l=A.length;if(D[H]"+(jt?"||P(A)":"")+"||(H==X&&l>-1&&l===l>>>0&&T(A.splice)))return!l",i:{l:"return false"}}),mn=bt?function(e){var t=typeof e;return"function"==t&&ht.call(e,"prototype")?an(e):e&&Kt[t]?bt(e):[]}:an,gn=a(en,{a:"n,ee,O,ff",q:"var J,L,Q,gg,dd=O==U;if(!dd)ff=[];for(var a=1,b=dd?2:arguments.length;a-1"},i:"if(A===hh)return true"}),xn=a(Gt,Yt),Tn=a(Gt,Zt),Nn=a(Gt,tn),Cn=a(Gt,nn,{j:"",i:"if(c(A,i,d))return A"}),kn=a(Gt,nn),Ln=a(Gt,Yt,{i:"q=c(A,i,d);(g.call(u,q)?u[q]:u[q]=[]).push(A)" +}),yn=a(on),bn=a({a:"n",j:"[]",i:"u"+(zt?"[o]=":".push")+"([i,A])"}),wn=a(on,{q:"if(typeof c!='function'){var q,t=e.apply(E,arguments),l=t.length;for(i=1;i-1"},i:"if(A===hh)return true"}),xn=a(Gt,Yt),Tn=a(Gt,Zt),Nn=a(Gt,tn),Cn=a(Gt,nn,{j:"",i:"if(c(A,i,d))return A"}),kn=a(Gt,nn),Ln=a(Gt,Yt,{i:"q=c(A,i,d);(g.call(u,q)?u[q]:u[q]=[]).push(A)" }),An=a(sn,{a:"d,V",q:"var C=w.call(arguments,2),S=typeof V=='function'",i:{b:"u[i]=(S?V:A[V]).apply(A,C)",l:"u"+(zt?"[o]=":".push")+"((S?V:A[V]).apply(A,C))"}}),On=a(Gt,sn),Mn=a(sn,{a:"d,bb",i:{b:"u[i]=A[bb]",l:"u"+(zt?"[o]=":".push")+"(A[bb])"}}),_n=a({a:"d,c,B,y",j:"B",q:"var W=arguments.length<3;if(y)c=k(c,y)",d:{b:"if(W)u=j[++i]"},i:{b:"u=c(u,A,i,d)",l:"u=W?(W=false,A):c(u,A,i,d)"}}),Dn=a(Gt,tn,{i:"!"+tn.i}),Pn=a(Gt,Zt,{j:"false",i:Zt.i.replace("!","")}),Hn=a(Gt,Yt,sn,{i:{b:"u[i]={a:c(A,i,d),b:i,d:A}" ,l:"u"+(zt?"[o]=":".push")+"({a:c(A,i,d),b:i,d:A})"},e:"u.sort(I);l=u.length;while(l--)u[l]=u[l].d"}),Bn=a(tn,{a:"d,aa",q:"var t=[];K(aa,function(A,q){t.push(q)});var cc=t.length",i:"for(var q,Z=true,s=0;s1){for(var i=1;ie?t():function(){if(1>--e)return t.apply (this,arguments)}},s.bind=_,s.bindAll=jn,s.chain=function(e){return e=new o(e),e._chain=n,e},s.clone=S,s.compact=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length;++nn?wt(0,r+n):Et(n,r-1))+1);r--;)if(e[r]===t)return r;return-1},s.map=On,s.max=L,s.memoize=function(e,t){var n={};return function(){var r=t?t.apply(this,arguments):arguments[0];return lt.call(n,r)?n[r]:n[r]=e.apply(this,arguments)}},s.merge=gn,s.min=function(e,t,n){var r=Infinity,i=r;if(!e)return i;var s=-1,o=e.length;if(!t){for(;++s>>0&&w(e.splice)?n:mn(e).length},s.some=Pn,s.sortBy=Hn,s.sortedIndex=O,s.tap=function(e,t){return t(e),e},s.template=function(e,t,n){n||(n={});var e=e+"",o,u;o=n.escape;var a=n.evaluate,f=n.interpolate,h=s.templateSettings,p=n=n.variable||h.variable;o==r&&(o=h.escape),a==r&&(a=h.evaluate||i),f==r&&(f=h.interpolate),o&&(e=e.replace(o,v)),f&&(e=e.replace(f,g)),a!=H&&(H=a,F=RegExp("|"+(a?"|"+a.source:""),"g")),o=at.length -,e=e.replace(F,m),o=o!=at.length,e="__p += '"+e.replace(it,c).replace(nt,l)+"';",at.length=0,p||(n=B||"obj",o?e="with("+n+"){"+e+"}":(n!=B&&(B=n,j=RegExp("(\\(\\s*)"+n+"\\."+n+"\\b","g")),e=e.replace(et,"$&"+n+".").replace(j,"$1__d"))),e=(o?e.replace(Q,""):e).replace(G,"$1").replace(Y,"$1;"),e="function("+n+"){"+(p?"":n+"||("+n+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":(p?"":",__d="+n+"."+n+"||"+n)+";")+e+"return __p}";try{ -u=Function("_","return "+e)(s)}catch(d){u=function(){throw d}}return t?u(t):(u.source=e,u)},s.throttle=function(e,t){function n(){a=new Date,u=r,e.apply(o,i)}var i,s,o,u,a=0;return function(){var r=new Date,f=t-(r-a);return i=arguments,o=this,0>=f?(a=r,s=e.apply(o,i)):u||(u=_t(n,f)),s}},s.times=function(e,t,n){var r=-1;if(n)for(;++r>>0?(Ft?dt.call -(e)==Ot:"string"==typeof e)?e.split(""):pt.call(e):En(e)},s.unescape=function(e){return e==r?"":(e+"").replace(K,y)},s.union=function(){for(var e=-1,t=[],n=ft.apply(t,arguments),r=n.length;++ek(t,n[e])&&t.push(n[e]);return t},s.uniq=M,s.uniqueId=function(e){var t=X++;return e?e+t:t},s.values=En,s.where=Bn,s.without=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length,i=u(arguments,1,20);++n>>0?t:mn(e).length},s.some=Pn,s.sortBy=Hn,s.sortedIndex=O,s.tap=function(e,t){return t(e),e},s.template=function(e,t,n){n||(n={});var e=e+"",o,u;o=n.escape;var a=n.evaluate,f=n.interpolate,h=s.templateSettings,p=n=n.variable||h.variable;o==r&&(o=h.escape),a==r&&(a=h.evaluate||i),f==r&&(f=h.interpolate),o&&(e=e.replace(o,v)),f&&(e=e.replace(f,g)),a!=H&&(H=a,F=RegExp("|"+(a?"|"+a.source:""),"g")),o=at.length,e=e.replace(F,m),o=o!=at.length,e="__p += '"+e.replace +(it,c).replace(nt,l)+"';",at.length=0,p||(n=B||"obj",o?e="with("+n+"){"+e+"}":(n!=B&&(B=n,j=RegExp("(\\(\\s*)"+n+"\\."+n+"\\b","g")),e=e.replace(et,"$&"+n+".").replace(j,"$1__d"))),e=(o?e.replace(Q,""):e).replace(G,"$1").replace(Y,"$1;"),e="function("+n+"){"+(p?"":n+"||("+n+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":(p?"":",__d="+n+"."+n+"||"+n)+";")+e+"return __p}";try{u=Function("_","return "+e)(s)}catch(d){u=function(){throw d +}}return t?u(t):(u.source=e,u)},s.throttle=function(e,t){function n(){a=new Date,u=r,e.apply(o,i)}var i,s,o,u,a=0;return function(){var r=new Date,f=t-(r-a);return i=arguments,o=this,0>=f?(a=r,s=e.apply(o,i)):u||(u=_t(n,f)),s}},s.times=function(e,t,n){var r=-1;if(n)for(;++r>>0?(Ft?dt.call(e)==Ot:"string"==typeof e)?e.split(""):pt.call(e):En(e)} +,s.unescape=function(e){return e==r?"":(e+"").replace(K,y)},s.union=function(){for(var e=-1,t=[],n=ft.apply(t,arguments),r=n.length;++ek(t,n[e])&&t.push(n[e]);return t},s.uniq=M,s.uniqueId=function(e){var t=X++;return e?e+t:t},s.values=En,s.where=Bn,s.without=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length,i=u(arguments,1,20);++n