From ac8f8d0dd623a1201c188bdeec4366b31f9d9f56 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 29 Oct 2013 09:32:40 -0700 Subject: [PATCH] Ensure `_.min` and `_.max` can be used as the `callback` of `_.map`. [closes #379] --- dist/lodash.compat.js | 31 ++++++++++++++------- dist/lodash.compat.min.js | 31 +++++++++++---------- dist/lodash.js | 31 ++++++++++++++------- dist/lodash.min.js | 37 +++++++++++++------------ dist/lodash.underscore.js | 32 +++++++++++++++------ dist/lodash.underscore.min.js | 52 +++++++++++++++++------------------ lodash.js | 31 ++++++++++++++------- test/test.js | 11 ++++++-- 8 files changed, 156 insertions(+), 100 deletions(-) diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index fd5f63b80..285d6fd00 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -2900,6 +2900,7 @@ if (!isObject(object)) { return object; } + // allows working with `_.reduce` and `_.reduceRight` without using // their `index` and `collection` arguments if (typeof args[2] != 'number') { @@ -3779,7 +3780,12 @@ var computed = -Infinity, result = computed; - if (!callback && isArray(collection)) { + // allows working with functions like `_.map` without using + // their `index` argument as a callback + if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { + callback = null; + } + if (callback == null && isArray(collection)) { var index = -1, length = collection.length; @@ -3790,7 +3796,7 @@ } } } else { - callback = (!callback && isString(collection)) + callback = (callback == null && isString(collection)) ? charAtCallback : lodash.createCallback(callback, thisArg, 3); @@ -3849,7 +3855,12 @@ var computed = Infinity, result = computed; - if (!callback && isArray(collection)) { + // allows working with functions like `_.map` without using + // their `index` argument as a callback + if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { + callback = null; + } + if (callback == null && isArray(collection)) { var index = -1, length = collection.length; @@ -3860,7 +3871,7 @@ } } } else { - callback = (!callback && isString(collection)) + callback = (callback == null && isString(collection)) ? charAtCallback : lodash.createCallback(callback, thisArg, 3); @@ -4034,8 +4045,8 @@ * @category Collections * @param {Array|Object|string} collection The collection to sample. * @param {number} [n] The number of elements to sample. - * @param- {Object} [guard] Allows working with functions, like `_.map`, - * without using their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with functions like `_.map` + * without using their `index` arguments as `n`. * @returns {Array} Returns the random sample(s) of `collection`. * @example * @@ -4577,7 +4588,7 @@ // juggle arguments if (typeof isShallow != 'boolean' && isShallow != null) { thisArg = callback; - callback = !(thisArg && thisArg[isShallow] === array) ? isShallow : null; + callback = (typeof isShallow != 'function' && thisArg && thisArg[isShallow] === array) ? null : isShallow; isShallow = false; } if (callback != null) { @@ -4931,7 +4942,7 @@ end = start; start = 0; } - // use `Array(length)` so engines, like Chakra and V8, avoid slower modes + // use `Array(length)` so engines like Chakra and V8 avoid slower modes // http://youtu.be/XAqIpGU8ZZk#t=17m25s var index = -1, length = nativeMax(0, ceil((end - start) / (step || 1))), @@ -5192,7 +5203,7 @@ // juggle arguments if (typeof isSorted != 'boolean' && isSorted != null) { thisArg = callback; - callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : null; + callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted; isSorted = false; } if (callback != null) { @@ -6842,7 +6853,7 @@ // expose Lo-Dash var _ = runInContext(); - // some AMD build optimizers, like r.js, check for condition patterns like the following: + // some AMD build optimizers like r.js check for condition patterns like the following: if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { // Expose Lo-Dash to the global object even when an AMD loader is present in // case Lo-Dash was injected by a third-party script and not intended to be diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 9a68b45c9..a72b930cc 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -21,28 +21,29 @@ r+="}"}return(e.b||Te.nonEnumArgs)&&(r+="}"),r+=e.c+";return E",n("d,j,k,m,o,p,q }function Ct(n){return typeof n=="string"||n&&typeof n=="object"&&ge.call(n)==H||!1}function kt(n){for(var t=-1,e=We(n),r=e.length,u=Yt(r);++te?Ne(0,o+e):e)||0,qe(n)?a=-1o&&(o=i)}}else t=!t&&Ct(n)?r:y.createCallback(t,e,3),Xe(n,function(n,e,r){e=t(n,e,r),e>u&&(u=e,o=n)});return o}function Pt(n,t,e,r){var u=3>arguments.length;if(t=et(t,r,4),qe(n)){var o=-1,a=n.length;for(u&&(e=n[++o]);++oarguments.length;return t=et(t,r,4),Dt(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)}),e}function Ft(n){var t=-1,e=n?n.length:0,r=Yt(typeof e=="number"?e:0); -return At(n,function(n){var e=ft(0,++t);r[t]=r[e],r[e]=n}),r}function $t(n,t,e){var r;if(t=y.createCallback(t,e,3),qe(n)){e=-1;for(var u=n.length;++e=w&&u===n;if(l){var c=o(i);c?(u=t,i=c):l=!1}for(;++ru(i,c)&&f.push(c);return l&&s(i),f}function Tt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=y.createCallback(t,e,3);++or?Ne(0,u+r):r||0}else if(r)return r=Kt(t,e),t[r]===e?r:-1;return n(t,e,r)}function qt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0;for(t=y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++to&&(o=i)}}else t=null==t&&Ct(n)?r:y.createCallback(t,e,3),Xe(n,function(n,e,r){e=t(n,e,r),e>u&&(u=e,o=n)});return o}function Pt(n,t,e,r){var u=3>arguments.length;if(t=et(t,r,4),qe(n)){var o=-1,a=n.length;for(u&&(e=n[++o]);++oarguments.length;return t=et(t,r,4),Dt(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o) +}),e}function Ft(n){var t=-1,e=n?n.length:0,r=Yt(typeof e=="number"?e:0);return At(n,function(n){var e=ft(0,++t);r[t]=r[e],r[e]=n}),r}function $t(n,t,e){var r;if(t=y.createCallback(t,e,3),qe(n)){e=-1;for(var u=n.length;++e=w&&u===n;if(l){var c=o(i);c?(u=t,i=c):l=!1}for(;++ru(i,c)&&f.push(c);return l&&s(i),f}function Tt(n,t,e){var r=0,u=n?n.length:0; +if(typeof t!="number"&&null!=t){var o=-1;for(t=y.createCallback(t,e,3);++or?Ne(0,u+r):r||0}else if(r)return r=Kt(t,e),t[r]===e?r:-1;return n(t,e,r)}function qt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0;for(t=y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:D,variable:"",imports:{_:y}},Oe||(tt=function(n){if(jt(n)){c.prototype=n;var t=new c;c.prototype=null}return t||{}});var ze=Ee?function(n,t){Q.value=t,Ee(n,"__bindData__",Q)}:c;Te.argsClass||(mt=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&be.call(n,"callee")||!1});var qe=Se||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&ge.call(n)==T||!1 },Ke=st({a:"z",e:"[]",i:"if(!(B[typeof z]))return E",g:"E.push(n)"}),We=De?function(n){return jt(n)?Te.enumPrototypes&&typeof n=="function"||Te.nonEnumArgs&&n.length&&mt(n)?Ke(n):De(n):[]}:Ke,Ge={a:"g,e,K",i:"e=e&&typeof K=='undefined'?e:d(e,K,3)",b:"typeof u=='number'",v:We,g:"if(e(t[n],n,g)===false)return E"},Je={a:"z,H,l",i:"var a=arguments,b=0,c=typeof l=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Ue=bt(He),Ve=oe("("+We(Ue).join("|")+")","g"),Qe=oe("["+We(He).join("")+"]","g"),Xe=st(Ge),Ye=st(Je,{i:Je.i.replace(";",";if(c>3&&typeof a[c-2]=='function'){var e=d(a[--c-1],a[c--],2)}else if(c>2&&typeof a[c-1]=='function'){e=a[--c]}"),g:"E[n]=e?e(E[n],t[n]):t[n]"}),Ze=st(Je),nr=st(Ge,Me,{j:!1}),tr=st(Ge,Me); wt(/x/)&&(wt=function(n){return typeof n=="function"&&ge.call(n)==W});var er=_e?function(n){if(!n||ge.call(n)!=J||!Te.argsClass&&mt(n))return!1;var t=n.valueOf,e=typeof t=="function"&&(e=_e(t))&&_e(e);return e?n==e||_e(n)==e:vt(n)}:vt,rr=ct(function(n,t,e){be.call(n,e)?n[e]++:n[e]=1}),ur=ct(function(n,t,e){(be.call(n,e)?n[e]:n[e]=[]).push(t)}),or=ct(function(n,t,e){n[e]=t}),ar=Nt;Fe&&(Ut=function(n){if(!wt(n))throw new ie;return Fe.apply(e,arguments)});var ir=8==Pe(x+"08")?Pe:function(n,t){return Pe(Ct(n)?n.replace(N,""):n,t||0) };return y.after=function(n,t){if(!wt(t))throw new ie;return function(){return 1>--n?t.apply(this,arguments):void 0}},y.assign=Ye,y.at=function(n){var t=arguments,e=-1,r=ut(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Yt(t);for(Te.unindexedChars&&Ct(n)&&(n=n.split(""));++e=w&&o(a?r[a]:v)}n:for(;++l(m?t(m,y):c(v,y))){for(a=u,(m||v).push(y);--a;)if(m=f[a],0>(m?t(m,y):c(r[a],y)))continue n;h.push(y)}}for(;u--;)(m=f[u])&&s(m);return p(f),p(v),h},y.invert=bt,y.invoke=function(n,t){var e=g(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Yt(typeof o=="number"?o:0);return At(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},y.keys=We,y.map=Nt,y.max=Bt,y.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):b+arguments[0]; -return be.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!wt(n))throw new ie;return e.cache={},e},y.merge=function(n){var t=arguments,e=2;if(!jt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(a,e))&&(o[e]=n)}),o},y.once=function(n){var t,e;if(!wt(n))throw new ie;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},y.pairs=function(n){for(var t=-1,e=We(n),r=e.length,u=Yt(r);++tr(a,e))&&(o[e]=n)}),o},y.once=function(n){var t,e;if(!wt(n))throw new ie;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},y.pairs=function(n){for(var t=-1,e=We(n),r=e.length,u=Yt(r);++te?Ee(0,o+e):e)||0,Be(n)?i=-1o&&(o=a)}}else t=!t&&kt(n)?r:Y.createCallback(t,e,3),Et(n,function(n,e,r){e=t(n,e,r),e>u&&(u=e,o=n)});return o}function Dt(n,t){var e=-1,r=n?n.length:0;if(typeof r=="number")for(var u=Yt(r);++earguments.length;t=ut(t,r,4);var o=-1,i=n.length;if(typeof i=="number")for(u&&(e=n[++o]);++oarguments.length;return t=ut(t,r,4),St(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)}),e}function Tt(n){var t=-1,e=n?n.length:0,r=Yt(typeof e=="number"?e:0);return Et(n,function(n){var e=ft(0,++t);r[t]=r[e],r[e]=n}),r}function Bt(n,t,e){var r; -t=Y.createCallback(t,e,3),e=-1;var u=n?n.length:0;if(typeof u=="number")for(;++e=b&&u===n;if(l){var c=o(a);c?(u=t,a=c):l=!1}for(;++ru(a,c)&&f.push(c);return l&&p(a),f}function qt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1;for(t=Y.createCallback(t,e,3);++or?Ee(0,u+r):r||0}else if(r)return r=Kt(t,e),t[r]===e?r:-1;return n(t,e,r)}function Pt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0;for(t=Y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:N,variable:"",imports:{_:Y}},xe||(rt=function(n){if(wt(n)){l.prototype=n;var t=new l;l.prototype=null}return t||{}});var Te=ke?function(n,t){U.value=t,ke(n,"__bindData__",U)}:l,Be=Ce||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&pe.call(n)==F||!1},We=Ne?function(n){return wt(n)?Ne(n):[]}:Q,qe={"&":"&","<":"<",">":">",'"':""","'":"'"},ze=bt(qe),Pe=oe("("+We(ze).join("|")+")","g"),Ke=oe("["+We(qe).join("")+"]","g"),Le=ct(function(n,t,e){_e.call(n,e)?n[e]++:n[e]=1 -}),Me=ct(function(n,t,e){(_e.call(n,e)?n[e]:n[e]=[]).push(t)}),Ue=ct(function(n,t,e){n[e]=t});De&&(Ht=function(n){if(!dt(n))throw new ae;return De.apply(e,arguments)});var Ve=8==Re(w+"08")?Re:function(n,t){return Re(kt(n)?n.replace(E,""):n,t||0)};return Y.after=function(n,t){if(!dt(t))throw new ae;return function(){return 1>--n?t.apply(this,arguments):void 0}},Y.assign=J,Y.at=function(n){for(var t=arguments,e=-1,r=ot(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Yt(t);++eo&&(o=a)}}else t=null==t&&kt(n)?r:Y.createCallback(t,e,3),Et(n,function(n,e,r){e=t(n,e,r),e>u&&(u=e,o=n)}); +return o}function Dt(n,t){var e=-1,r=n?n.length:0;if(typeof r=="number")for(var u=Yt(r);++earguments.length;t=ut(t,r,4);var o=-1,i=n.length;if(typeof i=="number")for(u&&(e=n[++o]);++oarguments.length;return t=ut(t,r,4),St(n,function(n,r,o){e=u?(u=!1,n):t(e,n,r,o)}),e}function Tt(n){var t=-1,e=n?n.length:0,r=Yt(typeof e=="number"?e:0); +return Et(n,function(n){var e=ft(0,++t);r[t]=r[e],r[e]=n}),r}function Bt(n,t,e){var r;t=Y.createCallback(t,e,3),e=-1;var u=n?n.length:0;if(typeof u=="number")for(;++e=b&&u===n;if(l){var c=o(a);c?(u=t,a=c):l=!1}for(;++ru(a,c)&&f.push(c);return l&&p(a),f}function qt(n,t,e){var r=0,u=n?n.length:0;if(typeof t!="number"&&null!=t){var o=-1; +for(t=Y.createCallback(t,e,3);++or?Ee(0,u+r):r||0}else if(r)return r=Kt(t,e),t[r]===e?r:-1;return n(t,e,r)}function Pt(n,t,e){if(typeof t!="number"&&null!=t){var r=0,u=-1,o=n?n.length:0;for(t=Y.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++t/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:N,variable:"",imports:{_:Y}},xe||(rt=function(n){if(wt(n)){l.prototype=n; +var t=new l;l.prototype=null}return t||{}});var Te=ke?function(n,t){U.value=t,ke(n,"__bindData__",U)}:l,Be=Ce||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&pe.call(n)==F||!1},We=Ne?function(n){return wt(n)?Ne(n):[]}:Q,qe={"&":"&","<":"<",">":">",'"':""","'":"'"},ze=bt(qe),Pe=oe("("+We(ze).join("|")+")","g"),Ke=oe("["+We(qe).join("")+"]","g"),Le=ct(function(n,t,e){_e.call(n,e)?n[e]++:n[e]=1}),Me=ct(function(n,t,e){(_e.call(n,e)?n[e]:n[e]=[]).push(t)}),Ue=ct(function(n,t,e){n[e]=t +});De&&(Ht=function(n){if(!dt(n))throw new ae;return De.apply(e,arguments)});var Ve=8==Re(w+"08")?Re:function(n,t){return Re(kt(n)?n.replace(E,""):n,t||0)};return Y.after=function(n,t){if(!dt(t))throw new ae;return function(){return 1>--n?t.apply(this,arguments):void 0}},Y.assign=J,Y.at=function(n){for(var t=arguments,e=-1,r=ot(t,!0,!1,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Yt(t);++e=b&&o(i?r[i]:g)}n:for(;++l(m?t(m,y):s(g,y))){for(i=u,(m||g).push(y);--i;)if(m=f[i],0>(m?t(m,y):s(r[i],y)))continue n;h.push(y)}}for(;u--;)(m=f[u])&&p(m);return c(f),c(g),h},Y.invert=bt,Y.invoke=function(n,t){var e=s(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,i=Yt(typeof o=="number"?o:0); return Et(n,function(n){i[++r]=(u?t:n[t]).apply(n,e)}),i},Y.keys=We,Y.map=Rt,Y.max=At,Y.memoize=function(n,t){function e(){var r=e.cache,u=t?t.apply(this,arguments):_+arguments[0];return _e.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!dt(n))throw new ae;return e.cache={},e},Y.merge=function(n){var t=arguments,e=2;if(!wt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(i,e))&&(o[e]=n)}),o},Y.once=function(n){var t,e;if(!dt(n))throw new ae;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e) -}},Y.pairs=function(n){for(var t=-1,e=We(n),r=e.length,u=Yt(r);++tr(i,e))&&(o[e]=n)}),o},Y.once=function(n){var t,e;if(!dt(n))throw new ae; +return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},Y.pairs=function(n){for(var t=-1,e=We(n),r=e.length,u=Yt(r);++t result) { @@ -2289,7 +2294,9 @@ } } } else { - callback = createCallback(callback, thisArg, 3); + callback = (callback == null && isString(collection)) + ? charAtCallback + : createCallback(callback, thisArg, 3); forEach(collection, function(value, index, collection) { var current = callback(value, index, collection); @@ -2346,10 +2353,15 @@ var computed = Infinity, result = computed; + // allows working with functions like `_.map` without using + // their `index` argument as a callback + if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { + callback = null; + } var index = -1, length = collection ? collection.length : 0; - if (!callback && typeof length == 'number') { + if (callback == null && typeof length == 'number') { while (++index < length) { var value = collection[index]; if (value < result) { @@ -2357,7 +2369,9 @@ } } } else { - callback = createCallback(callback, thisArg, 3); + callback = (callback == null && isString(collection)) + ? charAtCallback + : createCallback(callback, thisArg, 3); forEach(collection, function(value, index, collection) { var current = callback(value, index, collection); @@ -2541,8 +2555,8 @@ * @category Collections * @param {Array|Object|string} collection The collection to sample. * @param {number} [n] The number of elements to sample. - * @param- {Object} [guard] Allows working with functions, like `_.map`, - * without using their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with functions like `_.map` + * without using their `index` arguments as `n`. * @returns {Array} Returns the random sample(s) of `collection`. * @example * @@ -3254,7 +3268,7 @@ end = start; start = 0; } - // use `Array(length)` so engines, like Chakra and V8, avoid slower modes + // use `Array(length)` so engines like Chakra and V8 avoid slower modes // http://youtu.be/XAqIpGU8ZZk#t=17m25s var index = -1, length = nativeMax(0, ceil((end - start) / step)), @@ -3466,7 +3480,7 @@ // juggle arguments if (typeof isSorted != 'boolean' && isSorted != null) { thisArg = callback; - callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : null; + callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted; isSorted = false; } if (callback != null) { @@ -4785,7 +4799,7 @@ /*--------------------------------------------------------------------------*/ - // some AMD build optimizers, like r.js, check for condition patterns like the following: + // some AMD build optimizers like r.js check for condition patterns like the following: if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { // Expose Lo-Dash to the global object even when an AMD loader is present in // case Lo-Dash was injected by a third-party script and not intended to be diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 550ae5306..4600a16db 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -4,36 +4,36 @@ * 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")return 1;if(tt?0:t);++eu(f,c))&&(t&&f.push(c),i.push(a))}return i}function g(n){return function(r,t,e){var u={};t=Q(t,e,3),e=-1;var o=r?r.length:0;if(typeof o=="number")for(;++eu(f,l))&&(t&&f.push(l),i.push(a))}return i}function g(n){return function(r,t,e){var u={};t=Q(t,e,3),e=-1;var o=r?r.length:0;if(typeof o=="number")for(;++ee&&(e=t,u=n)});else for(;++ou&&(u=t);return u}function $(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=c(r,e,4); -var o=-1,i=n.length;if(typeof i=="number")for(u&&(t=n[++o]);++oarguments.length;return r=c(r,e,4),q(n,function(n,e,o){t=u?(u=!1,n):r(t,n,e,o)}),t}function z(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return F(n,function(n){var t;t=++r,t=0+Or(Ir()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function C(n,r,t){var e;r=Q(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++tt(u,i)&&o.push(i)}return o}function V(n,r,t){var e=0,o=n?n.length:0;if(typeof r!="number"&&null!=r){var i=-1;for(r=Q(r,t,3);++ie?Mr(0,u+e):e||0}else if(e)return e=J(r,t),r[e]===t?e:-1;return n(r,t,e) -}function H(n,r,t){if(typeof r!="number"&&null!=r){var e=0,o=-1,i=n?n.length:0;for(r=Q(r,t,3);++o>>1,t(n[e])/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},kr||(a=function(n){if(E(n)){e.prototype=n;var r=new e;e.prototype=null}return r||{}}),_(arguments)||(_=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Sr.call(n,"callee")||!1});var zr=Br||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ar.call(n)==fr||!1 +}),r.sort()}function j(n){for(var r=-1,t=Pr(n),e=t.length,u={};++ru&&(u=t);else r=null==r&&O(n)?charAtCallback:Q(r,t,3),F(n,function(n,t,o){t=r(n,t,o),t>e&&(e=t,u=n)});return u}function $(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=l(r,e,4);var o=-1,i=n.length;if(typeof i=="number")for(u&&(t=n[++o]);++oarguments.length;return r=l(r,e,4),q(n,function(n,e,o){t=u?(u=!1,n):r(t,n,e,o)}),t}function z(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return F(n,function(n){var t;t=++r,t=0+Or(Ir()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function C(n,r,t){var e;r=Q(r,t,3),t=-1; +var u=n?n.length:0;if(typeof u=="number")for(;++tt(u,i)&&o.push(i)}return o}function V(n,r,t){var e=0,o=n?n.length:0;if(typeof r!="number"&&null!=r){var i=-1;for(r=Q(r,t,3);++ie?Mr(0,u+e):e||0}else if(e)return e=J(r,t),r[e]===t?e:-1;return n(r,t,e)}function H(n,r,t){if(typeof r!="number"&&null!=r){var e=0,o=-1,i=n?n.length:0;for(r=Q(r,t,3);++o>>1,t(n[e])/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Rr||(a=function(n){if(E(n)){e.prototype=n;var r=new e;e.prototype=null}return r||{}}),_(arguments)||(_=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Sr.call(n,"callee")||!1});var zr=Br||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ar.call(n)==fr||!1 },Cr=function(n){var r,t=[];if(!n||!hr[typeof n])return t;for(r in n)Sr.call(n,r)&&t.push(r);return t},Pr=Dr?function(n){return E(n)?Dr(n):[]}:Cr,Ur={"&":"&","<":"<",">":">",'"':""","'":"'"},Vr=j(Ur),Gr=RegExp("("+Pr(Vr).join("|")+")","g"),Hr=RegExp("["+Pr(Ur).join("")+"]","g"),Jr=function(n,r){var t;if(!n||!hr[typeof n])return n;for(t in n)if(r(n[t],t,n)===tr)break;return n},Kr=function(n,r){var t;if(!n||!hr[typeof n])return n;for(t in n)if(Sr.call(n,t)&&r(n[t],t,n)===tr)break; -return n};A(/x/)&&(A=function(n){return typeof n=="function"&&"[object Function]"==Ar.call(n)});var Lr=g(function(n,r,t){Sr.call(n,t)?n[t]++:n[t]=1}),Qr=g(function(n,r,t){(Sr.call(n,t)?n[t]:n[t]=[]).push(r)}),Xr=g(function(n,r,t){n[t]=r});o.after=function(n,r){if(!A(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},o.bind=L,o.bindAll=function(n){for(var r=1--n?r.apply(this,arguments):void 0}},o.bind=L,o.bindAll=function(n){for(var r=1u(i,f)){for(var a=t;--a;)if(0>u(r[a],f))continue n; -i.push(f)}}return i},o.invert=j,o.invoke=function(n,r){var t=u(arguments,2),e=-1,o=typeof r=="function",i=n?n.length:0,f=Array(typeof i=="number"?i:0);return F(n,function(n){f[++e]=(o?r:n[r]).apply(n,t)}),f},o.keys=Pr,o.map=D,o.max=M,o.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):er+arguments[0];return Sr.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},o.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0;if(r||typeof i!="number")r=Q(r,t,3),F(n,function(n,t,o){t=r(n,t,o),tr(t,u)&&(e[u]=n)}),e},o.once=function(n){var r,t;if(!A(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},o.pairs=function(n){for(var r=-1,t=Pr(n),e=t.length,u=Array(e);++rr?0:r);++nu(i,f)){for(var a=t;--a;)if(0>u(r[a],f))continue n; +i.push(f)}}return i},o.invert=j,o.invoke=function(n,r){var t=u(arguments,2),e=-1,o=typeof r=="function",i=n?n.length:0,f=Array(typeof i=="number"?i:0);return F(n,function(n){f[++e]=(o?r:n[r]).apply(n,t)}),f},o.keys=Pr,o.map=D,o.max=M,o.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):er+arguments[0];return Sr.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},o.min=function(n,r,t){var e=1/0,u=e;typeof r!="function"&&t&&t[r]===n&&(r=null);var o=-1,i=n?n.length:0;if(null==r&&typeof i=="number")for(;++or(t,u)&&(e[u]=n)}),e},o.once=function(n){var r,t;if(!A(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},o.pairs=function(n){for(var r=-1,t=Pr(n),e=t.length,u=Array(e);++rr?0:r);++nt?Mr(0,e+t):$r(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},o.mixin=Z,o.noConflict=function(){return yr._=xr,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+Or(Ir()*(r-n+1))},o.reduce=I,o.reduceRight=W,o.result=function(n,r){if(n){var t=n[r];return A(t)?n[r]():t}},o.size=function(n){var r=n?n.length:0; return typeof r=="number"?r:Pr(n).length},o.some=C,o.sortedIndex=J,o.template=function(n,r,e){var u=o,i=u.templateSettings;n=(n||"")+"",e=b({},e,i);var f=0,a="__p+='",i=e.variable;n.replace(RegExp((e.escape||ur).source+"|"+(e.interpolate||ur).source+"|"+(e.evaluate||ur).source+"|$","g"),function(r,e,u,o,i){return a+=n.slice(f,i).replace(or,t),e&&(a+="'+_.escape("+e+")+'"),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 c=Function("_","return "+a)(u)}catch(l){throw l.source=a,l}return r?c(r):(c.source=a,c)},o.unescape=function(n){return null==n?"":(n+"").replace(Gr,m)},o.uniqueId=function(n){var r=++rr+"";return n?n+r:r},o.all=R,o.any=C,o.detect=B,o.findWhere=function(n,r){return P(n,r,!0)},o.foldl=I,o.foldr=W,o.include=N,o.inject=I,o.first=V,o.last=function(n,r,t){var e=0,o=n?n.length:0;if(typeof r!="number"&&null!=r){var i=o;for(r=Q(r,t,3);i--&&r(n[i],i,n);)e++}else if(e=r,null==e||t)return n?n[o-1]:nr; +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+"").replace(Gr,m)},o.uniqueId=function(n){var r=++rr+"";return n?n+r:r},o.all=N,o.any=C,o.detect=B,o.findWhere=function(n,r){return P(n,r,!0)},o.foldl=I,o.foldr=W,o.include=k,o.inject=I,o.first=V,o.last=function(n,r,t){var e=0,o=n?n.length:0;if(typeof r!="number"&&null!=r){var i=o;for(r=Q(r,t,3);i--&&r(n[i],i,n);)e++}else if(e=r,null==e||t)return n?n[o-1]:nr; return u(n,Mr(0,o-e))},o.sample=function(n,r,t){return n&&typeof n.length!="number"&&(n=S(n)),null==r||t?n?n[0+Or(Ir()*(n.length-1-0+1))]:nr:(n=z(n),n.length=$r(Mr(0,r),n.length),n)},o.take=V,o.head=V,Z(o),o.VERSION="2.2.1",o.prototype.chain=function(){return this.__chain__=!0,this},o.prototype.value=function(){return this.__wrapped__},F("pop push reverse shift sort splice unshift".split(" "),function(n){var r=wr[n];o.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),Wr.spliceObjects||0!==n.length||delete n[0],this }}),F(["concat","join","slice"],function(n){var r=wr[n];o.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new i(n),n.__chain__=!0),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(yr._=o, define(function(){return o})):mr&&_r?dr?(_r.exports=o)._=o:mr._=o:yr._=o}).call(this); \ No newline at end of file diff --git a/lodash.js b/lodash.js index 97d3ef934..47c792101 100644 --- a/lodash.js +++ b/lodash.js @@ -2918,6 +2918,7 @@ if (!isObject(object)) { return object; } + // allows working with `_.reduce` and `_.reduceRight` without using // their `index` and `collection` arguments if (typeof args[2] != 'number') { @@ -3797,7 +3798,12 @@ var computed = -Infinity, result = computed; - if (!callback && isArray(collection)) { + // allows working with functions like `_.map` without using + // their `index` argument as a callback + if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { + callback = null; + } + if (callback == null && isArray(collection)) { var index = -1, length = collection.length; @@ -3808,7 +3814,7 @@ } } } else { - callback = (!callback && isString(collection)) + callback = (callback == null && isString(collection)) ? charAtCallback : lodash.createCallback(callback, thisArg, 3); @@ -3867,7 +3873,12 @@ var computed = Infinity, result = computed; - if (!callback && isArray(collection)) { + // allows working with functions like `_.map` without using + // their `index` argument as a callback + if (typeof callback != 'function' && thisArg && thisArg[callback] === collection) { + callback = null; + } + if (callback == null && isArray(collection)) { var index = -1, length = collection.length; @@ -3878,7 +3889,7 @@ } } } else { - callback = (!callback && isString(collection)) + callback = (callback == null && isString(collection)) ? charAtCallback : lodash.createCallback(callback, thisArg, 3); @@ -4052,8 +4063,8 @@ * @category Collections * @param {Array|Object|string} collection The collection to sample. * @param {number} [n] The number of elements to sample. - * @param- {Object} [guard] Allows working with functions, like `_.map`, - * without using their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with functions like `_.map` + * without using their `index` arguments as `n`. * @returns {Array} Returns the random sample(s) of `collection`. * @example * @@ -4595,7 +4606,7 @@ // juggle arguments if (typeof isShallow != 'boolean' && isShallow != null) { thisArg = callback; - callback = !(thisArg && thisArg[isShallow] === array) ? isShallow : null; + callback = (typeof isShallow != 'function' && thisArg && thisArg[isShallow] === array) ? null : isShallow; isShallow = false; } if (callback != null) { @@ -4949,7 +4960,7 @@ end = start; start = 0; } - // use `Array(length)` so engines, like Chakra and V8, avoid slower modes + // use `Array(length)` so engines like Chakra and V8 avoid slower modes // http://youtu.be/XAqIpGU8ZZk#t=17m25s var index = -1, length = nativeMax(0, ceil((end - start) / (step || 1))), @@ -5210,7 +5221,7 @@ // juggle arguments if (typeof isSorted != 'boolean' && isSorted != null) { thisArg = callback; - callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : null; + callback = (typeof isSorted != 'function' && thisArg && thisArg[isSorted] === array) ? null : isSorted; isSorted = false; } if (callback != null) { @@ -6865,7 +6876,7 @@ // expose Lo-Dash var _ = runInContext(); - // some AMD build optimizers, like r.js, check for condition patterns like the following: + // some AMD build optimizers like r.js check for condition patterns like the following: if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { // Expose Lo-Dash to the global object even when an AMD loader is present in // case Lo-Dash was injected by a third-party script and not intended to be diff --git a/test/test.js b/test/test.js index e20b3077b..dd74fee81 100644 --- a/test/test.js +++ b/test/test.js @@ -917,7 +917,7 @@ deepEqual(args, [klass]); }); - test('`_.' + methodName + '`should support the `thisArg` argument', 1, function() { + test('`_.' + methodName + '` should support the `thisArg` argument', 1, function() { var actual = func('a', function(value) { return this[value]; }, { 'a': 'A' }); @@ -4668,7 +4668,7 @@ deepEqual(args, expected); }); - test('`_.' + methodName + '`should support the `thisArg` argument', 1, function() { + test('`_.' + methodName + '` should support the `thisArg` argument', 1, function() { var actual = func(array, function(num, index) { return -this[index]; }, array); @@ -4676,6 +4676,13 @@ equal(actual, methodName == 'max' ? 1 : 3); }); + test('`_.' + methodName + '` should work when used as `callback` for `_.map`', 1, function() { + var array = [[2, 3, 1], [5, 6, 4], [8, 9, 7]], + actual = _.map(array, func); + + deepEqual(actual, methodName == 'max' ? [3, 6, 9] : [1, 4, 7]); + }); + test('`_.' + methodName + '` should iterate an object', 1, function() { var actual = func({ 'a': 1, 'b': 2, 'c': 3 }); equal(actual, methodName == 'max' ? 3 : 1);