From 473dd7660ba8c5e051e129df0c5c301e2c3a10c4 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 20 Sep 2012 21:06:32 -0700 Subject: [PATCH] Reduce `_.reduceRight` and update vendors. Former-commit-id: f7250ccb4b8f15052c1f1420947c2ac68963a92c --- build.js | 2 +- lodash.js | 38 +++--------- lodash.min.js | 56 ++++++++--------- vendor/underscore/test/arrays.js | 8 +++ vendor/underscore/test/collections.js | 16 +++++ vendor/underscore/test/objects.js | 7 ++- vendor/underscore/underscore-min.js | 34 +++------- vendor/underscore/underscore.js | 89 ++++++++++++++++----------- 8 files changed, 130 insertions(+), 120 deletions(-) diff --git a/build.js b/build.js index 53a3736a7..bb195371c 100755 --- a/build.js +++ b/build.js @@ -133,7 +133,7 @@ 'random': [], 'range': [], 'reduce': [], - 'reduceRight': ['keys'], + 'reduceRight': ['keys', 'reduce'], 'reject': ['identity'], 'rest': [], 'result': ['isFunction'], diff --git a/lodash.js b/lodash.js index e1d16d6e3..8595b3e38 100644 --- a/lodash.js +++ b/lodash.js @@ -2220,39 +2220,19 @@ if (!collection) { return accumulator; } - var length = collection.length, noaccum = arguments.length < 3; - if(thisArg) { - callback = bindIterator(callback, thisArg); + if (length !== +length) { + var props = keys(collection); + length = props.length; } - if (length === +length) { - var iteratee = noCharByIndex && toString.call(collection) == stringClass - ? collection.split('') - : collection; - - if (length && noaccum) { - accumulator = iteratee[--length]; - } - while (length--) { - accumulator = callback(accumulator, iteratee[length], length, collection); - } - return accumulator; - } - - var prop, - props = keys(collection); - - length = props.length; - if (length && noaccum) { - accumulator = collection[props[--length]]; - } - while (length--) { - prop = props[length]; - accumulator = callback(accumulator, collection[prop], prop, collection); - } - return accumulator; + return reduce(collection, function(accumulator, value, index, object) { + var index = props ? props[--length] : --length; + return noaccum + ? (noaccum = false, object[index]) + : callback.call(thisArg, accumulator, object[index], index, object); + }, accumulator); } /** diff --git a/lodash.min.js b/lodash.min.js index fbfc2bcce..af6567f54 100644 --- a/lodash.min.js +++ b/lodash.min.js @@ -10,31 +10,31 @@ a.m.i+"}}else{"),a.h||(t+="var v=typeof k=='function'&&r.call(k,'prototype');"); e||!t&&m(e))return n;var r=e.constructor;return(!kt||"function"==typeof e.toString||"string"!=typeof (e+""))&&(!g(r)||r instanceof r)?St?(Yt(e,function(e,t,r){return n=!G.call(r,t),i}),n===i):(Yt(e,function(e,t){n=t}),n===i||G.call(e,n)):n}function b(e,t,s,o,u){if(e==r)return e;s&&(t=i);if(s=Bt[typeof e]){var a=tt.call(e);if(!Dt[a]||Tt&&m(e))return e;var f=a==ct,s=f||(a==vt?Jt(e,n):s)}if(!s||!t)return s?f?et.call(e):Gt({},e):e;s=e.constructor;switch(a){case ht:return new s(e==n);case pt:return new s(+e);case dt:case gt:return new s(e);case mt:return s(e.source,z.exec(e))}o||(o=[]),u||(u=[]);for(a=o.length;a--;)if(o[a]==e)return u[a];var l=f?s(a=e.length):{};o.push(e),u.push(l);if(f)for(f=-1;++f++u;)if(c=K[u],G.call(e,c)&&(!G.call(t,c)||!w(e[c],t[c],s,o)))return i;return n}function E(e,t,n,r){if(!e)return n;var i=e.length,s=3>arguments.length;r&&(t=u(t,r));if(i===+i){var o=Ct&&tt.call(e)==gt?e.split(""):e;for(i&&s&&(n=o[--i]);i--;)n=t(n,o[i],i,e);return n}o=rn(e);for((i=o.length)&&s&&(n=e[o[--i]]);i--;)s=o[i],n=t(n,e[s], -s,e);return n}function S(e,t,n){if(e)return t==r||n?e[0]:et.call(e,0,t)}function x(e,t){var n=[];if(!e)return n;for(var r,i=-1,s=e.length;++in?ut(0,i+n):n)-1}for(;++ri&&(i=e[s]);return i}for(n&&(t=u -(t,n));++sr&&(r=n,i=e[s]);return i}function C(e,t,n){return e?et.call(e,t==r||n?1:t):[]}function k(e,t,n,r){if(!e)return 0;var i=0,s=e.length;if(n){r&&(n=A(n,r));for(t=n(t);i>>1,n(e[r])>>1,e[r]T(f,r))f.push(r),s.push(e[o]);return s}function A -(e,t){return At||nt&&2|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/,I=/&(?:amp|lt|gt|quot|#x27);/g,q=/\b__p\+='';/g,R=/\b(__p\+=)''\+/g,U=/(__e\(.*?\)|\b__t\))\+'';/g,z=/\w*$/,W=/(?:__e|__t=)\(\s*(?![\d\s"']|this\.)/g,X=RegExp("^"+(P.valueOf+"").replace(/[.*+?^=!:${}()|[\]\/\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),V=/($^)/,$=/[&<>"']/g,J=/['\n\r\t\u2028\u2029\\]/g,K="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf" -.split(" "),Q=D.concat,G=P.hasOwnProperty,Y=D.push,Z=P.propertyIsEnumerable,et=D.slice,tt=P.toString,nt=X.test(nt=et.bind)&&nt,rt=Math.floor,it=X.test(it=Array.isArray)&&it,st=e.isFinite,ot=X.test(ot=Object.keys)&&ot,ut=Math.max,at=Math.min,ft=Math.random,lt="[object Arguments]",ct="[object Array]",ht="[object Boolean]",pt="[object Date]",dt="[object Number]",vt="[object Object]",mt="[object RegExp]",gt="[object String]",yt=e.clearTimeout,bt=e.setTimeout,wt,Et,St,xt=n;(function(){function e(){this -.x=1}var t={0:1,length:1},n=[];e.prototype={valueOf:1,y:1};for(var r in new e)n.push(r);for(r in arguments)xt=!r;wt=4>(n+"").length,St="x"!=n[0],Et=(n.splice.call(t,0,1),t[0])})(1);var Tt=!m(arguments),Nt="x"!=et.call("x")[0],Ct="xx"!="x"[0]+Object("x")[0];try{var kt=("[object Object]",tt.call(e.document||0)==vt)}catch(Lt){}var At=nt&&/\n|Opera/.test(nt+tt.call(e.opera)),Ot=ot&&/^.+$|true/.test(ot+!!e.attachEvent),Mt=!At,_t={};_t[ht]=_t[pt]=_t["[object Function]"]=_t[dt]=_t[vt]=_t[mt]=i,_t[lt]=_t[ct]= -_t[gt]=n;var Dt={};Dt[lt]=Dt["[object Function]"]=i,Dt[ct]=Dt[ht]=Dt[pt]=Dt[dt]=Dt[vt]=Dt[mt]=Dt[gt]=n;var Pt={"&":"&","<":"<",">":">",'"':""","'":"'"},Ht={"&":"&","<":"<",">":">",""":'"',"'":"'"},Bt={"boolean":i,"function":n,object:n,number:i,string:i,"undefined":i,unknown:n},jt={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"};s.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g -,variable:""};var Ft={a:"e,d,y",j:"e",q:"if(!d)d=i;else if(y)d=c(d,y)",i:"if(d(A,j,e)===false)return u"},It={j:"{}",q:"var q;if(typeof d!='function'){var hh=d;d=function(A){return A[hh]}}else if(y)d=c(d,y)",i:"q=d(A,j,e);(h.call(u,q)?u[q]++:u[q]=1)"},qt={j:"true",i:"if(!d(A,j,e))return!u"},Rt={r:i,s:i,a:"n",j:"n",q:"for(var a=1,b=arguments.length;a-1"},i:"if(A===gg)return true"}),cn=c(Ft,It),hn=c(Ft,qt),pn=c(Ft,Ut),dn=c(Ft,zt,{j:"",i:"if(d(A,j,e))return A"}),vn=c(Ft,zt),mn=c(Ft,It,{i:"q=d(A,j,e);(h.call(u,q)?u[q]:u[q]=[]).push(A)"}),gn=c(Xt,{a:"e,U",q:"var C=w.call(arguments,2),R=typeof U=='function'",i:{b:"u[j]=(R?U:A[U]).apply(A,C)",l:"u"+(Ot?"[o]=":".push")+"((R?U:A[U]).apply(A,C))"}}),yn=c(Ft,Xt),bn= -c(Xt,{a:"e,aa",i:{b:"u[j]=A[aa]",l:"u"+(Ot?"[o]=":".push")+"(A[aa])"}}),wn=c({a:"e,d,B,y",j:"B",q:"var V=arguments.length<3;if(y)d=c(d,y)",d:{b:"if(V)u=k[++j]"},i:{b:"u=d(u,A,j,e)",l:"u=V?(V=false,A):d(u,A,j,e)"}}),En=c(Ft,Ut,{i:"!"+Ut.i}),Sn=c(Ft,qt,{j:"false",i:qt.i.replace("!","")}),xn=c(Ft,It,Xt,{i:{b:"u[j]={a:d(A,j,e),b:j,c:A}",l:"u"+(Ot?"[o]=":".push")+"({a:d(A,j,e),b:j,c:A})"},e:"u.sort(I);l=u.length;while(l--)u[l]=u[l].c"}),Tn=c(Ut,{a:"e,Z",q:"var t=[];J(Z,function(A,q){t.push(q)});var bb=t.length" -,i:"for(var q,Y=true,s=0;s1){for(var j=1;je?t():function(){if(1>--e)return t.apply(this,arguments)}},s.bind=A,s.bindAll=Nn,s.chain=function(e){return e=new o(e),e.__chain__=n,e},s.clone=b,s.compact=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length;++n< -r;)e[n]&&t.push(e[n]);return t},s.compose=function(){var e=arguments;return function(){for(var t=arguments,n=e.length;n--;)t=[e[n].apply(this,t)];return t[0]}},s.contains=ln,s.countBy=cn,s.debounce=function(e,t,n){function i(){a=r,n||(o=e.apply(u,s))}var s,o,u,a;return function(){var r=n&&!a;return s=arguments,u=this,yt(a),a=bt(i,t),r&&(o=e.apply(u,s)),o}},s.defaults=Qt,s.defer=function(e){var n=et.call(arguments,1);return bt(function(){return e.apply(t,n)},1)},s.delay=function(e,n){var r=et.call -(arguments,2);return bt(function(){return e.apply(t,r)},n)},s.difference=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length,i=Q.apply(t,arguments),i=a(i,r);++nT(t,n)){for(var u=1;un?ut(0,r+n):at(n,r-1))+1);r--;)if(e[r]===t)return r;return-1},s.lateBind=function(e,t){return l(t,e,et.call(arguments,2))},s.map=yn,s.max= -N,s.memoize=function(e,t){var n={};return function(){var r=t?t.apply(this,arguments):arguments[0];return G.call(n,r)?n[r]:n[r]=e.apply(this,arguments)}},s.merge=sn,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=f?(a=r,s=e.apply(o,i)):u||(u=bt(n,f)),s}},s.times=function(e,t,n){var r=-1,i=Array(e||0);if( -n)for(;++rT(t,n[e])&&t.push(n[e]);return t},s.uniq=L,s.uniqueId=function(e){var t=H++;return e?e+t:t},s.values=fn,s.where=Tn,s.without=function(e){var t=[];if(!e)return t -;for(var n=-1,r=e.length,i=a(arguments,1,20);++n++u;)if(c=K[u],G.call(e,c)&&(!G.call(t,c)||!w(e[c],t[c],s,o)))return i;return n}function E(e,t,n,r){if(!e)return n;var s=e.length,o=3>arguments.length;if(s!==+s)var u=rn(e),s=u.length;return wn(e,function(e,n,a,f){return a=u?u[--s]:--s,o?(o=i,f[a]):t.call(r,e,f[a],a,f)},n)}function S(e,t,n){if(e)return t==r||n?e[0]:et.call(e,0,t)} +function x(e,t){var n=[];if(!e)return n;for(var r,i=-1,s=e.length;++in?ut(0,i+n):n)-1}for(;++ri&&(i=e[s]);return i}for(n&&(t=u(t,n));++sr&&(r=n,i=e[s]);return i}function C(e,t,n) +{return e?et.call(e,t==r||n?1:t):[]}function k(e,t,n,r){if(!e)return 0;var i=0,s=e.length;if(n){r&&(n=A(n,r));for(t=n(t);i>>1,n(e[r])>>1,e[r]T(f,r))f.push(r),s.push(e[o]);return s}function A(e,t){return At||nt&&2|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/ +,I=/&(?:amp|lt|gt|quot|#x27);/g,q=/\b__p\+='';/g,R=/\b(__p\+=)''\+/g,U=/(__e\(.*?\)|\b__t\))\+'';/g,z=/\w*$/,W=/(?:__e|__t=)\(\s*(?![\d\s"']|this\.)/g,X=RegExp("^"+(P.valueOf+"").replace(/[.*+?^=!:${}()|[\]\/\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),V=/($^)/,$=/[&<>"']/g,J=/['\n\r\t\u2028\u2029\\]/g,K="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),Q=D.concat,G=P.hasOwnProperty,Y=D.push,Z=P.propertyIsEnumerable,et=D.slice,tt= +P.toString,nt=X.test(nt=et.bind)&&nt,rt=Math.floor,it=X.test(it=Array.isArray)&&it,st=e.isFinite,ot=X.test(ot=Object.keys)&&ot,ut=Math.max,at=Math.min,ft=Math.random,lt="[object Arguments]",ct="[object Array]",ht="[object Boolean]",pt="[object Date]",dt="[object Number]",vt="[object Object]",mt="[object RegExp]",gt="[object String]",yt=e.clearTimeout,bt=e.setTimeout,wt,Et,St,xt=n;(function(){function e(){this.x=1}var t={0:1,length:1},n=[];e.prototype={valueOf:1,y:1};for(var r in new e)n.push(r);for(r in +arguments)xt=!r;wt=4>(n+"").length,St="x"!=n[0],Et=(n.splice.call(t,0,1),t[0])})(1);var Tt=!m(arguments),Nt="x"!=et.call("x")[0],Ct="xx"!="x"[0]+Object("x")[0];try{var kt=("[object Object]",tt.call(e.document||0)==vt)}catch(Lt){}var At=nt&&/\n|Opera/.test(nt+tt.call(e.opera)),Ot=ot&&/^.+$|true/.test(ot+!!e.attachEvent),Mt=!At,_t={};_t[ht]=_t[pt]=_t["[object Function]"]=_t[dt]=_t[vt]=_t[mt]=i,_t[lt]=_t[ct]=_t[gt]=n;var Dt={};Dt[lt]=Dt["[object Function]"]=i,Dt[ct]=Dt[ht]=Dt[pt]=Dt[dt]=Dt[vt]=Dt[mt +]=Dt[gt]=n;var Pt={"&":"&","<":"<",">":">",'"':""","'":"'"},Ht={"&":"&","<":"<",">":">",""":'"',"'":"'"},Bt={"boolean":i,"function":n,object:n,number:i,string:i,"undefined":i,unknown:n},jt={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"};s.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""};var Ft={a:"e,d,y",j:"e",q:"if(!d)d=i;else if(y)d=c(d,y)",i:"if(d(A,j,e)===false)return u" +},It={j:"{}",q:"var q;if(typeof d!='function'){var hh=d;d=function(A){return A[hh]}}else if(y)d=c(d,y)",i:"q=d(A,j,e);(h.call(u,q)?u[q]++:u[q]=1)"},qt={j:"true",i:"if(!d(A,j,e))return!u"},Rt={r:i,s:i,a:"n",j:"n",q:"for(var a=1,b=arguments.length;a-1"},i:"if(A===gg)return true"}),cn=c(Ft,It),hn=c(Ft,qt),pn=c(Ft,Ut),dn=c(Ft,zt,{j:"",i:"if(d(A,j,e))return A"}),vn=c(Ft,zt),mn=c(Ft,It,{i:"q=d(A,j,e);(h.call(u,q)?u[q]:u[q]=[]).push(A)"}),gn=c(Xt,{a:"e,U",q:"var C=w.call(arguments,2),R=typeof U=='function'",i:{b:"u[j]=(R?U:A[U]).apply(A,C)",l:"u"+(Ot?"[o]=":".push")+"((R?U:A[U]).apply(A,C))"}}),yn=c(Ft,Xt),bn=c(Xt,{a:"e,aa",i:{b:"u[j]=A[aa]",l:"u"+(Ot?"[o]=":".push")+"(A[aa])"} +}),wn=c({a:"e,d,B,y",j:"B",q:"var V=arguments.length<3;if(y)d=c(d,y)",d:{b:"if(V)u=k[++j]"},i:{b:"u=d(u,A,j,e)",l:"u=V?(V=false,A):d(u,A,j,e)"}}),En=c(Ft,Ut,{i:"!"+Ut.i}),Sn=c(Ft,qt,{j:"false",i:qt.i.replace("!","")}),xn=c(Ft,It,Xt,{i:{b:"u[j]={a:d(A,j,e),b:j,c:A}",l:"u"+(Ot?"[o]=":".push")+"({a:d(A,j,e),b:j,c:A})"},e:"u.sort(I);l=u.length;while(l--)u[l]=u[l].c"}),Tn=c(Ut,{a:"e,Z",q:"var t=[];J(Z,function(A,q){t.push(q)});var bb=t.length",i:"for(var q,Y=true,s=0;s1){for(var j=1;je?t():function(){if(1>--e)return t.apply(this,arguments)}},s.bind=A,s.bindAll=Nn,s.chain=function(e){return e=new o(e),e.__chain__=n,e},s.clone=b,s.compact=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length;++nT(t,n)){for(var u=1;un?ut(0,r+n):at(n,r-1))+1);r--;)if(e[r]===t)return r;return-1},s.lateBind=function(e,t){return l(t,e,et.call(arguments,2))},s.map=yn,s.max=N,s.memoize=function(e,t){var n={};return function(){var r=t?t.apply(this,arguments) +:arguments[0];return G.call(n,r)?n[r]:n[r]=e.apply(this,arguments)}},s.merge=sn,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=f?(a=r,s=e.apply(o,i)):u||(u=bt(n,f)),s}},s.times=function(e,t,n){var r=-1,i=Array(e||0);if(n)for(;++rT(r,t[e])&&r.push(t[e]);return r},s.uniq=L,s.uniqueId=function(e){var t=H++;return e?e+t:t},s.values=fn,s.where=Tn,s.without=function(e){var t=[];if(!e)return t;for(var n=-1,r=e.length,i=a(arguments,1,20);++n 4 ? 'hasOwnProperty' : 'constructor'; + }); + equal(grouped.constructor.length, 1); + equal(grouped.hasOwnProperty.length, 2); + }); test('countBy', function() { @@ -341,6 +351,12 @@ $(document).ready(function() { var context = {}; _.countBy([{}], function(){ ok(this === context); }, context); + + grouped = _.countBy([4.2, 6.1, 6.4], function(num) { + return Math.floor(num) > 4 ? 'hasOwnProperty' : 'constructor'; + }); + equal(grouped.constructor, 1); + equal(grouped.hasOwnProperty, 2); }); test('sortedIndex', function() { diff --git a/vendor/underscore/test/objects.js b/vendor/underscore/test/objects.js index 236a9d955..22949c3bf 100644 --- a/vendor/underscore/test/objects.js +++ b/vendor/underscore/test/objects.js @@ -15,17 +15,22 @@ $(document).ready(function() { }); test("values", function() { - equal(_.values({one : 1, two : 2}).join(', '), '1, 2', 'can extract the values from an object'); + equal(_.values({one: 1, two: 2}).join(', '), '1, 2', 'can extract the values from an object'); + equal(_.values({one: 1, two: 2, length: 3}).join(', '), '1, 2, 3', '... even when one of them is "length"'); }); test("pairs", function() { deepEqual(_.pairs({one: 1, two: 2}), [['one', 1], ['two', 2]], 'can convert an object into pairs'); + deepEqual(_.pairs({one: 1, two: 2, length: 3}), [['one', 1], ['two', 2], ['length', 3]], '... even when one of them is "length"'); }); test("invert", function() { var obj = {first: 'Moe', second: 'Larry', third: 'Curly'}; equal(_.keys(_.invert(obj)).join(' '), 'Moe Larry Curly', 'can invert an object'); ok(_.isEqual(_.invert(_.invert(obj)), obj), 'two inverts gets you back where you started'); + + var obj = {length: 3}; + ok(_.invert(obj)['3'] == 'length', 'can invert an object with "length"') }); test("functions", function() { diff --git a/vendor/underscore/underscore-min.js b/vendor/underscore/underscore-min.js index ab6a0fcf7..6596e3d3a 100644 --- a/vendor/underscore/underscore-min.js +++ b/vendor/underscore/underscore-min.js @@ -5,29 +5,11 @@ // Oliver Steele's Functional, and John Resig's Micro-Templating. // For all details and documentation: // http://documentcloud.github.com/underscore -(function(){var r=this,J=r._,n={},k=Array.prototype,o=Object.prototype,s=k.push,h=k.slice,p=k.concat,l=o.toString,K=o.hasOwnProperty,w=k.forEach,x=k.map,y=k.reduce,z=k.reduceRight,A=k.filter,B=k.every,C=k.some,q=k.indexOf,D=k.lastIndexOf,o=Array.isArray,L=Object.keys,t=Function.prototype.bind,b=function(a){if(a instanceof b)return a;if(!(this instanceof b))return new b(a);this._wrapped=a};"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports=module.exports=b),exports._= -b):r._=b;b.VERSION="1.3.3";var i=b.each=b.forEach=function(a,c,d){if(a!=null)if(w&&a.forEach===w)a.forEach(c,d);else if(a.length===+a.length)for(var e=0,g=a.length;e2;a==null&& -(a=[]);if(!g&&y&&a.reduce===y){e&&(c=b.bind(c,e));return f?a.reduce(c,d):a.reduce(c)}i(a,function(a,b,h){if(g){b=g.keys[b];h=g.list}if(f)d=c.call(e,d,a,b,h);else{d=a;f=true}});if(!f)throw new TypeError("Reduce of empty array with no initial value");return d};b.reduceRight=b.foldr=function(a,c,d,e){a==null&&(a=[]);if(z&&a.reduceRight===z){e&&(c=b.bind(c,e));return arguments.length>2?a.reduceRight(c,d):a.reduceRight(c)}var g=b.keys(a).reverse(),f=b.toArray(a).reverse();return b.reduce(f,c,d,e,{keys:g, -list:a})};b.find=b.detect=function(a,c,b){var e;E(a,function(a,f,j){if(c.call(b,a,f,j)){e=a;return true}});return e};b.filter=b.select=function(a,c,b){var e=[];if(a==null)return e;if(A&&a.filter===A)return a.filter(c,b);i(a,function(a,f,j){c.call(b,a,f,j)&&(e[e.length]=a)});return e};b.reject=function(a,c,b){var e=[];if(a==null)return e;i(a,function(a,f,j){c.call(b,a,f,j)||(e[e.length]=a)});return e};b.every=b.all=function(a,c,d){c||(c=b.identity);var e=true;if(a==null)return e;if(B&&a.every===B)return a.every(c, -d);i(a,function(a,b,j){if(!(e=e&&c.call(d,a,b,j)))return n});return!!e};var E=b.some=b.any=function(a,c,d){c||(c=b.identity);var e=false;if(a==null)return e;if(C&&a.some===C)return a.some(c,d);i(a,function(a,b,j){if(e||(e=c.call(d,a,b,j)))return n});return!!e};b.include=b.contains=function(a,c){var b=false;if(a==null)return b;if(q&&a.indexOf===q)return a.indexOf(c)!=-1;return b=E(a,function(a){return a===c})};b.invoke=function(a,c){var d=h.call(arguments,2);return b.map(a,function(a){return(b.isFunction(c)? -c:a[c]).apply(a,d)})};b.pluck=function(a,c){return b.map(a,function(a){return a[c]})};b.where=function(a,c){return b.isEmpty(c)?[]:b.filter(a,function(a){for(var b in c)if(c[b]!==a[b])return false;return true})};b.max=function(a,c,d){if(!c&&b.isArray(a)&&a[0]===+a[0]&&a.length<65535)return Math.max.apply(Math,a);if(!c&&b.isEmpty(a))return-Infinity;var e={computed:-Infinity};i(a,function(a,b,j){b=c?c.call(d,a,b,j):a;b>=e.computed&&(e={value:a,computed:b})});return e.value};b.min=function(a,c,d){if(!c&& -b.isArray(a)&&a[0]===+a[0]&&a.length<65535)return Math.min.apply(Math,a);if(!c&&b.isEmpty(a))return Infinity;var e={computed:Infinity};i(a,function(a,b,j){b=c?c.call(d,a,b,j):a;bd||b===void 0)return 1;if(b>1;d(a[f])=0})})};b.difference=function(a){var c=p.apply(k,h.call(arguments,1));return b.filter(a,function(a){return!b.include(c,a)})};b.zip=function(){for(var a=h.call(arguments),c=b.max(b.pluck(a,"length")),d=Array(c),e=0;e=0;d--)b=[a[d].apply(this,b)];return b[0]}};b.after=function(a,b){return a<=0?b():function(){if(--a<1)return b.apply(this,arguments)}};b.keys=L||function(a){if(a!==Object(a))throw new TypeError("Invalid object");var c=[],d;for(d in a)b.has(a,d)&&(c[c.length]=d);return c};b.values=function(a){return b.map(a,b.identity)};b.pairs=function(a){return b.map(a,function(a,b){return[b,a]})};b.invert=function(a){return b.reduce(a,function(a,b,e){a[b]=e;return a},{})};b.functions=b.methods= -function(a){var c=[],d;for(d in a)b.isFunction(a[d])&&c.push(d);return c.sort()};b.extend=function(a){i(h.call(arguments,1),function(b){for(var d in b)a[d]=b[d]});return a};b.pick=function(a){var b={},d=p.apply(k,h.call(arguments,1));i(d,function(d){d in a&&(b[d]=a[d])});return b};b.omit=function(a){var c={},d=p.apply(k,h.call(arguments,1)),e;for(e in a)b.include(d,e)||(c[e]=a[e]);return c};b.defaults=function(a){i(h.call(arguments,1),function(b){for(var d in b)a[d]==null&&(a[d]=b[d])});return a}; -b.clone=function(a){return!b.isObject(a)?a:b.isArray(a)?a.slice():b.extend({},a)};b.tap=function(a,b){b(a);return a};var u=function(a,c,d,e){if(a===c)return a!==0||1/a==1/c;if(a==null||c==null)return a===c;if(a instanceof b)a=a._wrapped;if(c instanceof b)c=c._wrapped;var g=l.call(a);if(g!=l.call(c))return false;switch(g){case "[object String]":return a==""+c;case "[object Number]":return a!=+a?c!=+c:a==0?1/a==1/c:a==+c;case "[object Date]":case "[object Boolean]":return+a==+c;case "[object RegExp]":return a.source== -c.source&&a.global==c.global&&a.multiline==c.multiline&&a.ignoreCase==c.ignoreCase}if(typeof a!="object"||typeof c!="object")return false;for(var f=d.length;f--;)if(d[f]==a)return e[f]==c;d.push(a);e.push(c);var f=0,j=true;if(g=="[object Array]"){f=a.length;if(j=f==c.length)for(;f--;)if(!(j=u(a[f],c[f],d,e)))break}else{var g=a.constructor,h=c.constructor;if(g!==h&&(!b.isFunction(g)||!(g instanceof g&&b.isFunction(h)&&h instanceof h)))return false;for(var i in a)if(b.has(a,i)){f++;if(!(j=b.has(c,i)&& -u(a[i],c[i],d,e)))break}if(j){for(i in c)if(b.has(c,i)&&!f--)break;j=!f}}d.pop();e.pop();return j};b.isEqual=function(a,b){return u(a,b,[],[])};b.isEmpty=function(a){if(a==null)return true;if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(b.has(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=o||function(a){return l.call(a)=="[object Array]"};b.isObject=function(a){return a===Object(a)};i("Arguments,Function,String,Number,Date,RegExp".split(","), -function(a){b["is"+a]=function(b){return l.call(b)=="[object "+a+"]"}});b.isArguments(arguments)||(b.isArguments=function(a){return!(!a||!b.has(a,"callee"))});b.isFinite=function(a){return b.isNumber(a)&&isFinite(a)};b.isNaN=function(a){return b.isNumber(a)&&a!=+a};b.isBoolean=function(a){return a===true||a===false||l.call(a)=="[object Boolean]"};b.isNull=function(a){return a===null};b.isUndefined=function(a){return a===void 0};b.has=function(a,b){return K.call(a,b)};b.noConflict=function(){r._=J; -return this};b.identity=function(a){return a};b.times=function(a,b,d){for(var e=0;e":">",'"':""","'":"'","/":"/"}};m.unescape=b.invert(m.escape);var M={escape:RegExp("["+b.keys(m.escape).join("")+"]","g"),unescape:RegExp("("+b.keys(m.unescape).join("|")+")","g")};b.each(["escape","unescape"],function(a){b[a]=function(b){return b==null?"":(""+ -b).replace(M[a],function(b){return m[a][b]})}});b.result=function(a,c){if(a==null)return null;var d=a[c];return b.isFunction(d)?d.call(a):d};b.mixin=function(a){i(b.functions(a),function(c){var d=b[c]=a[c];b.prototype[c]=function(){var a=[this._wrapped];s.apply(a,arguments);a=d.apply(b,a);return this._chain?b(a).chain():a}})};var N=0;b.uniqueId=function(a){var b=N++;return a?a+b:b};b.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var v=/(.)^/, -O={"'":"'","\\":"\\","\r":"r","\n":"n","\t":"t","\u2028":"u2028","\u2029":"u2029"},P=/\\|'|\r|\n|\t|\u2028|\u2029/g;b.template=function(a,c,d){var d=b.defaults({},d,b.templateSettings),e=RegExp([(d.escape||v).source,(d.interpolate||v).source,(d.evaluate||v).source].join("|")+"|$","g"),g=0,f="__p+='";a.replace(e,function(b,c,d,e,h){f=f+a.slice(g,h).replace(P,function(a){return"\\"+O[a]});f=f+(c?"'+\n((__t=("+c+"))==null?'':_.escape(__t))+\n'":d?"'+\n((__t=("+d+"))==null?'':__t)+\n'":e?"';\n"+e+"\n__p+='": -"");g=h+b.length});f=f+"';\n";d.variable||(f="with(obj||{}){\n"+f+"}\n");f="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+f+"return __p;\n";try{var h=new Function(d.variable||"obj","_",f)}catch(i){i.source=f;throw i;}if(c)return h(c,b);c=function(a){return h.call(this,a,b)};c.source="function("+(d.variable||"obj")+"){\n"+f+"}";return c};b.chain=function(a){return b(a).chain()};b.mixin(b);i("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var c= -k[a];b.prototype[a]=function(){var d=this._wrapped;c.apply(d,arguments);(a=="shift"||a=="splice")&&d.length===0&&delete d[0];return this._chain?b(d).chain():d}});i(["concat","join","slice"],function(a){var c=k[a];b.prototype[a]=function(){var a=c.apply(this._wrapped,arguments);return this._chain?b(a).chain():a}});b.extend(b.prototype,{chain:function(){this._chain=true;return this},value:function(){return this._wrapped}})}).call(this); +// Underscore.js 1.3.3 +// (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc. +// Underscore may be freely distributed under the MIT license. +// Portions of Underscore are inspired or borrowed from Prototype, +// Oliver Steele's Functional, and John Resig's Micro-Templating. +// For all details and documentation: +// http://documentcloud.github.com/underscore +(function(){var e=this,t=e._,n={},r=Array.prototype,i=Object.prototype,s=Function.prototype,o=r.push,u=r.slice,a=r.concat,f=r.unshift,l=i.toString,c=i.hasOwnProperty,h=r.forEach,p=r.map,d=r.reduce,v=r.reduceRight,m=r.filter,g=r.every,y=r.some,b=r.indexOf,w=r.lastIndexOf,E=Array.isArray,S=Object.keys,x=s.bind,T=function(e){if(e instanceof T)return e;if(!(this instanceof T))return new T(e);this._wrapped=e};typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(exports=module.exports=T),exports._=T):e._=T,T.VERSION="1.3.3";var N=T.each=T.forEach=function(e,t,r){if(e==null)return;if(h&&e.forEach===h)e.forEach(t,r);else if(e.length===+e.length){for(var i=0,s=e.length;i2;e==null&&(e=[]);if(!C&&d&&e.reduce===d)return r&&(t=T.bind(t,r)),i?e.reduce(t,n):e.reduce(t);N(e,function(e,s,o){C&&(s=C.keys[s],o=C.list),i?n=t.call(r,n,e,s,o):(n=e,i=!0)});if(!i)throw new TypeError("Reduce of empty array with no initial value");return n},T.reduceRight=T.foldr=function(e,t,n,r){var i=arguments.length>2;e==null&&(e=[]);if(v&&e.reduceRight===v)return r&&(t=T.bind(t,r)),arguments.length>2?e.reduceRight(t,n):e.reduceRight(t);var s=T.toArray(e).reverse();r&&!i&&(t=T.bind(t,r)),C={keys:T.keys(e).reverse(),list:e};var o=i?T.reduce(s,t,n,r):T.reduce(s,t);return C=null,o},T.find=T.detect=function(e,t,n){var r;return k(e,function(e,i,s){if(t.call(n,e,i,s))return r=e,!0}),r},T.filter=T.select=function(e,t,n){var r=[];return e==null?r:m&&e.filter===m?e.filter(t,n):(N(e,function(e,i,s){t.call(n,e,i,s)&&(r[r.length]=e)}),r)},T.reject=function(e,t,n){var r=[];return e==null?r:(N(e,function(e,i,s){t.call(n,e,i,s)||(r[r.length]=e)}),r)},T.every=T.all=function(e,t,r){t||(t=T.identity);var i=!0;return e==null?i:g&&e.every===g?e.every(t,r):(N(e,function(e,s,o){if(!(i=i&&t.call(r,e,s,o)))return n}),!!i)};var k=T.some=T.any=function(e,t,r){t||(t=T.identity);var i=!1;return e==null?i:y&&e.some===y?e.some(t,r):(N(e,function(e,s,o){if(i||(i=t.call(r,e,s,o)))return n}),!!i)};T.contains=T.include=function(e,t){var n=!1;return e==null?n:b&&e.indexOf===b?e.indexOf(t)!=-1:(n=k(e,function(e){return e===t}),n)},T.invoke=function(e,t){var n=u.call(arguments,2);return T.map(e,function(e){return(T.isFunction(t)?t:e[t]).apply(e,n)})},T.pluck=function(e,t){return T.map(e,function(e){return e[t]})},T.where=function(e,t){return T.isEmpty(t)?[]:T.filter(e,function(e){for(var n in t)if(t[n]!==e[n])return!1;return!0})},T.max=function(e,t,n){if(!t&&T.isArray(e)&&e[0]===+e[0]&&e.length<65535)return Math.max.apply(Math,e);if(!t&&T.isEmpty(e))return-Infinity;var r={computed:-Infinity};return N(e,function(e,i,s){var o=t?t.call(n,e,i,s):e;o>=r.computed&&(r={value:e,computed:o})}),r.value},T.min=function(e,t,n){if(!t&&T.isArray(e)&&e[0]===+e[0]&&e.length<65535)return Math.min.apply(Math,e);if(!t&&T.isEmpty(e))return Infinity;var r={computed:Infinity};return N(e,function(e,i,s){var o=t?t.call(n,e,i,s):e;or||n===void 0)return 1;if(n>>1;n.call(r,e[u])=0})})},T.difference=function(e){var t=a.apply(r,u.call(arguments,1));return T.filter(e,function(e){return!T.contains(t,e)})},T.zip=function(){var e=u.call(arguments),t=T.max(T.pluck(e,"length")),n=new Array(t);for(var r=0;r=0;n--)t=[e[n].apply(this,t)];return t[0]}},T.after=function(e,t){return e<=0?t():function(){if(--e<1)return t.apply(this,arguments)}},T.keys=S||function(e){if(e!==Object(e))throw new TypeError("Invalid object");var t=[];for(var n in e)T.has(e,n)&&(t[t.length]=n);return t},T.values=function(e){var t=[];for(var n in e)T.has(e,n)&&t.push(e[n]);return t},T.pairs=function(e){var t=[];for(var n in e)T.has(e,n)&&t.push([n,e[n]]);return t},T.invert=function(e){var t={};for(var n in e)T.has(e,n)&&(t[e[n]]=n);return t},T.functions=T.methods=function(e){var t=[];for(var n in e)T.isFunction(e[n])&&t.push(n);return t.sort()},T.extend=function(e){return N(u.call(arguments,1),function(t){for(var n in t)e[n]=t[n]}),e},T.pick=function(e){var t={},n=a.apply(r,u.call(arguments,1));return N(n,function(n){n in e&&(t[n]=e[n])}),t},T.omit=function(e){var t={},n=a.apply(r,u.call(arguments,1));for(var i in e)T.contains(n,i)||(t[i]=e[i]);return t},T.defaults=function(e){return N(u.call(arguments,1),function(t){for(var n in t)e[n]==null&&(e[n]=t[n])}),e},T.clone=function(e){return T.isObject(e)?T.isArray(e)?e.slice():T.extend({},e):e},T.tap=function(e,t){return t(e),e};var _=function(e,t,n,r){if(e===t)return e!==0||1/e==1/t;if(e==null||t==null)return e===t;e instanceof T&&(e=e._wrapped),t instanceof T&&(t=t._wrapped);var i=l.call(e);if(i!=l.call(t))return!1;switch(i){case"[object String]":return e==String(t);case"[object Number]":return e!=+e?t!=+t:e==0?1/e==1/t:e==+t;case"[object Date]":case"[object Boolean]":return+e==+t;case"[object RegExp]":return e.source==t.source&&e.global==t.global&&e.multiline==t.multiline&&e.ignoreCase==t.ignoreCase}if(typeof e!="object"||typeof t!="object")return!1;var s=n.length;while(s--)if(n[s]==e)return r[s]==t;n.push(e),r.push(t);var o=0,u=!0;if(i=="[object Array]"){o=e.length,u=o==t.length;if(u)while(o--)if(!(u=_(e[o],t[o],n,r)))break}else{var a=e.constructor,f=t.constructor;if(a!==f&&!(T.isFunction(a)&&a instanceof a&&T.isFunction(f)&&f instanceof f))return!1;for(var c in e)if(T.has(e,c)){o++;if(!(u=T.has(t,c)&&_(e[c],t[c],n,r)))break}if(u){for(c in t)if(T.has(t,c)&&!(o--))break;u=!o}}return n.pop(),r.pop(),u};T.isEqual=function(e,t){return _(e,t,[],[])},T.isEmpty=function(e){if(e==null)return!0;if(T.isArray(e)||T.isString(e))return e.length===0;for(var t in e)if(T.has(e,t))return!1;return!0},T.isElement=function(e){return!!e&&e.nodeType===1},T.isArray=E||function(e){return l.call(e)=="[object Array]"},T.isObject=function(e){return e===Object(e)},N(["Arguments","Function","String","Number","Date","RegExp"],function(e){T["is"+e]=function(t){return l.call(t)=="[object "+e+"]"}}),T.isArguments(arguments)||(T.isArguments=function(e){return!!e&&!!T.has(e,"callee")}),typeof /./!="function"&&(T.isFunction=function(e){return typeof e=="function"}),T.isFinite=function(e){return T.isNumber(e)&&isFinite(e)},T.isNaN=function(e){return T.isNumber(e)&&e!=+e},T.isBoolean=function(e){return e===!0||e===!1||l.call(e)=="[object Boolean]"},T.isNull=function(e){return e===null},T.isUndefined=function(e){return e===void 0},T.has=function(e,t){return c.call(e,t)},T.noConflict=function(){return e._=t,this},T.identity=function(e){return e},T.times=function(e,t,n){for(var r=0;r":">",'"':""","'":"'","/":"/"}};D.unescape=T.invert(D.escape);var P={escape:new RegExp("["+T.keys(D.escape).join("")+"]","g"),unescape:new RegExp("("+T.keys(D.unescape).join("|")+")","g")};T.each(["escape","unescape"],function(e){T[e]=function(t){return t==null?"":(""+t).replace(P[e],function(t){return D[e][t]})}}),T.result=function(e,t){if(e==null)return null;var n=e[t];return T.isFunction(n)?n.call(e):n},T.mixin=function(e){N(T.functions(e),function(t){var n=T[t]=e[t];T.prototype[t]=function(){var e=[this._wrapped];return o.apply(e,arguments),I.call(this,n.apply(T,e))}})};var H=0;T.uniqueId=function(e){var t=H++;return e?e+t:t},T.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var B=/(.)^/,j={"'":"'","\\":"\\","\r":"r","\n":"n"," ":"t","\u2028":"u2028","\u2029":"u2029"},F=/\\|'|\r|\n|\t|\u2028|\u2029/g;T.template=function(e,t,n){n=T.defaults({},n,T.templateSettings);var r=new RegExp([(n.escape||B).source,(n.interpolate||B).source,(n.evaluate||B).source].join("|")+"|$","g"),i=0,s="__p+='";e.replace(r,function(t,n,r,o,u){s+=e.slice(i,u).replace(F,function(e){return"\\"+j[e]}),s+=n?"'+\n((__t=("+n+"))==null?'':_.escape(__t))+\n'":r?"'+\n((__t=("+r+"))==null?'':__t)+\n'":o?"';\n"+o+"\n__p+='":"",i=u+t.length}),s+="';\n",n.variable||(s="with(obj||{}){\n"+s+"}\n"),s="var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};\n"+s+"return __p;\n";try{var o=new Function(n.variable||"obj","_",s)}catch(u){throw u.source=s,u}if(t)return o(t,T);var a=function(e){return o.call(this,e,T)};return a.source="function("+(n.variable||"obj")+"){\n"+s+"}",a},T.chain=function(e){return T(e).chain()};var I=function(e){return this._chain?T(e).chain():e};T.mixin(T),N(["pop","push","reverse","shift","sort","splice","unshift"],function(e){var t=r[e];T.prototype[e]=function(){var n=this._wrapped;return t.apply(n,arguments),(e=="shift"||e=="splice")&&n.length===0&&delete n[0],I.call(this,n)}}),N(["concat","join","slice"],function(e){var t=r[e];T.prototype[e]=function(){return I.call(this,t.apply(this._wrapped,arguments))}}),T.extend(T.prototype,{chain:function(){return this._chain=!0,this},value:function(){return this._wrapped}})}).call(this); \ No newline at end of file diff --git a/vendor/underscore/underscore.js b/vendor/underscore/underscore.js index b4521f5d5..1ac8de132 100644 --- a/vendor/underscore/underscore.js +++ b/vendor/underscore/underscore.js @@ -105,9 +105,12 @@ return results; }; + // Internal data flag for performing `reduceRight`. + var right = null; + // **Reduce** builds up a single result from a list of values, aka `inject`, // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available. - _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context, right) { + _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) { var initial = arguments.length > 2; if (obj == null) obj = []; if (!right && nativeReduce && obj.reduce === nativeReduce) { @@ -133,14 +136,18 @@ // The right-associative version of reduce, also known as `foldr`. // Delegates to **ECMAScript 5**'s native `reduceRight` if available. _.reduceRight = _.foldr = function(obj, iterator, memo, context) { + var initial = arguments.length > 2; if (obj == null) obj = []; if (nativeReduceRight && obj.reduceRight === nativeReduceRight) { if (context) iterator = _.bind(iterator, context); return arguments.length > 2 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator); } - var keys = _.keys(obj).reverse(); - var values = _.toArray(obj).reverse(); - return _.reduce(values, iterator, memo, context, {keys: keys, list: obj}); + var values = _.toArray(obj).reverse(); + if (context && !initial) iterator = _.bind(iterator, context); + right = {keys: _.keys(obj).reverse(), list: obj}; + var result = initial ? _.reduce(values, iterator, memo, context) : _.reduce(values, iterator); + right = null; + return result; }; // Return the first value which passes a truth test. Aliased as `detect`. @@ -206,9 +213,9 @@ return !!result; }; - // Determine if a given value is included in the array or object using `===`. - // Aliased as `contains`. - _.include = _.contains = function(obj, target) { + // Determine if the array or object contains a given value (using `===`). + // Aliased as `include`. + _.contains = _.include = function(obj, target) { var found = false; if (obj == null) return found; if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1; @@ -326,7 +333,7 @@ // to group by, or a function that returns the criterion. _.groupBy = function(obj, value, context) { return group(obj, value, context, function(result, key, value) { - (result[key] || (result[key] = [])).push(value); + (_.has(result, key) ? result[key] : (result[key] = [])).push(value); }); }; @@ -335,20 +342,20 @@ // criterion. _.countBy = function(obj, value, context) { return group(obj, value, context, function(result, key, value) { - result[key] || (result[key] = 0); + if (!_.has(result, key)) result[key] = 0; result[key]++; }); }; // Use a comparator function to figure out the smallest index at which // an object should be inserted so as to maintain order. Uses binary search. - _.sortedIndex = function(array, obj, iterator) { + _.sortedIndex = function(array, obj, iterator, context) { iterator || (iterator = _.identity); - var value = iterator(obj); + var value = iterator.call(context, obj); var low = 0, high = array.length; while (low < high) { - var mid = (low + high) >> 1; - iterator(array[mid]) < value ? low = mid + 1 : high = mid; + var mid = (low + high) >>> 1; + iterator.call(context, array[mid]) < value ? low = mid + 1 : high = mid; } return low; }; @@ -431,12 +438,12 @@ // Produce a duplicate-free version of the array. If the array has already // been sorted, you have the option of using a faster algorithm. // Aliased as `unique`. - _.uniq = _.unique = function(array, isSorted, iterator) { - var initial = iterator ? _.map(array, iterator) : array; + _.uniq = _.unique = function(array, isSorted, iterator, context) { + var initial = iterator ? _.map(array, iterator, context) : array; var results = []; var seen = []; each(initial, function(value, index) { - if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.include(seen, value)) { + if (isSorted ? (!index || seen[seen.length - 1] !== value) : !_.contains(seen, value)) { seen.push(value); results.push(array[index]); } @@ -465,7 +472,7 @@ // Only the elements present in just the first array will remain. _.difference = function(array) { var rest = concat.apply(ArrayProto, slice.call(arguments, 1)); - return _.filter(array, function(value){ return !_.include(rest, value); }); + return _.filter(array, function(value){ return !_.contains(rest, value); }); }; // Zip together multiple lists into a single array -- elements that share @@ -503,21 +510,25 @@ // for **isSorted** to use binary search. _.indexOf = function(array, item, isSorted) { if (array == null) return -1; - var i, l; + var i = 0, l = array.length; if (isSorted) { - i = _.sortedIndex(array, item); - return array[i] === item ? i : -1; + if (typeof isSorted == 'number') { + i = (isSorted < 0 ? Math.max(0, l + isSorted) : isSorted); + } else { + i = _.sortedIndex(array, item); + return array[i] === item ? i : -1; + } } - if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item); - for (i = 0, l = array.length; i < l; i++) if (array[i] === item) return i; + if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item, isSorted); + for (; i < l; i++) if (array[i] === item) return i; return -1; }; // Delegates to **ECMAScript 5**'s native `lastIndexOf` if available. - _.lastIndexOf = function(array, item) { + _.lastIndexOf = function(array, item, fromIndex) { if (array == null) return -1; - if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item); - var i = array.length; + if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item, fromIndex); + var i = (fromIndex != null ? fromIndex : array.length); while (i--) if (array[i] === item) return i; return -1; }; @@ -708,22 +719,23 @@ // Retrieve the values of an object's properties. _.values = function(obj) { - return _.map(obj, _.identity); + var values = []; + for (var key in obj) if (_.has(obj, key)) values.push(obj[key]); + return values; }; // Convert an object into a list of `[key, value]` pairs. _.pairs = function(obj) { - return _.map(obj, function(value, key) { - return [key, value]; - }); + var pairs = []; + for (var key in obj) if (_.has(obj, key)) pairs.push([key, obj[key]]); + return pairs; }; // Invert the keys and values of an object. The values must be serializable. _.invert = function(obj) { - return _.reduce(obj, function(memo, value, key) { - memo[value] = key; - return memo; - }, {}); + var result = {}; + for (var key in obj) if (_.has(obj, key)) result[obj[key]] = key; + return result; }; // Return a sorted list of the function names available on the object. @@ -761,7 +773,7 @@ var copy = {}; var keys = concat.apply(ArrayProto, slice.call(arguments, 1)); for (var key in obj) { - if (!_.include(keys, key)) copy[key] = obj[key]; + if (!_.contains(keys, key)) copy[key] = obj[key]; } return copy; }; @@ -897,7 +909,7 @@ // Is a given value a DOM element? _.isElement = function(obj) { - return !!(obj && obj.nodeType == 1); + return !!(obj && obj.nodeType === 1); }; // Is a given value an array? @@ -926,6 +938,13 @@ }; } + // Optimize `isFunction` if appropriate. + if (typeof (/./) !== 'function') { + _.isFunction = function(obj) { + return typeof obj === 'function'; + }; + } + // Is a given object a finite number? _.isFinite = function(obj) { return _.isNumber(obj) && isFinite(obj);