|
483 | 483 | '(hasOwnProperty.call(result, prop) ? result[prop]++ : result[prop] = 1)'
|
484 | 484 | };
|
485 | 485 |
|
| 486 | + /** Reusable iterator options for `drop` and `pick` */ |
| 487 | + var dropIteratorOptions = { |
| 488 | + 'useHas': false, |
| 489 | + 'args': 'object, callback, thisArg', |
| 490 | + 'init': '{}', |
| 491 | + 'top': |
| 492 | + 'var isFunc = typeof callback == \'function\';\n' + |
| 493 | + 'if (!isFunc) {\n' + |
| 494 | + ' var props = concat.apply(ArrayProto, arguments)\n' + |
| 495 | + '} else if (thisArg) {\n' + |
| 496 | + ' callback = iteratorBind(callback, thisArg)\n' + |
| 497 | + '}', |
| 498 | + 'inLoop': |
| 499 | + 'if (isFunc ? !callback(value, index, object) : indexOf(props, index) < 0) result[index] = value' |
| 500 | + }; |
| 501 | + |
486 | 502 | /** Reusable iterator options for `every` and `some` */
|
487 | 503 | var everyIteratorOptions = {
|
488 | 504 | 'init': 'true',
|
|
1104 | 1120 | /**
|
1105 | 1121 | * Creates a shallow clone of `object` excluding the specified properties.
|
1106 | 1122 | * Property names may be specified as individual arguments or as arrays of
|
1107 |
| - * property names. |
| 1123 | + * property names. If `callback` is passed, it will be executed for each property |
| 1124 | + * in the `object`, dropping the properties `callback` returns truthy for. The |
| 1125 | + * `callback` is bound to `thisArg` and invoked with 3 arguments; (value, key, object). |
1108 | 1126 | *
|
1109 | 1127 | * @static
|
1110 | 1128 | * @memberOf _
|
|
1118 | 1136 | * _.drop({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'userid');
|
1119 | 1137 | * // => { 'name': 'moe', 'age': 40 }
|
1120 | 1138 | */
|
1121 |
| - var drop = createIterator({ |
1122 |
| - 'useHas': false, |
1123 |
| - 'args': 'object', |
1124 |
| - 'init': '{}', |
1125 |
| - 'top': 'var props = concat.apply(ArrayProto, arguments)', |
1126 |
| - 'inLoop': 'if (indexOf(props, index) < 0) result[index] = value' |
1127 |
| - }); |
| 1139 | + var drop = createIterator(dropIteratorOptions); |
1128 | 1140 |
|
1129 | 1141 | /**
|
1130 | 1142 | * Assigns enumerable properties of the source object(s) to the `destination`
|
|
1763 | 1775 | /**
|
1764 | 1776 | * Creates a shallow clone of `object` composed of the specified properties.
|
1765 | 1777 | * Property names may be specified as individual arguments or as arrays of
|
1766 |
| - * property names. |
| 1778 | + * property names. If `callback` is passed, it will be executed for each property |
| 1779 | + * in the `object`, picking the properties `callback` returns truthy for. The |
| 1780 | + * `callback` is bound to `thisArg` and invoked with 3 arguments; (value, key, object). |
1767 | 1781 | *
|
1768 | 1782 | * @static
|
1769 | 1783 | * @memberOf _
|
1770 | 1784 | * @category Objects
|
1771 | 1785 | * @param {Object} object The source object.
|
1772 |
| - * @param {Object} [prop1, prop2, ...] The properties to pick. |
| 1786 | + * @param {Function|String} [callback|prop1, prop2, ...] The properties to pick |
| 1787 | + * or the function called per iteration. |
1773 | 1788 | * @returns {Object} Returns an object composed of the picked properties.
|
1774 | 1789 | * @example
|
1775 | 1790 | *
|
1776 | 1791 | * _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age');
|
1777 | 1792 | * // => { 'name': 'moe', 'age': 40 }
|
1778 | 1793 | */
|
1779 |
| - function pick(object) { |
1780 |
| - var result = {}; |
1781 |
| - if (!object) { |
1782 |
| - return result; |
1783 |
| - } |
1784 |
| - var prop, |
1785 |
| - index = 0, |
1786 |
| - props = concat.apply(ArrayProto, arguments), |
1787 |
| - length = props.length; |
1788 |
| - |
1789 |
| - // start `index` at `1` to skip `object` |
1790 |
| - while (++index < length) { |
1791 |
| - prop = props[index]; |
1792 |
| - if (prop in object) { |
1793 |
| - result[prop] = object[prop]; |
1794 |
| - } |
1795 |
| - } |
1796 |
| - return result; |
1797 |
| - } |
| 1794 | + var pick = createIterator(dropIteratorOptions, { |
| 1795 | + 'top': |
| 1796 | + 'if (typeof callback != \'function\') {\n' + |
| 1797 | + ' var prop,\n' + |
| 1798 | + ' props = concat.apply(ArrayProto, arguments),\n' + |
| 1799 | + ' length = props.length;\n' + |
| 1800 | + ' for (index = 1; index < length; index++) {\n' + |
| 1801 | + ' prop = props[index];\n' + |
| 1802 | + ' if (prop in object) result[prop] = object[prop]\n' + |
| 1803 | + ' }\n' + |
| 1804 | + '} else {\n' + |
| 1805 | + ' if (thisArg) callback = iteratorBind(callback, thisArg)', |
| 1806 | + 'inLoop': |
| 1807 | + 'if (callback(value, index, object)) result[index] = value', |
| 1808 | + 'bottom': '}' |
| 1809 | + }); |
1798 | 1810 |
|
1799 | 1811 | /**
|
1800 | 1812 | * Gets the size of `value` by returning `value.length` if `value` is an
|
|
0 commit comments