diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index 9635ce6fa..76f80374b 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -1784,18 +1784,20 @@ } /** - * Creates a function that aggregates a collection, creating an object composed - * of keys generated from the results of running each element of the collection + * Creates a function that aggregates a collection, creating an object or + * array composed from the results of running each element of the collection * through a callback. The given `setter` function sets the keys and values - * of the composed object. + * of the composed object or array. * * @private * @param {Function} setter The setter function. + * @param {boolean} [retArray=false] A flag to indicate that the aggregator + * function should return an array. * @returns {Function} Returns the new aggregator function. */ - function createAggregator(setter) { + function createAggregator(setter, retArray) { return function(collection, callback, thisArg) { - var result = {}; + var result = retArray ? [[], []] : {}; callback = lodash.createCallback(callback, thisArg, 3); if (isArray(collection)) { @@ -4082,6 +4084,54 @@ return result; } + /** + * Creates an array of grouped elements, the first of which contains elements + * the callback returns truey for, the second of which contains elements the + * callback returns falsey for. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index|key, collection). + * + * If a property name is provided for `callback` the created "_.pluck" style + * callback will return the property value of the given element. + * + * If an object is provided for `callback` the created "_.where" style callback + * will return `true` for elements that have the properties of the given object, + * else `false`. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function|Object|string} [callback=identity] The function called + * per iteration. If a property name or object is provided it will be used + * to create a "_.pluck" or "_.where" style callback, respectively. + * @param {*} [thisArg] The `this` binding of `callback`. + * @returns {Array} Returns a new array of grouped elements. + * @example + * + * _.partition([1, 2, 3], function(num) { return num % 2; }); + * // => [[1, 3], [2]] + * + * _.partition([1.2, 2.3, 3.4], function(num) { return this.floor(num) % 2; }, Math); + * // => [[1, 3], [2]] + * + * var characters = [ + * { 'name': 'barney', 'age': 36 }, + * { 'name': 'fred', 'age': 40, 'blocked': true }, + * { 'name': 'pebbles', 'age': 1 } + * ]; + * + * // using "_.where" callback shorthand + * _.map(_.partition(characters, { 'age': 1 }), function(array) { return _.pluck(array, 'name'); }); + * // => [['pebbles'], ['barney', 'fred']] + * + * // using "_.pluck" callback shorthand + * _.map(_.partition(characters, 'blocked'), function(array) { return _.pluck(array, 'name'); }); + * // => [['fred'], ['barney', 'pebbles']] + */ + var partition = createAggregator(function(result, value, key) { + result[key ? 0 : 1].push(value); + }, true); + /** * Retrieves the value of a specified property from all elements in the collection. * @@ -7435,6 +7485,7 @@ lodash.pairs = pairs; lodash.partial = partial; lodash.partialRight = partialRight; + lodash.partition = partition; lodash.pick = pick; lodash.pluck = pluck; lodash.property = property; diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 1a679ac1e..becaa3d24 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -9,58 +9,58 @@ */ (function(){function n(n,t){if(n!==t){if(n>t||typeof n=="undefined")return 1;if(ne||13e||8202r||13r||8202e||13e||8202r||13r||8202=S&&(o=r,e=ce(e));++uo(e,l)&&i.push(l)}return i}function ht(n,t){var r=-1,e=n,u=n?n.length:0; -if(typeof u=="number")for(fe.unindexedChars&&lr(e)&&(e=e.split(""));++r=S,f=[];if(l)var p=ce(),a=r;else p=u?c():f;for(;++oa(p,h))&&((u||l)&&p.push(h),f.push(g)) -}return!l&&u&&s(p),f}function kt(n){return function(t,r,e){var o={};if(r=u.createCallback(r,e,3),de(t)){e=-1;for(var a=t.length;++er&&(r=0),e=[n,t,r,e,u,o],t==j||t==(j|O)?m(e):st(e)) +}for(l=!u,u||(u=c()),o||(o=c()),f=u.length;f--;)if(u[f]==n)return o[f]==t;var h=0,a=true;if(u.push(n),o.push(t),i){if(f=n.length,h=t.length,(a=h==f)||e)for(;h--;)if(i=f,g=t[h],e)for(;i--&&!(a=wt(n[i],g,r,e,u,o)););else if(!(a=wt(n[h],g,r,e,u,o)))break}else se(t,function(t,i,l){return Br.call(l,i)?(h++,a=Br.call(n,i)&&wt(n[i],t,r,e,u,o)):void 0}),a&&!e&&se(n,function(n,t,r){return Br.call(r,t)?a=-1<--h:void 0});return u.pop(),o.pop(),l&&(s(u),s(o)),a}function jt(n,t,r,e,u){(be(t)?ht:dt)(t,function(t,o){var a,i,l=t,f=n[o]; +if(t&&((i=be(t))||_e(t))){for(l=e.length;l--;)if(a=e[l]==t){f=u[l];break}if(!a){var c;r&&(l=r(f,t),c=typeof l!="undefined")&&(f=l),c||(f=i?be(f)?f:[]:_e(f)?f:{}),e.push(t),u.push(f),c||jt(f,t,r,e,u)}}else r&&(l=r(f,t),typeof l=="undefined"&&(l=t)),typeof l!="undefined"&&(f=l);n[o]=f})}function xt(n,t){return n+Fr(ee()*(t-n+1))}function Ct(n,e,u){var o=-1,a=St(),i=n?n.length:0,l=ce&&!e&&a===t&&i>=S,f=[];if(l)var p=ce(),a=r;else p=u?c():f;for(;++oa(p,h))&&((u||l)&&p.push(h),f.push(g)) +}return!l&&u&&s(p),f}function kt(n,t){return function(r,e,o){var a=t?[[],[]]:{};if(e=u.createCallback(e,o,3),be(r)){o=-1;for(var i=r.length;++or&&(r=0),e=[n,t,r,e,u,o],t==j||t==(j|O)?m(e):st(e)) }function Et(n){n.d=Y;var t=br,r="return function("+n.a+"){",e="var r="+n.b+";if(!j(p)){return r}";fe.nonEnumArgs&&(e+="var m=p.length;if(m&&i(p)){l=-1;while(++lu;u++)e+="l='"+n.d[u]+"';if((!(k&&n[l])&&g.call(p,l))",n.e||(e+="||(!n[l]&&p[l]!==q[l])"),e+="){"+n.c+"}"; e+="}"}return t("e,f,g,i,j,q,o,u,v,w",r+(e+"return r;")+"}")(et,Er,Br,Nt,ar,Sr,le,lt,Ar,Rr)}function St(){var n=(n=u.indexOf)===Tt?t:n;return n}function At(n){return typeof n=="function"&&Tr.test($r.call(n))}function It(n){var t,r;return!n||Rr.call(n)!=at||!Br.call(n,"constructor")&&(t=n.constructor,or(t)&&!(t instanceof t))||!fe.argsClass&&Nt(n)||!fe.nodeClass&&p(n)?false:fe.ownLast?(se(n,function(n,t,e){return r=Br.call(e,t),false}),false!==r):(se(n,function(n,t){r=t}),typeof r=="undefined"||Br.call(n,r)) }function Nt(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Rr.call(n)==Z||false}function Rt(n,t,r){var e=0,o=n?n.length:0;if(typeof t!="number"&&null!=t){var a=-1;for(t=u.createCallback(t,r,3);++ae?Zr(0,u+e):e||0;else if(e)return e=Ft(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function Pt(n,t,r){if(typeof t!="number"&&null!=t){var e=0,o=-1,a=n?n.length:0; for(t=u.createCallback(t,r,3);++ot?t=Zr(u+t,0):t>u&&(t=u),typeof r=="undefined"?r=u:0>r?r=Zr(u+r,0):r>u&&(r=u),u=r-t||0,r=yr(u);++e>>1,r(n[e])r?0:r);++t=e)return false;if(typeof n=="string"||!de(n)&&lr(n))return Xr?Xr.call(n,t,r):-1r?Zr(0,e+r):r)||0,-1a&&(a=l)}else t=null==t&&lr(n)?e:u.createCallback(t,r,3),ht(n,function(n,r,e){r=t(n,r,e),r>o&&(o=r,a=n)});return a}function Ht(n,t,r,e){var o=3>arguments.length;if(t=u.createCallback(t,e,4),de(n)){var a=-1,i=n.length;for(o&&i&&(r=n[++a]);++aarguments.length;return t=u.createCallback(t,e,4),vt(n,function(n,e,u){r=o?(o=false,n):t(r,n,e,u)}),r}function Qt(n){var t=-1,r=n?n.length:0,e=yr(typeof r=="number"?r:0);return ht(n,function(n){var r=xt(0,++t);e[t]=e[r],e[r]=n}),e}function Yt(n,t,r){var e;if(t=u.createCallback(t,r,3),de(n)){r=-1;for(var o=n.length;++rarguments.length)return Ot(n,j,null,t); -if(n)var r=n[N]?n[N][2]:n.length,e=qt(arguments,2),r=r-e.length;return Ot(n,j|O,r,t,e)}function nr(n,t,r){var e,u,o,a,i,l,f,c=0,p=false,s=true;if(!or(n))throw new kr;if(t=Zr(0,t)||0,true===r)var g=true,s=false;else ar(r)&&(g=r.leading,p="maxWait"in r&&(Zr(t,r.maxWait)||0),s="trailing"in r?r.trailing:s);var h=function(){var r=t-(Ce()-a);0=y;m?(u&&(u=qr(u)),c=a,o=n.apply(i,e)):u||(u=Kr(v,y))}return m&&l?l=qr(l):l||t===p||(l=Kr(h,t)),r&&(m=true,o=n.apply(i,e)),!m||l||u||(e=i=null),o}}function tr(n,t,r){var e=arguments,u=0,o=e.length,a=typeof r;if("number"!=a&&"string"!=a||!e[3]||e[3][r]!==t||(o=2),3r?0:r);++t=e)return false;if(typeof n=="string"||!be(n)&&lr(n))return Xr?Xr.call(n,t,r):-1r?Zr(0,e+r):r)||0,-1a&&(a=l)}else t=null==t&&lr(n)?e:u.createCallback(t,r,3),ht(n,function(n,r,e){r=t(n,r,e),r>o&&(o=r,a=n)});return a}function Ht(n,t,r,e){var o=3>arguments.length;if(t=u.createCallback(t,e,4),be(n)){var a=-1,i=n.length;for(o&&i&&(r=n[++a]);++aarguments.length;return t=u.createCallback(t,e,4),vt(n,function(n,e,u){r=o?(o=false,n):t(r,n,e,u)}),r}function Qt(n){var t=-1,r=n?n.length:0,e=yr(typeof r=="number"?r:0);return ht(n,function(n){var r=xt(0,++t);e[t]=e[r],e[r]=n}),e}function Yt(n,t,r){var e;if(t=u.createCallback(t,r,3),be(n)){r=-1;for(var o=n.length;++rarguments.length)return Ot(n,j,null,t); +if(n)var r=n[N]?n[N][2]:n.length,e=qt(arguments,2),r=r-e.length;return Ot(n,j|O,r,t,e)}function nr(n,t,r){var e,u,o,a,i,l,f,c=0,p=false,s=true;if(!or(n))throw new kr;if(t=Zr(0,t)||0,true===r)var g=true,s=false;else ar(r)&&(g=r.leading,p="maxWait"in r&&(Zr(t,r.maxWait)||0),s="trailing"in r?r.trailing:s);var h=function(){var r=t-(ke()-a);0=y;m?(u&&(u=qr(u)),c=a,o=n.apply(i,e)):u||(u=Kr(v,y))}return m&&l?l=qr(l):l||t===p||(l=Kr(h,t)),r&&(m=true,o=n.apply(i,e)),!m||l||u||(e=i=null),o}}function tr(n,t,r){var e=arguments,u=0,o=e.length,a=typeof r;if("number"!=a&&"string"!=a||!e[3]||e[3][r]!==t||(o=2),3--n?t.apply(this,arguments):void 0}},u.assign=tr,u.at=function(n,t){var r=arguments,e=-1,u=mt(r,true,false,1),o=u.length,a=typeof t;for("number"!=a&&"string"!=a||!r[2]||r[2][t]!==n||(o=1),fe.unindexedChars&&lr(n)&&(n=n.split("")),r=yr(o);++e--n?t.apply(this,arguments):void 0}},u.assign=tr,u.at=function(n,t){var r=arguments,e=-1,u=mt(r,true,false,1),o=u.length,a=typeof t;for("number"!=a&&"string"!=a||!r[2]||r[2][t]!==n||(o=1),fe.unindexedChars&&lr(n)&&(n=n.split("")),r=yr(o);++earguments.length?Ot(t,j|x,null,n):Ot(t,j|x|O,null,n,qt(arguments,2))},u.chain=function(n){return n=new o(n),n.__chain__=true,n},u.compact=function(n){for(var t=-1,r=n?n.length:0,e=0,u=[];++t=S&&ce(e?n[e]:l)))}var i=n[0],p=-1,g=i?i.length:0,h=[]; -n:for(;++p(v?r(v,f):a(l,f))){for(e=u,(v||l).push(f);--e;)if(v=o[e],0>(v?r(v,f):a(n[e],f)))continue n;h.push(f)}}return s(o),s(l),h},u.invert=function(n,t){for(var r=-1,e=_e(n),u=e.length,o={};++rarguments.length&&de(n))for(;++r=S&&ce(e?n[e]:l)))}var i=n[0],p=-1,g=i?i.length:0,h=[]; +n:for(;++p(v?r(v,f):a(l,f))){for(e=u,(v||l).push(f);--e;)if(v=o[e],0>(v?r(v,f):a(n[e],f)))continue n;h.push(f)}}return s(o),s(l),h},u.invert=function(n,t){for(var r=-1,e=we(n),u=e.length,o={};++rarguments.length&&be(n))for(;++rr?Zr(0,e+r):ne(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},u.mixin=gr,u.noConflict=function(){return n._=Nr,this},u.noop=hr,u.now=Ce,u.parseInt=ke,u.random=function(n,t,r){var e=null==n,u=null==t;return null==r&&(u&&typeof n=="boolean"?(r=n,n=1):typeof t=="boolean"&&(r=t,u=true)),e&&u&&(t=1,u=false),n=+n||0,u?(t=n,n=0):t=+t||0,r||n%1||t%1?(r=ee(),ne(n+r*(t-n+parseFloat("1e-"+((r+"").length-1))),t)):xt(n,t) -},u.reduce=Ht,u.reduceRight=Jt,u.result=function(n,t,r){var e=null==n?w:n[t];return typeof e=="undefined"?r:or(e)?n[t]():e},u.runInContext=_,u.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:_e(n).length},u.some=Yt,u.sortedIndex=Ft,u.template=function(n,t,r){var e=u.templateSettings;n=Cr(n||""),r=rr({},r,e);var o,a=rr({},r.imports,e.imports),e=_e(a),a=fr(a),i=0,l=r.interpolate||U,c="__p+='",l=xr((r.escape||U).source+"|"+l.source+"|"+(l===D?W:U).source+"|"+(r.evaluate||U).source+"|$","g"); +},u.isNumber=ir,u.isObject=ar,u.isPlainObject=_e,u.isRegExp=function(n){var t=typeof n;return n&&("function"==t||"object"==t)&&Rr.call(n)==it||false},u.isString=lr,u.isUndefined=function(n){return typeof n=="undefined"},u.lastIndexOf=function(n,t,r){var e=n?n.length:0;for(typeof r=="number"&&(e=(0>r?Zr(0,e+r):ne(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},u.mixin=gr,u.noConflict=function(){return n._=Nr,this},u.noop=hr,u.now=ke,u.parseInt=Oe,u.random=function(n,t,r){var e=null==n,u=null==t;return null==r&&(u&&typeof n=="boolean"?(r=n,n=1):typeof t=="boolean"&&(r=t,u=true)),e&&u&&(t=1,u=false),n=+n||0,u?(t=n,n=0):t=+t||0,r||n%1||t%1?(r=ee(),ne(n+r*(t-n+parseFloat("1e-"+((r+"").length-1))),t)):xt(n,t) +},u.reduce=Ht,u.reduceRight=Jt,u.result=function(n,t,r){var e=null==n?w:n[t];return typeof e=="undefined"?r:or(e)?n[t]():e},u.runInContext=_,u.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:we(n).length},u.some=Yt,u.sortedIndex=Ft,u.template=function(n,t,r){var e=u.templateSettings;n=Cr(n||""),r=rr({},r,e);var o,a=rr({},r.imports,e.imports),e=we(a),a=fr(a),i=0,l=r.interpolate||U,c="__p+='",l=xr((r.escape||U).source+"|"+l.source+"|"+(l===D?W:U).source+"|"+(r.evaluate||U).source+"|$","g"); n.replace(l,function(t,r,e,u,a,l){return e||(e=u),c+=n.slice(i,l).replace(X,f),r&&(c+="'+__e("+r+")+'"),a&&(o=true,c+="';"+a+";\n__p+='"),e&&(c+="'+((__t=("+e+"))==null?'':__t)+'"),i=l+t.length,t}),c+="';",l=r=r.variable,l||(r="obj",c="with("+r+"){"+c+"}"),c=(o?c.replace(T,""):c).replace(P,"$1").replace(q,"$1;"),c="function("+r+"){"+(l?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+c+"return __p}";try{var p=br(e,"return "+c).apply(w,a) -}catch(s){throw s.source=c,s}return t?p(t):(p.source=c,p)},u.trim=we,u.trimLeft=je,u.trimRight=xe,u.unescape=function(n){return null==n?"":(n=Cr(n),0>n.indexOf(";")?n:n.replace(F,b))},u.uniqueId=function(n){var t=++R;return Cr(null==n?"":n)+t},u.all=zt,u.any=Yt,u.detect=Mt,u.findWhere=Mt,u.foldl=Ht,u.foldr=Jt,u.include=Wt,u.inject=Ht,gr(function(){var n={};return dt(u,function(t,r){u.prototype[r]||(n[r]=t)}),n}(),false),u.first=Rt,u.last=function(n,t,r){var e=0,o=n?n.length:0;if(typeof t!="number"&&null!=t){var a=o; +}catch(s){throw s.source=c,s}return t?p(t):(p.source=c,p)},u.trim=je,u.trimLeft=xe,u.trimRight=Ce,u.unescape=function(n){return null==n?"":(n=Cr(n),0>n.indexOf(";")?n:n.replace(F,b))},u.uniqueId=function(n){var t=++R;return Cr(null==n?"":n)+t},u.all=zt,u.any=Yt,u.detect=Mt,u.findWhere=Mt,u.foldl=Ht,u.foldr=Jt,u.include=Wt,u.inject=Ht,gr(function(){var n={};return dt(u,function(t,r){u.prototype[r]||(n[r]=t)}),n}(),false),u.first=Rt,u.last=function(n,t,r){var e=0,o=n?n.length:0;if(typeof t!="number"&&null!=t){var a=o; for(t=u.createCallback(t,r,3);a--&&t(n[a],a,n);)e++}else if(e=t,null==e||r)return n?n[o-1]:w;return e=o-e,qt(n,0"']/g,L=/<%-([\s\S]+?)%>/g,B=/<%([\s\S]+?)%>/g,D=/<%=([\s\S]+?)%>/g,W=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,z=/\w*$/,K=/^\s*function[ \n\r\t]+\w/,M=/^0[xX]/,U=/($^)/,V=/\bthis\b/,X=/['\n\r\t\u2028\u2029\\]/g,G=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",H=[],J=[],Q="Array Boolean Date Error Function Math Number Object RegExp Set String _ clearTimeout document isFinite isNaN parseInt setTimeout TypeError window WinRTError".split(" "),Y="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),Z="[object Arguments]",nt="[object Array]",tt="[object Boolean]",rt="[object Date]",et="[object Error]",ut="[object Function]",ot="[object Number]",at="[object Object]",it="[object RegExp]",lt="[object String]",ft={}; diff --git a/dist/lodash.js b/dist/lodash.js index 74dfeb62a..fb0b199d1 100644 --- a/dist/lodash.js +++ b/dist/lodash.js @@ -1556,18 +1556,20 @@ } /** - * Creates a function that aggregates a collection, creating an object composed - * of keys generated from the results of running each element of the collection + * Creates a function that aggregates a collection, creating an object or + * array composed from the results of running each element of the collection * through a callback. The given `setter` function sets the keys and values - * of the composed object. + * of the composed object or array. * * @private * @param {Function} setter The setter function. + * @param {boolean} [retArray=false] A flag to indicate that the aggregator + * function should return an array. * @returns {Function} Returns the new aggregator function. */ - function createAggregator(setter) { + function createAggregator(setter, retArray) { return function(collection, callback, thisArg) { - var result = {}; + var result = retArray ? [[], []] : {}; callback = lodash.createCallback(callback, thisArg, 3); var index = -1, @@ -3820,6 +3822,54 @@ return result; } + /** + * Creates an array of grouped elements, the first of which contains elements + * the callback returns truey for, the second of which contains elements the + * callback returns falsey for. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index|key, collection). + * + * If a property name is provided for `callback` the created "_.pluck" style + * callback will return the property value of the given element. + * + * If an object is provided for `callback` the created "_.where" style callback + * will return `true` for elements that have the properties of the given object, + * else `false`. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array|Object|string} collection The collection to iterate over. + * @param {Function|Object|string} [callback=identity] The function called + * per iteration. If a property name or object is provided it will be used + * to create a "_.pluck" or "_.where" style callback, respectively. + * @param {*} [thisArg] The `this` binding of `callback`. + * @returns {Array} Returns a new array of grouped elements. + * @example + * + * _.partition([1, 2, 3], function(num) { return num % 2; }); + * // => [[1, 3], [2]] + * + * _.partition([1.2, 2.3, 3.4], function(num) { return this.floor(num) % 2; }, Math); + * // => [[1, 3], [2]] + * + * var characters = [ + * { 'name': 'barney', 'age': 36 }, + * { 'name': 'fred', 'age': 40, 'blocked': true }, + * { 'name': 'pebbles', 'age': 1 } + * ]; + * + * // using "_.where" callback shorthand + * _.map(_.partition(characters, { 'age': 1 }), function(array) { return _.pluck(array, 'name'); }); + * // => [['pebbles'], ['barney', 'fred']] + * + * // using "_.pluck" callback shorthand + * _.map(_.partition(characters, 'blocked'), function(array) { return _.pluck(array, 'name'); }); + * // => [['fred'], ['barney', 'pebbles']] + */ + var partition = createAggregator(function(result, value, key) { + result[key ? 0 : 1].push(value); + }, true); + /** * Retrieves the value of a specified property from all elements in the collection. * @@ -7156,6 +7206,7 @@ lodash.pairs = pairs; lodash.partial = partial; lodash.partialRight = partialRight; + lodash.partition = partition; lodash.pick = pick; lodash.pluck = pluck; lodash.property = property; diff --git a/dist/lodash.min.js b/dist/lodash.min.js index 286e1efde..7075038da 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -10,54 +10,54 @@ (function(){function n(n,t){if(n!==t){if(n>t||typeof n=="undefined")return 1;if(ne||13e||8202r||13r||8202=A&&(o=r,e=ie(e));++uo(e,f)&&a.push(f)}return a}function ht(n,t){var r=-1,e=n?n.length:0;if(typeof e=="number")for(;++r=A&&(o=r,e=ie(e));++uo(e,f)&&a.push(f)}return a}function ht(n,t){var r=-1,e=n?n.length:0;if(typeof e=="number")for(;++r=A,l=[];if(f)var s=ie(),i=r;else s=u?c():l;for(;++oi(s,h))&&((u||f)&&s.push(h),l.push(g)) -}return!f&&u&&p(s),l}function xt(n){return function(t,r,e){var u={};r=y.createCallback(r,e,3),e=-1;var o=t?t.length:0;if(typeof o=="number")for(;++er&&(r=0),e=[n,t,r,e,u,o],t==w||t==(w|C)?S(e):pt(e)) +}for(l=!u,u||(u=c()),i||(i=c()),s=u.length;s--;)if(u[s]==n)return i[s]==t;var h=0,a=true;if(u.push(n),i.push(t),f){if(s=n.length,h=t.length,(a=h==s)||e)for(;h--;)if(f=s,g=t[h],e)for(;f--&&!(a=_t(n[f],g,r,e,u,i)););else if(!(a=_t(n[h],g,r,e,u,i)))break}else o(t,function(t,o,f){return Br.call(f,o)?(h++,a=Br.call(n,o)&&_t(n[o],t,r,e,u,i)):void 0}),a&&!e&&o(n,function(n,t,r){return Br.call(r,t)?a=-1<--h:void 0});return u.pop(),i.pop(),l&&(p(u),p(i)),a}function wt(n,t,r,e,u){(ge(t)?ht:bt)(t,function(t,o){var i,a,f=t,l=n[o]; +if(t&&((a=ge(t))||he(t))){for(f=e.length;f--;)if(i=e[f]==t){l=u[f];break}if(!i){var c;r&&(f=r(l,t),c=typeof f!="undefined")&&(l=f),c||(l=a?ge(l)?l:[]:he(l)?l:{}),e.push(t),u.push(l),c||wt(l,t,r,e,u)}}else r&&(f=r(l,t),typeof f=="undefined"&&(f=t)),typeof f!="undefined"&&(l=f);n[o]=l})}function kt(n,t){return n+Tr(Zr()*(t-n+1))}function jt(n,e,u){var o=-1,i=Ot(),a=n?n.length:0,f=ie&&!e&&i===t&&a>=A,l=[];if(f)var s=ie(),i=r;else s=u?c():l;for(;++oi(s,h))&&((u||f)&&s.push(h),l.push(g)) +}return!f&&u&&p(s),l}function xt(n,t){return function(r,e,u){var o=t?[[],[]]:{};e=y.createCallback(e,u,3),u=-1;var i=r?r.length:0;if(typeof i=="number")for(;++ur&&(r=0),e=[n,t,r,e,u,o],t==w||t==(w|C)?S(e):pt(e)) }function Ot(){var n=(n=y.indexOf)===Rt?t:n;return n}function At(n){return typeof n=="function"&&Nr.test(Fr.call(n))}function St(n){var t,r;return n&&Er.call(n)==et&&(Br.call(n,"constructor")||(t=n.constructor,!er(t)||t instanceof t))?(o(n,function(n,t){r=t}),typeof r=="undefined"||Br.call(n,r)):false}function Et(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Er.call(n)==Q||false}function Nt(n,t,r){var e=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=y.createCallback(t,r,3);++oe?Hr(0,u+e):e||0;else if(e)return e=Ft(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function It(n,t,r){if(typeof t!="number"&&null!=t){var e=0,u=-1,o=n?n.length:0;for(t=y.createCallback(t,r,3);++ut?t=Hr(u+t,0):t>u&&(t=u),typeof r=="undefined"?r=u:0>r?r=Hr(u+r,0):r>u&&(r=u),u=r-t||0,r=hr(u);++e>>1,r(n[e])r?0:r);++t=e)return false;if(typeof n=="string"||!se(n)&&ir(n))return Kr?Kr.call(n,t,r):-1r?Hr(0,e+r):r)||0,-1>>1,r(n[e])r?0:r);++t=e)return false;if(typeof n=="string"||!ge(n)&&ir(n))return Kr?Kr.call(n,t,r):-1r?Hr(0,e+r):r)||0,-1o&&(o=a)}else t=null==t&&ir(n)?e:y.createCallback(t,r,3),ht(n,function(n,r,e){r=t(n,r,e),r>u&&(u=r,o=n)});return o}function Xt(n,t,r,e){var u=3>arguments.length;t=y.createCallback(t,e,4);var o=-1,i=n?n.length:0;if(typeof i=="number")for(u&&i&&(r=n[++o]);++oarguments.length;return t=y.createCallback(t,e,4),vt(n,function(n,e,o){r=u?(u=false,n):t(r,n,e,o)}),r}function Ht(n){var t=-1,r=n?n.length:0,e=hr(typeof r=="number"?r:0); -return ht(n,function(n){var r=kt(0,++t);e[t]=e[r],e[r]=n}),e}function Jt(n,t,r){var e;t=y.createCallback(t,r,3),r=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++rarguments.length)return Ct(n,w,null,t);if(n)var r=n[N]?n[N][2]:n.length,e=Tt(arguments,2),r=r-e.length;return Ct(n,w|C,r,t,e)}function Yt(n,t,r){function e(){c&&Ir(c),i=c=p=_,(h||g!==t)&&(s=be(),a=n.apply(l,o),c||i||(o=l=null)) -}function u(){var r=t-(be()-f);0=y;m?(i&&(i=Ir(i)),s=f,a=n.apply(l,o)):i||(i=Wr(e,y))}return m&&c?c=Ir(c):c||t===g||(c=Wr(u,t)),r&&(m=true,a=n.apply(l,o)),!m||c||i||(o=l=null),a -}}function Zt(n,t,r){var e=arguments,u=0,o=e.length,i=typeof r;if("number"!=i&&"string"!=i||!e[3]||e[3][r]!==t||(o=2),3arguments.length)return Ct(n,w,null,t);if(n)var r=n[N]?n[N][2]:n.length,e=Tt(arguments,2),r=r-e.length;return Ct(n,w|C,r,t,e)}function Yt(n,t,r){function e(){c&&Ir(c),i=c=p=_,(h||g!==t)&&(s=de(),a=n.apply(l,o),c||i||(o=l=null)) +}function u(){var r=t-(de()-f);0=y;m?(i&&(i=Ir(i)),s=f,a=n.apply(l,o)):i||(i=Wr(e,y))}return m&&c?c=Ir(c):c||t===g||(c=Wr(u,t)),r&&(m=true,a=n.apply(l,o)),!m||c||i||(o=l=null),a +}}function Zt(n,t,r){var e=arguments,u=0,o=e.length,i=typeof r;if("number"!=i&&"string"!=i||!e[3]||e[3][r]!==t||(o=2),3--n?t.apply(this,arguments):void 0}},y.assign=Zt,y.at=function(n,t){var r=arguments,e=-1,u=mt(r,true,false,1),o=u.length,i=typeof t;for("number"!=i&&"string"!=i||!r[2]||r[2][t]!==n||(o=1),r=hr(o);++e--n?t.apply(this,arguments):void 0}},y.assign=Zt,y.at=function(n,t){var r=arguments,e=-1,u=mt(r,true,false,1),o=u.length,i=typeof t;for("number"!=i&&"string"!=i||!r[2]||r[2][t]!==n||(o=1),r=hr(o);++earguments.length?Ct(t,w|k,null,n):Ct(t,w|k|C,null,n,Tt(arguments,2))},y.chain=function(n){return n=new m(n),n.__chain__=true,n},y.compact=function(n){for(var t=-1,r=n?n.length:0,e=0,u=[];++t=A&&ie(e?n[e]:f)))}var a=n[0],s=-1,g=a?a.length:0,h=[]; -n:for(;++s(v?r(v,l):i(f,l))){for(e=u,(v||f).push(l);--e;)if(v=o[e],0>(v?r(v,l):i(n[e],l)))continue n;h.push(l)}}return p(o),p(f),h},y.invert=function(n,t){for(var r=-1,e=he(n),u=e.length,o={};++rarguments.length&&typeof u=="number")for(;++r=A&&ie(e?n[e]:f)))}var a=n[0],s=-1,g=a?a.length:0,h=[]; +n:for(;++s(v?r(v,l):i(f,l))){for(e=u,(v||f).push(l);--e;)if(v=o[e],0>(v?r(v,l):i(n[e],l)))continue n;h.push(l)}}return p(o),p(f),h},y.invert=function(n,t){for(var r=-1,e=ve(n),u=e.length,o={};++rarguments.length&&typeof u=="number")for(;++rr?Hr(0,e+r):Jr(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},y.mixin=pr,y.noConflict=function(){return n._=Sr,this},y.noop=sr,y.now=be,y.parseInt=de,y.random=function(n,t,r){var e=null==n,u=null==t;return null==r&&(u&&typeof n=="boolean"?(r=n,n=1):typeof t=="boolean"&&(r=t,u=true)),e&&u&&(t=1,u=false),n=+n||0,u?(t=n,n=0):t=+t||0,r||n%1||t%1?(r=Zr(),Jr(n+r*(t-n+parseFloat("1e-"+((r+"").length-1))),t)):kt(n,t) -},y.reduce=Xt,y.reduceRight=Gt,y.result=function(n,t,r){var e=null==n?_:n[t];return typeof e=="undefined"?r:er(e)?n[t]():e},y.runInContext=d,y.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:he(n).length},y.some=Jt,y.sortedIndex=Ft,y.template=function(n,t,r){var e=y.templateSettings;n=kr(n||""),r=nr({},r,e);var u,o=nr({},r.imports,e.imports),e=he(o),o=ar(o),i=0,a=r.interpolate||M,f="__p+='",a=wr((r.escape||M).source+"|"+a.source+"|"+(a===W?z:M).source+"|"+(r.evaluate||M).source+"|$","g"); +}),e},y.findLastIndex=function(n,t,r){var e=n?n.length:0;for(t=y.createCallback(t,r,3);e--;)if(t(n[e],e,n))return e;return-1},y.findLastKey=function(n,t,r){var e;return t=y.createCallback(t,r,3),dt(n,function(n,r,u){return t(n,r,u)?(e=r,false):void 0}),e},y.has=function(n,t){return n?Br.call(n,t):false},y.identity=lr,y.indexOf=Rt,y.isArguments=Et,y.isArray=ge,y.isBoolean=function(n){return true===n||false===n||n&&typeof n=="object"&&Er.call(n)==Z||false},y.isDate=function(n){return n&&typeof n=="object"&&Er.call(n)==nt||false +},y.isElement=rr,y.isEmpty=function(n){var t=true;if(!n)return t;var r=Er.call(n),e=n.length;return r==Y||r==ot||r==Q||r==et&&typeof e=="number"&&er(n.splice)?!e:(bt(n,function(){return t=false}),t)},y.isEqual=function(n,t,r,e){return _t(n,t,typeof r=="function"&&ct(r,e,2))},y.isFinite=function(n){return Vr(n)&&!Xr(parseFloat(n))},y.isFunction=er,y.isNaN=function(n){return or(n)&&n!=+n},y.isNull=function(n){return null===n},y.isNumber=or,y.isObject=ur,y.isPlainObject=he,y.isRegExp=function(n){return n&&typeof n=="object"&&Er.call(n)==ut||false +},y.isString=ir,y.isUndefined=function(n){return typeof n=="undefined"},y.lastIndexOf=function(n,t,r){var e=n?n.length:0;for(typeof r=="number"&&(e=(0>r?Hr(0,e+r):Jr(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},y.mixin=pr,y.noConflict=function(){return n._=Sr,this},y.noop=sr,y.now=de,y.parseInt=_e,y.random=function(n,t,r){var e=null==n,u=null==t;return null==r&&(u&&typeof n=="boolean"?(r=n,n=1):typeof t=="boolean"&&(r=t,u=true)),e&&u&&(t=1,u=false),n=+n||0,u?(t=n,n=0):t=+t||0,r||n%1||t%1?(r=Zr(),Jr(n+r*(t-n+parseFloat("1e-"+((r+"").length-1))),t)):kt(n,t) +},y.reduce=Xt,y.reduceRight=Gt,y.result=function(n,t,r){var e=null==n?_:n[t];return typeof e=="undefined"?r:er(e)?n[t]():e},y.runInContext=d,y.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:ve(n).length},y.some=Jt,y.sortedIndex=Ft,y.template=function(n,t,r){var e=y.templateSettings;n=kr(n||""),r=nr({},r,e);var u,o=nr({},r.imports,e.imports),e=ve(o),o=ar(o),i=0,a=r.interpolate||M,f="__p+='",a=wr((r.escape||M).source+"|"+a.source+"|"+(a===W?z:M).source+"|"+(r.evaluate||M).source+"|$","g"); n.replace(a,function(t,r,e,o,a,c){return e||(e=o),f+=n.slice(i,c).replace(V,l),r&&(f+="'+__e("+r+")+'"),a&&(u=true,f+="';"+a+";\n__p+='"),e&&(f+="'+((__t=("+e+"))==null?'':__t)+'"),i=c+t.length,t}),f+="';",a=r=r.variable,a||(r="obj",f="with("+r+"){"+f+"}"),f=(u?f.replace(I,""):f).replace(T,"$1").replace(F,"$1;"),f="function("+r+"){"+(a?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+f+"return __p}";try{var c=mr(e,"return "+f).apply(_,o) -}catch(p){throw p.source=f,p}return t?c(t):(c.source=f,c)},y.trim=ve,y.trimLeft=ye,y.trimRight=me,y.unescape=function(n){return null==n?"":(n=kr(n),0>n.indexOf(";")?n:n.replace($,b))},y.uniqueId=function(n){var t=++R;return kr(null==n?"":n)+t},y.all=zt,y.any=Jt,y.detect=Pt,y.findWhere=Pt,y.foldl=Xt,y.foldr=Gt,y.include=Wt,y.inject=Xt,pr(function(){var n={};return bt(y,function(t,r){y.prototype[r]||(n[r]=t)}),n}(),false),y.first=Nt,y.last=function(n,t,r){var e=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=u; +}catch(p){throw p.source=f,p}return t?c(t):(c.source=f,c)},y.trim=ye,y.trimLeft=me,y.trimRight=be,y.unescape=function(n){return null==n?"":(n=kr(n),0>n.indexOf(";")?n:n.replace($,b))},y.uniqueId=function(n){var t=++R;return kr(null==n?"":n)+t},y.all=zt,y.any=Jt,y.detect=Pt,y.findWhere=Pt,y.foldl=Xt,y.foldr=Gt,y.include=Wt,y.inject=Xt,pr(function(){var n={};return bt(y,function(t,r){y.prototype[r]||(n[r]=t)}),n}(),false),y.first=Nt,y.last=function(n,t,r){var e=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=u; for(t=y.createCallback(t,r,3);o--&&t(n[o],o,n);)e++}else if(e=t,null==e||r)return n?n[u-1]:_;return e=u-e,Tt(n,0"']/g,D=/<%-([\s\S]+?)%>/g,q=/<%([\s\S]+?)%>/g,W=/<%=([\s\S]+?)%>/g,z=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,L=/\w*$/,P=/^\s*function[ \n\r\t]+\w/,K=/^0[xX]/,M=/($^)/,U=/\bthis\b/,V=/['\n\r\t\u2028\u2029\\]/g,X=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",G=[],H=[],J="Array Boolean Date Function Math Number Object RegExp Set String _ clearTimeout document isFinite isNaN parseInt setTimeout TypeError window WinRTError".split(" "),Q="[object Arguments]",Y="[object Array]",Z="[object Boolean]",nt="[object Date]",tt="[object Function]",rt="[object Number]",et="[object Object]",ut="[object RegExp]",ot="[object String]",it={}; diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 695de3c16..81d8d17fe 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -900,18 +900,20 @@ } /** - * Creates a function that aggregates a collection, creating an object composed - * of keys generated from the results of running each element of the collection + * Creates a function that aggregates a collection, creating an object or + * array composed from the results of running each element of the collection * through a callback. The given `setter` function sets the keys and values - * of the composed object. + * of the composed object or array. * * @private * @param {Function} setter The setter function. + * @param {boolean} [retArray=false] A flag to indicate that the aggregator + * function should return an array. * @returns {Function} Returns the new aggregator function. */ - function createAggregator(setter) { + function createAggregator(setter, retArray) { return function(collection, callback, thisArg) { - var result = {}; + var result = retArray ? [[], []] : {}; callback = createCallback(callback, thisArg, 3); var index = -1, diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 5c943ed29..5544cb9d8 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -3,39 +3,38 @@ * Lo-Dash 2.4.1 (Custom Build) lodash.com/license | Underscore.js 1.5.2 underscorejs.org/LICENSE * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js` */ -;(function(){function n(n,r,t){t=(t||0)-1;for(var e=n?n.length:0;++te||typeof t=="undefined"){t=1;break n}if(te(r,i)&&o.push(i)}return o}function s(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(;++tu(f,l))&&(t&&f.push(l),i.push(a))}return i}function m(n){return function(r,t,e){var u={};t=X(t,e,3),e=-1;var o=r?r.length:0;if(typeof o=="number")for(;++et&&(t=0),n=[n,r,t,e,u,o],r==er||r==(er|fr)?f(n):c(n)}function _(){var r=(r=o.indexOf)===x?n:r;return r}function d(n){return typeof n=="function"&&Mr.test(zr.call(n))}function w(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ir.call(n)==yr||false}function j(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=X(r,t,3);++oe?Qr(0,u+e):e||0;else if(e)return e=E(r,t),u&&r[e]===t?e:-1;return n(r,t,e)}function T(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=X(r,t,3);++ur?r=Qr(u+r,0):r>u&&(r=u),typeof t=="undefined"?t=u:0>t?t=Qr(u+t,0):t>u&&(t=u),u=t-r||0,t=Array(u);++e>>1,t(n[e])u&&(u=t);else r=X(r,t,3),s(n,function(n,t,o){t=r(n,t,o),t>e&&(e=t,u=n)});return u}function $(n,r,t,e){var u=3>arguments.length;r=X(r,e,4);var o=-1,i=n?n.length:0; -if(typeof i=="number")for(u&&i&&(t=n[++o]);++oarguments.length;return r=X(r,e,4),g(n,function(n,e,o){t=u?(u=false,n):r(t,n,e,o)}),t}function M(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return s(n,function(n){var t;t=++r,t=0+Wr(Zr()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function D(n,r,t){var e;r=X(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++targuments.length)return b(n,er,null,r);if(n)var t=n[lr]?n[lr][2]:n.length,e=A(arguments,2),t=t-e.length;return b(n,er|fr,t,r,e)}function C(n,r,t){var e,u,o,i,f,a,l,c=0,p=false,s=true;if(!H(n))throw new TypeError;if(r=Qr(0,r)||0,true===t)var g=true,s=false;else J(t)&&(g=t.leading,p="maxWait"in t&&(Qr(r,t.maxWait)||0),s="trailing"in t?t.trailing:s);var v=function(){var t=r-(lt()-i); -0=y;m?(u&&(u=clearTimeout(u)),c=i,o=n.apply(f,e)):u||(u=setTimeout(h,y))}return m&&a?a=clearTimeout(a):a||r===p||(a=setTimeout(v,r)),t&&(m=true,o=n.apply(f,e)),!m||a||u||(e=f=null),o}}function P(n,r,t){if(!n)return n; -var e=arguments,u=0,o=e.length,i=typeof t;for("number"!=i&&"string"!=i||!e[3]||e[3][t]!==r||(o=2);++u"']/g,vr=/($^)/,hr=/['\n\r\t\u2028\u2029\\]/g,yr="[object Arguments]",mr="[object Array]",br="[object Boolean]",_r="[object Date]",dr="[object Number]",wr="[object Object]",jr="[object RegExp]",xr="[object String]",Tr={"&":"&","<":"<",">":">",'"':""","'":"'"},Ar={"&":"&","<":"<",">":">",""":'"',"'":"'"},Er={"function":true,object:true},Or={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},Sr=Er[typeof window]&&window||this,kr=Er[typeof exports]&&exports&&!exports.nodeType&&exports,Nr=Er[typeof module]&&module&&!module.nodeType&&module,qr=kr&&Nr&&typeof global=="object"&&global; -!qr||qr.global!==qr&&qr.window!==qr&&qr.self!==qr||(Sr=qr);var Fr=Nr&&Nr.exports===kr&&kr,Br=Array.prototype,Rr=Object.prototype,$r=Sr._,Ir=Rr.toString,Mr=RegExp("^"+(Ir+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),Dr=Math.ceil,Wr=Math.floor,zr=Function.prototype.toString,Cr=Rr.hasOwnProperty,Pr=Br.push,Ur=Rr.propertyIsEnumerable,Vr=Br.splice,Gr=d(Gr=Object.create)&&Gr,Hr=d(Hr=Array.isArray)&&Hr,Jr=Sr.isFinite,Kr=Sr.isNaN,Lr=d(Lr=Object.keys)&&Lr,Qr=Math.max,Xr=Math.min,Yr=d(Yr=Date.now)&&Yr,Zr=Math.random; -i.prototype=o.prototype;var nt={};!function(){var n={0:1,length:1};nt.spliceObjects=(Vr.call(n,0,1),!n[0])}(1),o.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Gr||(a=function(){function n(){}return function(r){if(J(r)){n.prototype=r;var t=new n;n.prototype=null}return t||Sr.Object()}}()),w(arguments)||(w=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Cr.call(n,"callee")&&!Ur.call(n,"callee")||false});var rt=function(n,r){if(!J(n))return n; -for(var t in n)if(r(n[t],t,n)===cr)break;return n},tt=function(n){var r=[];if(!J(n))return r;for(var t in n)Cr.call(n,t)&&r.push(t);return r},et=m(function(n,r,t){Cr.call(n,t)?n[t]++:n[t]=1}),ut=m(function(n,r,t){Cr.call(n,t)?n[t].push(r):n[t]=[r]}),ot=m(function(n,r,t){n[t]=r}),it=B,ft=Hr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ir.call(n)==mr||false};H(/x/)&&(H=function(n){return typeof n=="function"&&"[object Function]"==Ir.call(n)});var at=Lr?function(n){return J(n)?Lr(n):[] -}:tt,lt=Yr||function(){return(new Date).getTime()};o.after=function(n,r){if(!H(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},o.bind=z,o.bindAll=function(n){for(var r=1i(a,e)){for(r=t;--r;)if(0>i(n[r],e))continue n;a.push(e)}return a},o.invert=function(n){for(var r=-1,t=at(n),e=t.length,u={};++rarguments.length&&typeof u=="number")for(;++tr?0:r);++nt?Qr(0,e+t):Xr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},o.mixin=nr,o.noConflict=function(){return Sr._=$r,this},o.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0,n+Wr(Zr()*(r-n+1))},o.reduce=$,o.reduceRight=I,o.result=function(n,r){if(null!=n){var t=n[r]; -return H(t)?n[r]():t}},o.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:at(n).length},o.some=D,o.sortedIndex=E,o.template=function(n,r,t){var u=o,i=u.templateSettings;n=(n||"")+"",t=U({},t,i);var f=0,a="__p+='",i=t.variable;n.replace(RegExp((t.escape||vr).source+"|"+(t.interpolate||vr).source+"|"+(t.evaluate||vr).source+"|$","g"),function(r,t,u,o,i){return a+=n.slice(f,i).replace(hr,e),t&&(a+="'+_.escape("+t+")+'"),o&&(a+="';"+o+";\n__p+='"),u&&(a+="'+((__t=("+u+"))==null?'':__t)+'"),f=i+r.length,r -}),a+="';",i||(i="obj",a="with("+i+"||{}){"+a+"}"),a="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}";try{var l=Function("_","return "+a)(u)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},o.unescape=function(n){return null==n?"":(n+="",0>n.indexOf(";")?n:n.replace(sr,u))},o.uniqueId=function(n){var r=++pr+"";return n?n+r:r},o.all=k,o.any=D,o.detect=q,o.findWhere=function(n,r){return W(n,r,true)},o.foldl=$,o.foldr=I,o.include=S,o.inject=$,o.first=j,o.last=function(n,r,t){var e=0,u=n?n.length:0; -if(typeof r!="number"&&null!=r){var o=u;for(r=X(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:tr;return e=u-e,A(n,0e||typeof t=="undefined"){t=1;break n}if(te(r,i)&&o.push(i)}return o}function v(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(;++tu(f,l))&&(t&&f.push(l),i.push(a))}return i}function _(n){return function(r,t,e){var u={};t=Z(t,e,3),e=-1;var o=r?r.length:0;if(typeof o=="number")for(;++et&&(t=0),n=[n,r,t,e,u,o],r==or||r==(or|lr)?l(n):s(n)}function w(){var n=(n=f.indexOf)===A?t:n;return n}function j(n){return typeof n=="function"&&Wr.test(Pr.call(n))}function x(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Dr.call(n)==br||false}function T(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=Z(r,t,3);++oe?Yr(0,u+e):e||0;else if(e)return e=k(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function E(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=Z(r,t,3);++ur?r=Yr(u+r,0):r>u&&(r=u),typeof t=="undefined"?t=u:0>t?t=Yr(u+t,0):t>u&&(t=u),u=t-r||0,t=Array(u);++e>>1,t(n[e])u&&(u=t);else r=Z(r,t,3),v(n,function(n,t,o){t=r(n,t,o),t>e&&(e=t,u=n)});return u}function M(n,r,t,e){var u=3>arguments.length;r=Z(r,e,4);var o=-1,i=n?n.length:0; +if(typeof i=="number")for(u&&i&&(t=n[++o]);++oarguments.length;return r=Z(r,e,4),h(n,function(n,e,o){t=u?(u=false,n):r(t,n,e,o)}),t}function W(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return v(n,function(n){var t;t=++r,t=0+Cr(rt()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function z(n,r,t){var e;r=Z(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++targuments.length)return d(n,or,null,r);if(n)var t=n[pr]?n[pr][2]:n.length,e=O(arguments,2),t=t-e.length;return d(n,or|lr,t,r,e)}function U(n,r,t){function e(){c&&clearTimeout(c),i=c=p=ur,(v||g!==r)&&(s=lt(),f=n.apply(l,o),c||i||(o=l=null))}function u(){var t=r-(lt()-a);0=y;m?(i&&(i=clearTimeout(i)),s=a,f=n.apply(l,o)):i||(i=setTimeout(e,y))}return m&&c?c=clearTimeout(c):c||r===g||(c=setTimeout(u,r)),t&&(m=true,f=n.apply(l,o)),!m||c||i||(o=l=null),f}}function V(n,r,t){if(!n)return n;var e=arguments,u=0,o=e.length,i=typeof t; +for("number"!=i&&"string"!=i||!e[3]||e[3][t]!==r||(o=2);++u"']/g,yr=/($^)/,mr=/['\n\r\t\u2028\u2029\\]/g,br="[object Arguments]",_r="[object Array]",dr="[object Boolean]",wr="[object Date]",jr="[object Number]",xr="[object Object]",Tr="[object RegExp]",Ar="[object String]",Er={"&":"&","<":"<",">":">",'"':""","'":"'"},Or={"&":"&","<":"<",">":">",""":'"',"'":"'"},kr={"function":true,object:true},Sr={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},Nr=kr[typeof window]&&window||this,qr=kr[typeof exports]&&exports&&!exports.nodeType&&exports,Fr=kr[typeof module]&&module&&!module.nodeType&&module,Br=qr&&Fr&&typeof global=="object"&&global; +!Br||Br.global!==Br&&Br.window!==Br&&Br.self!==Br||(Nr=Br);var Rr=Fr&&Fr.exports===qr&&qr,$r=Array.prototype,Ir=Object.prototype,Mr=Nr._,Dr=Ir.toString,Wr=RegExp("^"+(Dr+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),zr=Math.ceil,Cr=Math.floor,Pr=Function.prototype.toString,Ur=Ir.hasOwnProperty,Vr=$r.push,Gr=Ir.propertyIsEnumerable,Hr=$r.splice,Jr=j(Jr=Object.create)&&Jr,Kr=j(Kr=Array.isArray)&&Kr,Lr=Nr.isFinite,Qr=Nr.isNaN,Xr=j(Xr=Object.keys)&&Xr,Yr=Math.max,Zr=Math.min,nt=j(nt=Date.now)&&nt,rt=Math.random; +a.prototype=f.prototype;var tt={};!function(){var n={0:1,length:1};tt.spliceObjects=(Hr.call(n,0,1),!n[0])}(1),f.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Jr||(c=function(){function n(){}return function(r){if(L(r)){n.prototype=r;var t=new n;n.prototype=null}return t||Nr.Object()}}()),x(arguments)||(x=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ur.call(n,"callee")&&!Gr.call(n,"callee")||false});var et=_(function(n,r,t){Ur.call(n,t)?n[t]++:n[t]=1 +}),ut=_(function(n,r,t){Ur.call(n,t)?n[t].push(r):n[t]=[r]}),ot=_(function(n,r,t){n[t]=r}),it=$,ft=Kr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Dr.call(n)==_r||false};K(/x/)&&(K=function(n){return typeof n=="function"&&"[object Function]"==Dr.call(n)});var at=Xr?function(n){return L(n)?Xr(n):[]}:n,lt=nt||function(){return(new Date).getTime()};f.after=function(n,r){if(!K(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},f.bind=P,f.bindAll=function(n){for(var r=1i(a,e)){for(r=t;--r;)if(0>i(n[r],e))continue n;a.push(e)}return a},f.invert=function(n){for(var r=-1,t=at(n),e=t.length,u={};++rarguments.length&&typeof u=="number")for(;++tr?0:r);++nt?Yr(0,e+t):Zr(t,e-1))+1);e--;)if(n[e]===r)return e; +return-1},f.mixin=tr,f.noConflict=function(){return Nr._=Mr,this},f.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0,n+Cr(rt()*(r-n+1))},f.reduce=M,f.reduceRight=D,f.result=function(n,r){if(null!=n){var t=n[r];return K(t)?n[r]():t}},f.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:at(n).length},f.some=z,f.sortedIndex=k,f.template=function(n,r,t){var e=f,u=e.templateSettings;n=(n||"")+"",t=G({},t,u);var i=0,a="__p+='",u=t.variable;n.replace(RegExp((t.escape||yr).source+"|"+(t.interpolate||yr).source+"|"+(t.evaluate||yr).source+"|$","g"),function(r,t,e,u,f){return a+=n.slice(i,f).replace(mr,o),t&&(a+="'+_.escape("+t+")+'"),u&&(a+="';"+u+";\n__p+='"),e&&(a+="'+((__t=("+e+"))==null?'':__t)+'"),i=f+r.length,r +}),a+="';",u||(u="obj",a="with("+u+"||{}){"+a+"}"),a="function("+u+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}";try{var l=Function("_","return "+a)(e)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},f.unescape=function(n){return null==n?"":(n+="",0>n.indexOf(";")?n:n.replace(vr,i))},f.uniqueId=function(n){var r=++gr+"";return n?n+r:r},f.all=q,f.any=z,f.detect=B,f.findWhere=function(n,r){return C(n,r,true)},f.foldl=M,f.foldr=D,f.include=N,f.inject=M,f.first=T,f.last=function(n,r,t){var e=0,u=n?n.length:0; +if(typeof r!="number"&&null!=r){var o=u;for(r=Z(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:ur;return e=u-e,O(n,0 [[1, 3], [2]] + * + * _.partition([1.2, 2.3, 3.4], function(num) { return this.floor(num) % 2; }, Math); + * // => [[1, 3], [2]] + * + * var characters = [ + * { 'name': 'barney', 'age': 36 }, + * { 'name': 'fred', 'age': 40, 'blocked': true }, + * { 'name': 'pebbles', 'age': 1 } + * ]; + * + * // using "_.where" callback shorthand + * _.map(_.partition(characters, { 'age': 1 }), function(array) { return _.pluck(array, 'name'); }); + * // => [['pebbles'], ['barney', 'fred']] + * + * // using "_.pluck" callback shorthand + * _.map(_.partition(characters, 'blocked'), function(array) { return _.pluck(array, 'name'); }); + * // => [['fred'], ['barney', 'pebbles']] + */ + var partition = createAggregator(function(result, value, key) { + result[key ? 0 : 1].push(value); + }, true); + /** * Retrieves the value of a specified property from all elements in the collection. * @@ -7452,6 +7502,7 @@ lodash.pairs = pairs; lodash.partial = partial; lodash.partialRight = partialRight; + lodash.partition = partition; lodash.pick = pick; lodash.pluck = pluck; lodash.property = property; diff --git a/perf/perf.js b/perf/perf.js index 4be112ef3..52c5cf5d5 100644 --- a/perf/perf.js +++ b/perf/perf.js @@ -1533,6 +1533,50 @@ /*--------------------------------------------------------------------------*/ + suites.push( + Benchmark.Suite('`_.partition` iterating an array') + .add(buildName, '\ + lodash.partition(numbers, function(num) {\ + return num % 2;\ + })' + ) + .add(otherName, '\ + _.partition(numbers, function(num) {\ + return num % 2;\ + })' + ) + ); + + suites.push( + Benchmark.Suite('`_.partition` iterating an array with `thisArg` (slow path)') + .add(buildName, '\ + lodash.partition(numbers, function(num, index) {\ + return this["key" + index] % 2;\ + }, object)' + ) + .add(otherName, '\ + _.partition(numbers, function(num, index) {\ + return this["key" + index] % 2;\ + }, object)' + ) + ); + + suites.push( + Benchmark.Suite('`_.partition` iterating an object') + .add(buildName, '\ + lodash.partition(object, function(num) {\ + return num % 2;\ + })' + ) + .add(otherName, '\ + _.partition(object, function(num) {\ + return num % 2;\ + })' + ) + ); + + /*--------------------------------------------------------------------------*/ + suites.push( Benchmark.Suite('`_.pick`') .add(buildName, '\ @@ -1657,12 +1701,12 @@ Benchmark.Suite('`_.reject` iterating an object') .add(buildName, '\ lodash.reject(object, function(num) {\ - return num % 2\ + return num % 2;\ })' ) .add(otherName, '\ _.reject(object, function(num) {\ - return num % 2\ + return num % 2;\ })' ) ); diff --git a/test/test.js b/test/test.js index d2897d3b6..eb4b510bd 100644 --- a/test/test.js +++ b/test/test.js @@ -6103,6 +6103,69 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.partition'); + + (function() { + var array = [1, 0, 1]; + + test('should always return two groups of elements', 3, function() { + deepEqual(_.partition([], function(value) { return value; }), [[], []]); + deepEqual(_.partition(array, function(value) { return true; }), [array, []]); + deepEqual(_.partition(array, function(value) { return false; }), [[], array]); + }); + + test('should use `_.identity` when no `callback` is provided', 1, function() { + var actual = _.partition(array); + deepEqual(actual, [[1, 1], [0]]); + }); + + test('should pass the correct `callback` arguments', 1, function() { + var args; + + _.partition(array, function() { + args || (args = slice.call(arguments)); + }); + + deepEqual(args, [1, 0, array]); + }); + + test('should support the `thisArg` argument', 1, function() { + var actual = _.partition([1.1, 0.2, 1.3], function(num) { + return this.floor(num); + }, Math); + + deepEqual(actual, [[1.1, 1.3], [0.2]]); + }); + + test('should work with an object for `collection`', 1, function() { + var actual = _.partition({ 'a': 1.1, 'b': 0.2, 'c': 1.3 }, function(num) { + return Math.floor(num); + }); + + deepEqual(actual, [[1.1, 1.3], [0.2]]); + }); + + test('should work with a number for `callback`', 2, function() { + var array = [ + [1, 0], + [0, 1], + [1, 0] + ]; + + deepEqual(_.partition(array, 0), [[array[0], array[2]], [array[1]]]); + deepEqual(_.partition(array, 1), [[array[1]], [array[0], array[2]]]); + }); + + test('should work with a string for `callback`', 1, function() { + var objects = [{ 'a': 1 }, { 'a': 1 }, { 'b': 2 }], + actual = _.partition(objects, 'a'); + + deepEqual(actual, [objects.slice(0, 2), objects.slice(2)]); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.pick'); (function() { @@ -9129,7 +9192,7 @@ var acceptFalsey = _.difference(allMethods, rejectFalsey); - test('should accept falsey arguments', 164, function() { + test('should accept falsey arguments', 165, function() { var emptyArrays = _.map(falsey, function() { return []; }), isExposed = '_' in root, oldDash = root._;