From 201d17bc315f840d97303de632d73515a463a050 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 4 Feb 2014 00:35:14 -0800 Subject: [PATCH] Allow `_.omit` and `_.pick` to work as a callback for `_.map` when combined with `_.partialRight`. --- dist/lodash.compat.js | 84 ++++++++++++++++++++--------------- dist/lodash.compat.min.js | 6 +-- dist/lodash.js | 84 ++++++++++++++++++++--------------- dist/lodash.min.js | 6 +-- dist/lodash.underscore.js | 60 +++++++++++++++---------- dist/lodash.underscore.min.js | 15 ++++--- lodash.js | 78 +++++++++++++++++++------------- test/test.js | 56 ++++++++++++++++++----- 8 files changed, 237 insertions(+), 152 deletions(-) diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index b7a761fe0..867237f62 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -2377,8 +2377,7 @@ callback = isShallow; isShallow = false; - // allows working with functions like `_.map` without using - // their `index` argument as a callback + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === array) { callback = null; } @@ -3059,8 +3058,7 @@ callback = isSorted; isSorted = false; - // allows working with functions like `_.map` without using - // their `index` argument as a callback + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === array) { callback = null; } @@ -3342,8 +3340,7 @@ length = props.length, type = typeof guard; - // allows working with functions like `_.map` without using - // their `index` arguments + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && args[2] && args[2][guard] === collection) { length = 1; } @@ -3980,8 +3977,7 @@ result = computed, type = typeof callback; - // allows working with functions like `_.map` without using - // their `index` argument as a callback + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === collection) { callback = null; } @@ -4056,8 +4052,7 @@ result = computed, type = typeof callback; - // allows working with functions like `_.map` without using - // their `index` argument as a callback + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === collection) { callback = null; } @@ -4247,8 +4242,7 @@ * @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 `index` arguments as `n`. + * @param- {Object} [guard] Enables use as a callback for functions like `_.map`. * @returns {*} Returns the random sample(s) of `collection`. * @example * @@ -5209,8 +5203,7 @@ * return typeof a == 'undefined' ? b : a; * }); * - * var object = { 'name': 'barney' }; - * defaults(object, { 'name': 'fred', 'employer': 'slate' }); + * defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' }); * // => { 'name': 'barney', 'employer': 'slate' } */ function assign(object, source, guard) { @@ -5219,8 +5212,7 @@ argsLength = args.length, type = typeof guard; - // allows working with functions like `_.reduce` without using their - // `key` and `object` arguments as sources + // enables use as a callback for functions like `_.reduce` if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) { argsLength = 2; } @@ -5301,8 +5293,7 @@ callback = isDeep; isDeep = false; - // allows working with functions like `_.map` without using - // their `index` argument as a callback + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === value) { callback = null; } @@ -5402,13 +5393,11 @@ * @category Objects * @param {Object} object The destination object. * @param {...Object} [source] The source objects. - * @param- {Object} [guard] Allows working with functions like `_.reduce` - * without using their `key` and `object` arguments as sources. + * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. * @returns {Object} Returns the destination object. * @example * - * var object = { 'name': 'barney' }; - * _.defaults(object, { 'name': 'fred', 'employer': 'slate' }); + * _.defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' }); * // => { 'name': 'barney', 'employer': 'slate' } */ function defaults(object, source, guard) { @@ -5417,8 +5406,7 @@ argsLength = args.length, type = typeof guard; - // allows working with functions like `_.reduce` without using their - // `key` and `object` arguments as sources + // enables use as a callback for functions like `_.reduce` if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) { argsLength = 2; } @@ -6320,8 +6308,7 @@ length = args.length, type = typeof guard; - // allows working with functions like `_.reduce` without using their - // `key` and `object` arguments as sources + // enables use as a callback for functions like `_.reduce` if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) { length = 2; } @@ -6356,8 +6343,9 @@ * @memberOf _ * @category Objects * @param {Object} object The source object. - * @param {Function|...string|string[]} [callback] The properties to omit or the - * function called per iteration. + * @param {Function|...string|string[]} [callback] The function called per + * iteration or property names to omit, specified as individual property + * names or arrays of property names. * @param {*} [thisArg] The `this` binding of `callback`. * @returns {Object} Returns an object without the omitted properties. * @example @@ -6371,16 +6359,31 @@ * // => { 'name': 'fred' } */ function omit(object, callback, thisArg) { - var result = {}; - if (typeof callback != 'function') { + var result = {}, + type = typeof callback; + + if (type != 'function') { + // enables use as a callback for functions like `_.map` + // when combined with `_.partialRight` + var args = arguments; + if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) { + args = slice(args); + splice.call(args, 1, 2); + } + var omitProps = baseFlatten(args, true, false, 1), + length = omitProps.length; + + while (length--) { + omitProps[length] = String(omitProps[length]); + } var props = []; baseForIn(object, function(value, key) { props.push(key); }); - props = baseDifference(props, baseFlatten(arguments, true, false, 1)); - var index = -1, - length = props.length; + var index = -1; + props = baseDifference(props, omitProps); + length = props.length; while (++index < length) { var key = props[index]; @@ -6452,10 +6455,19 @@ * // => { 'name': 'fred' } */ function pick(object, callback, thisArg) { - var result = {}; - if (typeof callback != 'function') { + var result = {}, + type = typeof callback; + + if (type != 'function') { + // enables use as a callback for functions like `_.map` + // when combined with `_.partialRight` + var args = arguments; + if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) { + args = slice(args); + splice.call(args, 1, 2); + } var index = -1, - props = baseFlatten(arguments, true, false, 1), + props = baseFlatten(args, true, false, 1), length = isObject(object) ? props.length : 0; while (++index < length) { diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index ff0c0b28f..98ef3626f 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -47,9 +47,9 @@ return"boolean"!=u&&null!=t&&(e=r,r=t,t=false,"number"!=u&&"string"!=u||!e||e[r] for(t=H(t,r,3);u--&&(r=e[u],false!==t(n[r],r,n)););return n},u.functions=er,u.groupBy=ve,u.indexBy=ye,u.initial=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 e=null==t||r?1:t||e;return e=o-e,qt(n,0,0=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 { 'name': 'barney', 'employer': 'slate' } */ function assign(object, source, guard) { @@ -4953,8 +4946,7 @@ argsLength = args.length, type = typeof guard; - // allows working with functions like `_.reduce` without using their - // `key` and `object` arguments as sources + // enables use as a callback for functions like `_.reduce` if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) { argsLength = 2; } @@ -5035,8 +5027,7 @@ callback = isDeep; isDeep = false; - // allows working with functions like `_.map` without using - // their `index` argument as a callback + // enables use as a callback for functions like `_.map` if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === value) { callback = null; } @@ -5136,13 +5127,11 @@ * @category Objects * @param {Object} object The destination object. * @param {...Object} [source] The source objects. - * @param- {Object} [guard] Allows working with functions like `_.reduce` - * without using their `key` and `object` arguments as sources. + * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. * @returns {Object} Returns the destination object. * @example * - * var object = { 'name': 'barney' }; - * _.defaults(object, { 'name': 'fred', 'employer': 'slate' }); + * _.defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' }); * // => { 'name': 'barney', 'employer': 'slate' } */ function defaults(object, source, guard) { @@ -5151,8 +5140,7 @@ argsLength = args.length, type = typeof guard; - // allows working with functions like `_.reduce` without using their - // `key` and `object` arguments as sources + // enables use as a callback for functions like `_.reduce` if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) { argsLength = 2; } @@ -6041,8 +6029,7 @@ length = args.length, type = typeof guard; - // allows working with functions like `_.reduce` without using their - // `key` and `object` arguments as sources + // enables use as a callback for functions like `_.reduce` if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) { length = 2; } @@ -6077,8 +6064,9 @@ * @memberOf _ * @category Objects * @param {Object} object The source object. - * @param {Function|...string|string[]} [callback] The properties to omit or the - * function called per iteration. + * @param {Function|...string|string[]} [callback] The function called per + * iteration or property names to omit, specified as individual property + * names or arrays of property names. * @param {*} [thisArg] The `this` binding of `callback`. * @returns {Object} Returns an object without the omitted properties. * @example @@ -6092,16 +6080,31 @@ * // => { 'name': 'fred' } */ function omit(object, callback, thisArg) { - var result = {}; - if (typeof callback != 'function') { + var result = {}, + type = typeof callback; + + if (type != 'function') { + // enables use as a callback for functions like `_.map` + // when combined with `_.partialRight` + var args = arguments; + if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) { + args = slice(args); + splice.call(args, 1, 2); + } + var omitProps = baseFlatten(args, true, false, 1), + length = omitProps.length; + + while (length--) { + omitProps[length] = String(omitProps[length]); + } var props = []; baseForIn(object, function(value, key) { props.push(key); }); - props = baseDifference(props, baseFlatten(arguments, true, false, 1)); - var index = -1, - length = props.length; + var index = -1; + props = baseDifference(props, omitProps); + length = props.length; while (++index < length) { var key = props[index]; @@ -6173,10 +6176,19 @@ * // => { 'name': 'fred' } */ function pick(object, callback, thisArg) { - var result = {}; - if (typeof callback != 'function') { + var result = {}, + type = typeof callback; + + if (type != 'function') { + // enables use as a callback for functions like `_.map` + // when combined with `_.partialRight` + var args = arguments; + if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) { + args = slice(args); + splice.call(args, 1, 2); + } var index = -1, - props = baseFlatten(arguments, true, false, 1), + props = baseFlatten(args, true, false, 1), length = isObject(object) ? props.length : 0; while (++index < length) { diff --git a/dist/lodash.min.js b/dist/lodash.min.js index 97bade34d..177cbd1d4 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -43,9 +43,9 @@ return"boolean"!=u&&null!=t&&(e=r,r=t,t=false,"number"!=u&&"string"!=u||!e||e[r] for(t=ct(t,r,3);u--&&(r=e[u],false!==t(n[r],r,n)););return n},y.functions=tr,y.groupBy=le,y.indexBy=ce,y.initial=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 e=null==t||r?1:t||e;return e=u-e,Tt(n,0,0=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 { 'name': 'barney', 'employer': 'slate' } */ function assign(object, source, guard) { @@ -3785,13 +3780,11 @@ * @category Objects * @param {Object} object The destination object. * @param {...Object} [source] The source objects. - * @param- {Object} [guard] Allows working with functions like `_.reduce` - * without using their `key` and `object` arguments as sources. + * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. * @returns {Object} Returns the destination object. * @example * - * var object = { 'name': 'barney' }; - * _.defaults(object, { 'name': 'fred', 'employer': 'slate' }); + * _.defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' }); * // => { 'name': 'barney', 'employer': 'slate' } */ function defaults(object, source, guard) { @@ -4298,8 +4291,9 @@ * @memberOf _ * @category Objects * @param {Object} object The source object. - * @param {Function|...string|string[]} [callback] The properties to omit or the - * function called per iteration. + * @param {Function|...string|string[]} [callback] The function called per + * iteration or property names to omit, specified as individual property + * names or arrays of property names. * @param {*} [thisArg] The `this` binding of `callback`. * @returns {Object} Returns an object without the omitted properties. * @example @@ -4312,16 +4306,29 @@ * }); * // => { 'name': 'fred' } */ - function omit(object) { + function omit(object, guard) { + var args = arguments, + result = {}, + type = typeof guard; + + if ((type == 'number' || type == 'string') && args[2] && args[2][guard] === object) { + args = slice(args); + splice.call(args, 1, 2); + } + var omitProps = baseFlatten(args, true, false, 1), + length = omitProps.length; + + while (length--) { + omitProps[length] = String(omitProps[length]); + } var props = []; baseForIn(object, function(value, key) { props.push(key); }); - props = baseDifference(props, baseFlatten(arguments, true, false, 1)); - var index = -1, - length = props.length, - result = {}; + var index = -1; + props = baseDifference(props, omitProps); + length = props.length; while (++index < length) { var key = props[index]; @@ -4384,9 +4391,16 @@ * }); * // => { 'name': 'fred' } */ - function pick(object) { + function pick(object, guard) { + var args = arguments, + type = typeof guard; + + if ((type == 'number' || type == 'string') && args[2] && args[2][guard] === object) { + args = slice(args); + splice.call(args, 1, 2); + } var index = -1, - props = baseFlatten(arguments, true, false, 1), + props = baseFlatten(args, true, false, 1), length = props.length, result = {}; diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 7d28da74b..d42facb5d 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -28,13 +28,14 @@ for(var t in n)if(r(n[t],t,n)===cr)break;return n},tt=function(n){var r=[];if(!J return function(){for(var t=r-1,e=n[t].apply(this,arguments);t--;)e=n[t].call(this,e);return e}},o.countBy=et,o.debounce=C,o.defaults=U,o.defer=function(n){if(!H(n))throw new TypeError;var r=A(arguments,1);return setTimeout(function(){n.apply(tr,r)},1)},o.delay=function(n,r){if(!H(n))throw new TypeError;var t=A(arguments,2);return setTimeout(function(){n.apply(tr,t)},r)},o.difference=function(n){return p(n,v(arguments,true,true,1))},o.filter=N,o.flatten=function(n,r,t){var e=typeof r;return"number"!=e&&"string"!=e||!t||t[r]!==n||(r=false),v(n,r) },o.forEach=F,o.functions=V,o.groupBy=ut,o.indexBy=ot,o.initial=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 e=null==r||t?1:r||e;return e=u-e,A(n,0,0i(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 +"number"!=o&&"string"!=o||!t||t[r]!==n||(r=null);var o=-1,i=n?n.length:0;if(null==r&&typeof i=="number")for(;++or?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,0 { 'name': 'fred' } */ function omit(object, callback, thisArg) { - var result = {}; - if (typeof callback != 'function') { + var result = {}, + type = typeof callback; + + if (type != 'function') { + // enables use as a callback for functions like `_.map` + // when combined with `_.partialRight` + var args = arguments; + if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) { + args = slice(args); + splice.call(args, 1, 2); + } + var omitProps = baseFlatten(args, true, false, 1), + length = omitProps.length; + + while (length--) { + omitProps[length] = String(omitProps[length]); + } var props = []; baseForIn(object, function(value, key) { props.push(key); }); - props = baseDifference(props, baseFlatten(arguments, true, false, 1)); - var index = -1, - length = props.length; + var index = -1; + props = baseDifference(props, omitProps); + length = props.length; while (++index < length) { var key = props[index]; @@ -6467,10 +6472,19 @@ * // => { 'name': 'fred' } */ function pick(object, callback, thisArg) { - var result = {}; - if (typeof callback != 'function') { + var result = {}, + type = typeof callback; + + if (type != 'function') { + // enables use as a callback for functions like `_.map` + // when combined with `_.partialRight` + var args = arguments; + if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) { + args = slice(args); + splice.call(args, 1, 2); + } var index = -1, - props = baseFlatten(arguments, true, false, 1), + props = baseFlatten(args, true, false, 1), length = isObject(object) ? props.length : 0; while (++index < length) { diff --git a/test/test.js b/test/test.js index 697528bd1..2108fb89d 100644 --- a/test/test.js +++ b/test/test.js @@ -717,7 +717,7 @@ deepEqual(actual, [1, 3]); }); - test('should work when used as `callback` for `_.map`', 1, function() { + test('should work when used as a callback for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.at); @@ -1132,7 +1132,7 @@ ok(actual != expected && actual[0] === expected[0]); }); - test('`_.clone` should perform a shallow clone when used as `callback` for `_.map`', 1, function() { + test('`_.clone` should perform a shallow clone when used as a callback for `_.map`', 1, function() { var expected = [{ 'a': [0] }, { 'b': [1] }], actual = _.map(expected, _.clone); @@ -2220,7 +2220,7 @@ deepEqual(func({}, new Foo), {}); }); - test('should work when used as `callback` for `_.reduce`', 1, function() { + test('should work when used as a callback for `_.reduce`', 1, function() { var array = [{ 'a': 1 }, { 'b': 2 }, { 'c': 3 }], actual = _.reduce(array, _.merge); @@ -2423,7 +2423,7 @@ strictEqual(_.first([]), undefined); }); - test('should work when used as `callback` for `_.map`', 1, function() { + test('should work when used as a callback for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.first); @@ -2554,7 +2554,7 @@ deepEqual(_.flatten(array, 'a'), [1, 2, 3]); }); - test('should perform a deep flatten when used as `callback` for `_.map`', 1, function() { + test('should perform a deep flatten when used as a callback for `_.map`', 1, function() { var array = [[[['a']]], [[['b']]]], actual = _.map(array, _.flatten); @@ -3410,7 +3410,7 @@ }); }); - test('should work when used as `callback` for `_.map`', 1, function() { + test('should work when used as a callback for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.initial); @@ -4226,7 +4226,7 @@ strictEqual(actual, false); }); - test('should work when used as `callback` for `_.every`', 1, function() { + test('should work when used as a callback for `_.every`', 1, function() { var actual = _.every([1, 1, 1], _.partial(_.isEqual, 1)); ok(actual); }); @@ -4943,7 +4943,7 @@ strictEqual(_.last([]), undefined); }); - test('should work when used as `callback` for `_.map`', 1, function() { + test('should work when used as a callback for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.last); @@ -5549,7 +5549,7 @@ equal(actual, methodName == 'max' ? 1 : 3); }); - test('`_.' + methodName + '` should work when used as `callback` for `_.map`', 1, function() { + test('`_.' + methodName + '` should work when used as a callback for `_.map`', 1, function() { var array = [[2, 3, 1], [5, 6, 4], [8, 9, 7]], actual = _.map(array, func); @@ -5803,6 +5803,22 @@ deepEqual(actual, expected); }); + + test('should coerce property names to strings', 1, function() { + deepEqual(_.omit({ '0': 'a' }, 0), {}); + }); + + test('should work when used as a callback for `_.map`', 2, function() { + var callback = _.partialRight(_.omit, 'a'), + objects = [{ '0': 0, 'a': 0 }, { '1': 1, 'a': 1 }], + actual = _.map(objects, callback), + expected = [{ '0': 0 }, { '1': 1 }]; + + deepEqual(actual, expected); + + actual = _.map({ '0': objects[0], '1': objects[1] }, callback); + deepEqual(actual, expected); + }); }('a', 'c')); /*--------------------------------------------------------------------------*/ @@ -6153,6 +6169,22 @@ deepEqual(actual, expected); }); + + test('should coerce property names to strings', 1, function() { + deepEqual(_.pick({ '0': 'a', '1': 'b' }, 0), { '0': 'a' }); + }); + + test('should work when used as a callback for `_.map`', 2, function() { + var callback = _.partialRight(_.pick, 'a'), + objects = [{ '0': 0, 'a': 0 }, { '1': 1, 'a': 1 }], + actual = _.map(objects, callback), + expected = [{ 'a': 0 }, { 'a': 1 }]; + + deepEqual(actual, expected); + + actual = _.map({ '0': objects[0], '1': objects[1] }, callback); + deepEqual(actual, expected); + }); }('a', 'c')); /*--------------------------------------------------------------------------*/ @@ -6741,7 +6773,7 @@ deepEqual(_.rest([]), []); }); - test('should work when used as `callback` for `_.map`', 1, function() { + test('should work when used as a callback for `_.map`', 1, function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.rest); @@ -6875,7 +6907,7 @@ ok(actual[0] !== actual[1] && _.contains(array, actual[0]) && _.contains(array, actual[1])); }); - test('should work when used as `callback` for `_.map`', 1, function() { + test('should work when used as a callback for `_.map`', 1, function() { var a = [1, 2, 3], b = [4, 5, 6], c = [7, 8, 9], @@ -8328,7 +8360,7 @@ deepEqual(actual, [1, 2, 3]); }); - test('should perform an unsorted uniq operation when used as `callback` for `_.map`', 1, function() { + test('should perform an unsorted uniq operation when used as a callback for `_.map`', 1, function() { var array = [[2, 1, 2], [1, 2, 1]], actual = _.map(array, _.uniq);