From fbdda6df03cf57c39f59ed01e35077ca9a6b9ab4 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 27 May 2016 15:26:21 -0700 Subject: [PATCH] Add `overArg` helper. --- lodash.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lodash.js b/lodash.js index 98c345fb5..cbd4cf884 100644 --- a/lodash.js +++ b/lodash.js @@ -1083,6 +1083,20 @@ return result; } + /** + * Creates a function that invokes `func` with its first argument transformed. + * + * @private + * @param {Function} func The function to wrap. + * @param {Function} transform The argument transform. + * @returns {Function} Returns the new function. + */ + function overArg(func, transform) { + return function(arg) { + return func(transform(arg)); + }; + } + /** * Replaces all `placeholder` elements in `array` with an internal placeholder * and returns an array of their indexes. @@ -1278,7 +1292,6 @@ Symbol = context.Symbol, Uint8Array = context.Uint8Array, enumerate = Reflect ? Reflect.enumerate : undefined, - getOwnPropertySymbols = Object.getOwnPropertySymbols, iteratorSymbol = typeof (iteratorSymbol = Symbol && Symbol.iterator) == 'symbol' ? iteratorSymbol : undefined, objectCreate = Object.create, propertyIsEnumerable = objectProto.propertyIsEnumerable, @@ -1291,6 +1304,7 @@ var nativeCeil = Math.ceil, nativeFloor = Math.floor, nativeGetPrototype = Object.getPrototypeOf, + nativeGetSymbols = Object.getOwnPropertySymbols, nativeIsFinite = context.isFinite, nativeJoin = arrayProto.join, nativeKeys = Object.keys, @@ -3066,9 +3080,7 @@ * @param {Object} object The object to query. * @returns {Array} Returns the array of property names. */ - function baseKeys(object) { - return nativeKeys(Object(object)); - } + var baseKeys = overArg(nativeKeys, Object); /** * The base implementation of `_.keysIn` which doesn't skip the constructor @@ -5450,9 +5462,7 @@ * @param {*} value The value to query. * @returns {null|Object} Returns the `[[Prototype]]`. */ - function getPrototype(value) { - return nativeGetPrototype(Object(value)); - } + var getPrototype = overArg(nativeGetPrototype, Object); /** * Creates an array of the own enumerable symbol properties of `object`. @@ -5461,11 +5471,7 @@ * @param {Object} object The object to query. * @returns {Array} Returns the array of symbols. */ - var getSymbols = !getOwnPropertySymbols ? stubArray : function(object) { - // Coerce `object` to an object to avoid non-object errors in V8. - // See https://bugs.chromium.org/p/v8/issues/detail?id=3443 for more details. - return getOwnPropertySymbols(Object(object)); - }; + var getSymbols = nativeGetSymbols ? overArg(nativeGetSymbols, Object) : stubArray; /** * Creates an array of the own and inherited enumerable symbol properties @@ -5475,7 +5481,7 @@ * @param {Object} object The object to query. * @returns {Array} Returns the array of symbols. */ - var getSymbolsIn = !getOwnPropertySymbols ? getSymbols : function(object) { + var getSymbolsIn = !nativeGetSymbols ? getSymbols : function(object) { var result = []; while (object) { arrayPush(result, getSymbols(object)); @@ -9919,8 +9925,7 @@ } /** - * Creates a function that invokes `func` with arguments transformed by - * corresponding `transforms`. + * Creates a function that invokes `func` with its arguments transformed. * * @static * @since 4.0.0