From 78cc8f8b6e7d8deab9ab3d26a057970b169b9987 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 18 Apr 2012 15:10:46 -0400 Subject: [PATCH] lodash: Use native Object.keys and Array.isArray. [jddalton] --- doc/README.md | 152 +++++++++++++++++++++++++------------------------- lodash.js | 13 +++-- 2 files changed, 83 insertions(+), 82 deletions(-) diff --git a/doc/README.md b/doc/README.md index 4ea59bbb5..a7eb44068 100644 --- a/doc/README.md +++ b/doc/README.md @@ -143,7 +143,7 @@ The Lodash function. -### `_.VERSION` +### `_.VERSION` *(String)*: The semantic version number. [▲][1] @@ -152,7 +152,7 @@ The Lodash function. -### `_.after(times, func)` +### `_.after(times, func)` Creates a new function that is restricted to executing only after it is called a given number of `times`. [▲][1] @@ -177,7 +177,7 @@ _.forEach(notes, function(note) { -### `_.bind(func [, arg1, arg2, ...])` +### `_.bind(func [, arg1, arg2, ...])` Creates a new function that, when called, invokes `func` with the `this` binding of `thisArg` and prepends additional arguments to those passed to the bound function. [▲][1] @@ -201,7 +201,7 @@ func(); -### `_.bindAll(object [, methodName1, methodName2, ...])` +### `_.bindAll(object [, methodName1, methodName2, ...])` Binds methods on the `object` to the object, overwriting the non-bound method. If no method names are provided, all the function properties of the `object` will be bound. [▲][1] @@ -230,7 +230,7 @@ jQuery('#lodash_button').on('click', buttonView.onClick); -### `_.chain(value)` +### `_.chain(value)` Wraps the value in a `lodash` chainable object. [▲][1] @@ -261,7 +261,7 @@ var youngest = _.chain(stooges) -### `_.clone(value)` +### `_.clone(value)` Create a shallow clone of the `value`. Any nested objects or arrays will be assigned by reference and not cloned. [▲][1] @@ -282,7 +282,7 @@ _.clone({ 'name': 'moe' }); -### `_.compact(array)` +### `_.compact(array)` Produces a new array with all falsey values of `array` removed. The values `false`, `null`, `0`, `""`, `undefined` and `NaN` are all falsey. [▲][1] @@ -303,7 +303,7 @@ _.compact([0, 1, false, 2, '', 3]); -### `_.compose([func1, func2, ...])` +### `_.compose([func1, func2, ...])` Creates a new function that is the composition of the passed functions, where each function consumes the return value of the function that follows. In math terms, composing thefunctions `f()`, `g()`, and `h()` produces `f(g(h()))`. [▲][1] @@ -349,7 +349,7 @@ _.contains([1, 2, 3], 3); -### `_.debounce(func, wait, immediate)` +### `_.debounce(func, wait, immediate)` Creates a new function that will postpone its execution until after `wait` milliseconds have elapsed since the last time it was invoked. Pass `true` for `immediate` to cause debounce to invoke the function on the leading, intead of the trailing, edge of the wait timeout. [▲][1] @@ -372,7 +372,7 @@ jQuery(window).on('resize', lazyLayout); -### `_.defaults(object [, defaults1, defaults2, ..])` +### `_.defaults(object [, defaults1, defaults2, ..])` Assigns missing properties in `object` with default values from the defaults objects. As soon as a property is set, additional defaults of the same property will be ignored. [▲][1] @@ -395,7 +395,7 @@ _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'lots' }); -### `_.defer(func [, arg1, arg2, ...])` +### `_.defer(func [, arg1, arg2, ...])` Defers invoking the `func` function until the current call stack has cleared. Additional arguments are passed to `func` when it is invoked. [▲][1] @@ -417,7 +417,7 @@ _.defer(function() { alert('deferred'); }); -### `_.delay(func, wait [, arg1, arg2, ...])` +### `_.delay(func, wait [, arg1, arg2, ...])` Invokes the `func` function after `wait` milliseconds. Additional arguments are passed `func` when it is invoked. [▲][1] @@ -441,7 +441,7 @@ _.delay(log, 1000, 'logged later'); -### `_.difference(array [, array1, array2, ...])` +### `_.difference(array [, array1, array2, ...])` Produces a new array of `array` values not present in the other arrays using strict equality for comparisons, i.e. `===`. [▲][1] @@ -463,7 +463,7 @@ _.difference([1, 2, 3, 4, 5], [5, 2, 10]); -### `_.escape(string)` +### `_.escape(string)` Escapes a string for insertion into HTML, replacing `&`, `<`, `>`, `"`, `'`, and `/` characters. [▲][1] @@ -507,7 +507,7 @@ _.every([true, 1, null, 'yes'], Boolean); -### `_.extend(destination [, source1, source2, ..])` +### `_.extend(destination [, source1, source2, ..])` Copies enumerable properties from the source objects to the `destination` object. Subsequent sources will overwrite propery assignments of previous sources. [▲][1] @@ -575,7 +575,7 @@ var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); -### `_.first(array [, n, guard])` +### `_.first(array [, n, guard])` Gets the first value of the `array`. Pass `n` to return the first `n` values of the `array`. [▲][1] @@ -598,7 +598,7 @@ _.first([5, 4, 3, 2, 1]); -### `_.flatten(array, shallow)` +### `_.flatten(array, shallow)` Flattens a nested array *(the nesting can be to any depth)*. If `shallow` is truthy, `array` will only be flattened a single level. [▲][1] @@ -649,7 +649,7 @@ _.forEach({ 'one': 1, 'two': 2, 'three': 3}, function(num) { alert(num); }); -### `_.functions(object)` +### `_.functions(object)` Produces a sorted array of the `object`'s enumerable own property names that have function values. [▲][1] @@ -696,7 +696,7 @@ _.groupBy(['one', 'two', 'three'], 'length'); -### `_.has(object, key)` +### `_.has(object, key)` Checks if an object has the specified key as a direct property. [▲][1] @@ -718,7 +718,7 @@ _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b'); -### `_.identity(value)` +### `_.identity(value)` This function simply returns the first argument passed to it. Note: It is used throughout Lodash as a default callback. [▲][1] @@ -740,7 +740,7 @@ moe === _.identity(moe); -### `_.indexOf(array, value [, isSorted=false])` +### `_.indexOf(array, value [, isSorted=false])` Gets the index at which the first occurrence of `value` is found using strict equality for comparisons, i.e. `===`. If the `array` is already sorted, passing `true` for `isSorted` will run a faster binary search. [▲][1] @@ -763,7 +763,7 @@ _.indexOf([1, 2, 3], 2); -### `_.initial(array [, n, guard])` +### `_.initial(array [, n, guard])` Gets all but the last value of the `array`. Pass `n` to exclude the last `n` values from the result. [▲][1] @@ -786,7 +786,7 @@ _.initial([5, 4, 3, 2, 1]); -### `_.intersection([array1, array2, ...])` +### `_.intersection([array1, array2, ...])` Computes the intersection of all the passed-in arrays. [▲][1] @@ -830,7 +830,7 @@ _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); -### `_.isArguments(value)` +### `_.isArguments(value)` Checks if a `value` is an `arguments` object. [▲][1] @@ -854,7 +854,7 @@ _.isArguments([1, 2, 3]); -### `_.isArray(value)` +### `_.isArray(value)` Checks if a `value` is an array. [▲][1] @@ -878,7 +878,7 @@ _.isArray([1, 2, 3]); -### `_.isBoolean(value)` +### `_.isBoolean(value)` Checks if a `value` is a boolean *(`true` or `false`)* value. [▲][1] @@ -899,7 +899,7 @@ _.isBoolean(null); -### `_.isDate(value)` +### `_.isDate(value)` Checks if a `value` is a date. [▲][1] @@ -920,7 +920,7 @@ _.isDate(new Date); -### `_.isElement(value)` +### `_.isElement(value)` Checks if a `value` is a DOM element. [▲][1] @@ -941,7 +941,7 @@ _.isElement(document.body); -### `_.isEmpty(value)` +### `_.isEmpty(value)` Checks if a `value` is empty. Arrays or strings with a length of `0` and objects with no enumerable own properties are considered "empty". [▲][1] @@ -965,7 +965,7 @@ _.isEmpty({}); -### `_.isEqual(a, b)` +### `_.isEqual(a, b)` Performs a deep comparison between two values to determine if they are equivalent to each other. [▲][1] @@ -993,7 +993,7 @@ _.isEqual(moe, clone); -### `_.isFinite(value)` +### `_.isFinite(value)` Checks if a `value` is a finite number. [▲][1] @@ -1020,7 +1020,7 @@ _.isFinite(Infinity); -### `_.isFunction(value)` +### `_.isFunction(value)` Checks if a `value` is a function. [▲][1] @@ -1041,7 +1041,7 @@ _.isFunction(''.concat); -### `_.isNaN(value)` +### `_.isNaN(value)` Checks if a `value` is `NaN`. Note: this is not the same as native `isNaN`, which will return true for `undefined` and other values. See http://es5.github.com/#x15.1.2.4. [▲][1] @@ -1071,7 +1071,7 @@ _.isNaN(undefined); -### `_.isNull(value)` +### `_.isNull(value)` Checks if a `value` is `null`. [▲][1] @@ -1095,7 +1095,7 @@ _.isNull(undefined); -### `_.isNumber(value)` +### `_.isNumber(value)` Checks if a `value` is a number. [▲][1] @@ -1116,7 +1116,7 @@ _.isNumber(8.4 * 5; -### `_.isObject(value)` +### `_.isObject(value)` Checks if a `value` is an object. [▲][1] @@ -1140,7 +1140,7 @@ _.isObject(1); -### `_.isRegExp(value)` +### `_.isRegExp(value)` Checks if a `value` is a regular expression. [▲][1] @@ -1161,7 +1161,7 @@ _.isRegExp(/moe/); -### `_.isString(value)` +### `_.isString(value)` Checks if a `value` is a string. [▲][1] @@ -1182,7 +1182,7 @@ _.isString('moe'); -### `_.isUndefined(value)` +### `_.isUndefined(value)` Checks if a `value` is `undefined`. [▲][1] @@ -1203,7 +1203,7 @@ _.isUndefined(void 0); -### `_.keys(object)` +### `_.keys(object)` Produces an array of the `object`'s enumerable own property names. [▲][1] @@ -1224,7 +1224,7 @@ _.keys({ 'one': 1, 'two': 2, 'three': 3 }); -### `_.last(array [, n, guard])` +### `_.last(array [, n, guard])` Gets the last value of the `array`. Pass `n` to return the lasy `n` values of the `array`. [▲][1] @@ -1247,7 +1247,7 @@ _.last([5, 4, 3, 2, 1]); -### `_.lastIndexOf(array, value)` +### `_.lastIndexOf(array, value)` Gets the index at which the last occurrence of `value` is found using strict equality for comparisons, i.e. `===`. [▲][1] @@ -1295,7 +1295,7 @@ _.map({ 'one': 1, 'two': 2, 'three': 3 }, function(num) { return num * 3; }); -### `_.max(collection [, callback, thisArg])` +### `_.max(collection [, callback, thisArg])` Retrieves the maximum value of a `collection`. If `callback` is passed, it will be executed for each value in the `collection` to generate the criterion by which the value is ranked. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. [▲][1] @@ -1324,7 +1324,7 @@ _.max(stooges, function(stooge) { return stooge.age; }); -### `_.memoize(func [, hasher=_.identity])` +### `_.memoize(func [, hasher=_.identity])` Creates a new function that memoizes the result of `func`. If `hasher` is passed, it will be used to compute the hash key for storing the result, based on the arguments to the original function. The default `hasher` uses the first argument to the memoized function as the cache key. [▲][1] @@ -1347,7 +1347,7 @@ var fibonacci = _.memoize(function(n) { -### `_.min(collection [, callback, thisArg])` +### `_.min(collection [, callback, thisArg])` Retrieves the minimum value of a `collection`. If `callback` is passed, it will be executed for each value in the `collection` to generate the criterion by which the value is ranked. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. [▲][1] @@ -1370,7 +1370,7 @@ _.min([10, 5, 100, 2, 1000]); -### `_.mixin(object)` +### `_.mixin(object)` Adds functions properties of `object` to the `lodash` function and chainable wrapper. [▲][1] @@ -1397,7 +1397,7 @@ _('larry').capitalize(); -### `_.noConflict()` +### `_.noConflict()` Reverts the '_' variable to its previous value and returns a reference to the `lodash` function. [▲][1] @@ -1414,7 +1414,7 @@ var lodash = _.noConflict(); -### `_.once(func)` +### `_.once(func)` Creates a new function that is restricted to one execution. Repeat calls to the function will return the value of the first call. [▲][1] @@ -1437,7 +1437,7 @@ initialize(); -### `_.pick(object [, prop1, prop2, ..])` +### `_.pick(object [, prop1, prop2, ..])` Creates an object composed of the specified properties. Property names may be specified as individual arguments or as arrays of property names. [▲][1] @@ -1459,7 +1459,7 @@ _.pick({ 'name': 'moe', 'age': 40, 'userid': 'moe1' }, 'name', 'age'); -### `_.pluck(collection, property)` +### `_.pluck(collection, property)` Retrieves the value of a specified property from all values in a `collection`. [▲][1] @@ -1487,7 +1487,7 @@ _.pluck(stooges, 'name'); -### `_.range([start=0], end [, step=1])` +### `_.range([start=0], end [, step=1])` Creates an array of numbers *(positive and/or negative)* progressing from `start` up to but not including `stop`. This method is a port of Python's `range()` function. See http://docs.python.org/library/functions.html#range. [▲][1] @@ -1522,7 +1522,7 @@ _.range(0); -### `_.reduce(collection, callback [, accumulator, thisArg])` +### `_.reduce(collection, callback [, accumulator, thisArg])` Boils down a `collection` to a single value. The initial state of the reduction is `accumulator` and each successive step of it should be returned by the `callback`. The `callback` is bound to the `thisArg` value, if one is passed. The `callback` is invoked with `4` arguments; for arrays they are *(accumulator, value, index, array)* and for objects they are *(accumulator, value, key, object)*. [▲][1] @@ -1546,7 +1546,7 @@ var sum = _.reduce([1, 2, 3], function(memo, num) { return memo + num; }); -### `_.reduceRight(collection, callback [, accumulator, thisArg])` +### `_.reduceRight(collection, callback [, accumulator, thisArg])` The right-associative version of `_.reduce`. The `callback` is bound to the `thisArg` value, if one is passed. The `callback` is invoked with `4` arguments; for arrays they are *(accumulator, value, index, array)* and for objects they are *(accumulator, value, key, object)*. [▲][1] @@ -1571,7 +1571,7 @@ var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []); -### `_.reject(collection, callback [, thisArg])` +### `_.reject(collection, callback [, thisArg])` The opposite of `_.filter`, this method returns the values of a `collection` that `callback` does **not** return truthy for. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. [▲][1] @@ -1594,7 +1594,7 @@ var odds = _.reject([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); -### `_.rest(array [, n, guard])` +### `_.rest(array [, n, guard])` The opposite of `_.initial`, this method gets all but the first value of the `array`. Pass `n` to exclude the first `n` values from the result. [▲][1] @@ -1617,7 +1617,7 @@ _.rest([5, 4, 3, 2, 1]); -### `_.result(object, property)` +### `_.result(object, property)` Resolves the value of `property` on `object`. If the property is a function it will be invoked and its result returned, else the property value is returned. [▲][1] @@ -1649,7 +1649,7 @@ _.result(object, 'stuff'); -### `_.shuffle(collection)` +### `_.shuffle(collection)` Produces a new array of shuffled `collection` values, using a version of the Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. [▲][1] @@ -1670,7 +1670,7 @@ _.shuffle([1, 2, 3, 4, 5, 6]); -### `_.size(collection)` +### `_.size(collection)` Gets the number of values in the `collection`. [▲][1] @@ -1691,7 +1691,7 @@ _.size({ 'one': 1, 'two': 2, 'three': 3 }); -### `_.some(collection, callback [, thisArg])` +### `_.some(collection, callback [, thisArg])` Checks if the `callback` returns truthy for **any** value of a `collection`. The function returns as soon as it finds passing value, and does not iterate over the entire `collection`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. [▲][1] @@ -1714,7 +1714,7 @@ _.some([null, 0, 'yes', false]); -### `_.sortBy(collection, callback [, thisArg])` +### `_.sortBy(collection, callback [, thisArg])` Produces a new sorted array, ranked in ascending order by the results of running each value of a `collection` through `callback`. The `callback` is invoked with `3` arguments; for arrays they are *(value, index, array)* and for objects they are *(value, key, object)*. The `callback` argument may also be the name of a property to sort by *(e.g. 'length')*. [▲][1] @@ -1737,7 +1737,7 @@ _.sortBy([1, 2, 3, 4, 5, 6], function(num) { return Math.sin(num); }); -### `_.sortedIndex(array, value [, callback])` +### `_.sortedIndex(array, value [, callback])` Uses a binary search to determine the index at which the `value` should be inserted into the `collection` in order to maintain the `collection`'s sorted order. If `callback` is passed, it will be executed for each value in the `collection` to compute their sort ranking. The `callback` is invoked with `1` arguments. [▲][1] @@ -1760,7 +1760,7 @@ _.sortedIndex([10, 20, 30, 40, 50], 35); -### `_.tap(value, interceptor)` +### `_.tap(value, interceptor)` Invokes `interceptor` with the `value` as the first argument, and then returns `value`. The primary purpose of this method is to "tap into" a method chain, in order to performoperations on intermediate results within the chain. [▲][1] @@ -1787,7 +1787,7 @@ _.chain([1,2,3,200]) -### `_.template(text, data, settings)` +### `_.template(text, data, settings)` JavaScript micro-templating, similar to John Resig's implementation. Lo-Dash templating handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. [▲][1] @@ -1842,7 +1842,7 @@ _.template('<%= data.hasWith %>', { 'hasWith': 'no' }, { 'variable': 'data' }); -### `_.throttle(func, wait)` +### `_.throttle(func, wait)` Creates a new function that, when invoked, will only call the original function at most once per every `wait` milliseconds. [▲][1] @@ -1864,7 +1864,7 @@ jQuery(window).on('scroll', throttled); -### `_.times(n, callback [, thisArg])` +### `_.times(n, callback [, thisArg])` Executes the `callback` function `n` times. [▲][1] @@ -1883,7 +1883,7 @@ _.times(3, function() { genie.grantWish(); }); -### `_.toArray(collection)` +### `_.toArray(collection)` Converts the `collection`, into an array. Useful for converting the `arguments` object. [▲][1] @@ -1904,7 +1904,7 @@ Converts the `collection`, into an array. Useful for converting the `arguments` -### `_.union([array1, array2, ...])` +### `_.union([array1, array2, ...])` Computes the union of the passed-in arrays. [▲][1] @@ -1925,7 +1925,7 @@ _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]); -### `_.uniq(array [, isSorted=false, callback])` +### `_.uniq(array [, isSorted=false, callback])` Produces a duplicate-value-free version of the `array` using strict equality for comparisons, i.e. `===`. If the `array` is already sorted, passing `true` for `isSorted` will run a faster algorithm. If `callback` is passed, each value of `array` is passed through a transformation `callback` before uniqueness is computed. The `callback` is invoked with `3` arguments; *(value, index, array)*. [▲][1] @@ -1948,7 +1948,7 @@ _.uniq([1, 2, 1, 3, 1, 4]); -### `_.uniqueId([prefix])` +### `_.uniqueId([prefix])` Generates a unique id. If `prefix` is passed, the id will be appended to it. [▲][1] @@ -1969,7 +1969,7 @@ _.uniqueId('contact_'); -### `_.values(object)` +### `_.values(object)` Produces an array of the `object`'s enumerable own property values. [▲][1] @@ -1990,7 +1990,7 @@ _.values({ 'one': 1, 'two': 2, 'three': 3 }); -### `_.without(array [, value1, value2, ...])` +### `_.without(array [, value1, value2, ...])` Produces a new array with all occurrences of the values removed using strict equality for comparisons, i.e. `===`. [▲][1] @@ -2012,7 +2012,7 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); -### `_.wrap(func, wrapper [, arg1, arg2, ...])` +### `_.wrap(func, wrapper [, arg1, arg2, ...])` Create a new function that passes the `func` function to the `wrapper` function as its first argument. Additional arguments are appended to those passed to the `wrapper` function. [▲][1] @@ -2039,7 +2039,7 @@ hello(); -### `_.zip([array1, array2, ...])` +### `_.zip([array1, array2, ...])` Merges together the values of each of the arrays with the value at the corresponding position. Useful for separate data sources that are coordinated through matching array indexes. For a matrix of nested arrays, `_.zip.apply(...)` can transpose the matrix in a similar fashion. [▲][1] @@ -2070,7 +2070,7 @@ The Lodash function. -### `_#chain()` +### `_#chain()` Extracts the value from a wrapped chainable object. [▲][1] @@ -2088,7 +2088,7 @@ _([1, 2, 3]).value(); -### `_#value()` +### `_#value()` Extracts the value from a wrapped chainable object. [▲][1] @@ -2113,7 +2113,7 @@ _([1, 2, 3]).value(); -### `_.templateSettings` +### `_.templateSettings` *(Object)*: By default, Lodash uses ERB-style template delimiters, change the following template settings to use alternative delimiters. [▲][1] diff --git a/lodash.js b/lodash.js index 1eccd01dc..08a565e7c 100644 --- a/lodash.js +++ b/lodash.js @@ -394,6 +394,9 @@ */ var map = iterationFactory({ 'top': 'var result=[]', + 'beforeLoop': { + 'array': 'result=Array(length)' + }, 'inLoop': { 'array': 'result[index]=callback(collection[index],index,collection)', 'object': 'result[result.length]=callback(collection[index],index,collection)' @@ -2490,16 +2493,14 @@ /*--------------------------------------------------------------------------*/ - /* - _.keys = nativeKeys || + keys = nativeKeys || keys; + isArray = nativeIsArray || isArray; - _.isArray = nativeIsArray || function(collection) { if (!isArguments(arguments)) { - _.isArguments = function(collection) { - return !!(collection && hasOwnProperty.call(collection, 'callee')); + isArguments = function(value) { + return !!(value && hasOwnProperty.call(value, 'callee')); }; } - */ /*--------------------------------------------------------------------------*/