Add _.partialRight and make _.assign and _.defaults work with arrays.

Former-commit-id: 6d9fea855de53e9ccb5ac6f58db68239ef08e9de
This commit is contained in:
John-David Dalton
2013-01-19 22:17:14 -08:00
parent 39fc839ff2
commit 873cc63f94
9 changed files with 419 additions and 371 deletions

View File

@@ -1,10 +1,10 @@
/**
* @license
* Lo-Dash 1.0.0-rc.3 (Custom Build) <http://lodash.com>
* Lo-Dash 1.0.0-rc.3 (Custom Build) <http://lodash.com/>
* Build: `lodash underscore -d -o ./lodash.underscore.js`
* (c) 2012 John-David Dalton <http://allyoucanleet.com/>
* Based on Underscore.js 1.4.3 <http://underscorejs.org>
* (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.4.3 <http://underscorejs.org/>
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
* Available under MIT license <http://lodash.com/license>
*/
;(function(window, undefined) {
@@ -192,10 +192,10 @@
* `concat`, `countBy`, `debounce`, `defaults`, `defer`, `delay`, `difference`,
* `filter`, `flatten`, `forEach`, `forIn`, `forOwn`, `functions`, `groupBy`,
* `initial`, `intersection`, `invert`, `invoke`, `keys`, `map`, `max`, `memoize`,
* `merge`, `min`, `object`, `omit`, `once`, `pairs`, `partial`, `pick`, `pluck`,
* `push`, `range`, `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
* `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`,
* `unshift`, `values`, `where`, `without`, `wrap`, and `zip`
* `merge`, `min`, `object`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
* `pick`, `pluck`, `push`, `range`, `reject`, `rest`, `reverse`, `shuffle`,
* `slice`, `sort`, `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`,
* `union`, `uniq`, `unshift`, `values`, `where`, `without`, `wrap`, and `zip`
*
* The non-chainable wrapper functions are:
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`, `identity`,
@@ -272,18 +272,6 @@
/*--------------------------------------------------------------------------*/
/** Reusable iterator options for `assign` and `defaults` */
var assignIteratorOptions = {
'args': 'object, source, guard',
'top':
'var argsIndex = 0,\n' +
" argsLength = typeof guard == 'number' ? 2 : arguments.length;\n" +
'while (++argsIndex < argsLength) {\n' +
' if ((iteratee = arguments[argsIndex])) {',
'loop': 'result[index] = iteratee[index]',
'bottom': ' }\n}'
};
/** Reusable iterator options shared by `each`, `forIn`, and `forOwn` */
var eachIteratorOptions = {
'arrays': true,
@@ -341,14 +329,15 @@
}
/**
* Creates a function that, when called, invokes `func` with the `this`
* binding of `thisArg` and prepends any `partailArgs` to the arguments passed
* to the bound function.
* Creates a function that, when called, invokes `func` with the `this` binding
* of `thisArg` and prepends any `partailArgs` to the arguments passed to the
* bound function.
*
* @private
* @param {Function|String} func The function to bind or the method name.
* @param {Mixed} [thisArg] The `this` binding of `func`.
* @param {Array} partialArgs An array of arguments to be partially applied.
* @param {Object} [right] Used to indicate partially applying arguments from the right.
* @returns {Function} Returns the new bound function.
*/
function createBound(func, thisArg, partialArgs) {
@@ -596,38 +585,6 @@
}
/*--------------------------------------------------------------------------*/
/**
* Assigns own enumerable properties of source object(s) to the `destination`
* object. Subsequent sources will overwrite propery assignments of previous
* sources.
*
* @static
* @memberOf _
* @alias extend
* @category Objects
* @param {Object} object The destination object.
* @param {Object} [source1, source2, ...] The source objects.
* @returns {Object} Returns the destination object.
* @example
*
* _.assign({ 'name': 'moe' }, { 'age': 40 });
* // => { 'name': 'moe', 'age': 40 }
*/
function assign(object) {
if (!object) {
return object;
}
for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
var iteratee = arguments[argsIndex];
if (iteratee) {
for (var key in iteratee) {
object[key] = iteratee[key];
}
}
}
return object;
}
/**
* Checks if `value` is an `arguments` object.
*
@@ -811,6 +768,38 @@
/*--------------------------------------------------------------------------*/
/**
* Assigns own enumerable properties of source object(s) to the `destination`
* object. Subsequent sources will overwrite propery assignments of previous
* sources.
*
* @static
* @memberOf _
* @alias extend
* @category Objects
* @param {Object} object The destination object.
* @param {Object} [source1, source2, ...] The source objects.
* @param- {Object} [guard] Internally used to allow this method to work with
* `_.reduce` without using its callback's `key and `object` arguments as sources.
* @returns {Object} Returns the destination object.
* @example
*
* _.assign({ 'name': 'moe' }, { 'age': 40 });
* // => { 'name': 'moe', 'age': 40 }
*/
function assign(object, source, guard) {
var args = arguments,
index = 0,
length = typeof guard == 'number' ? 2 : args.length;
while (++index < length) {
(isArray(args[index]) ? forEach : forOwn)(args[index], function(value, key) {
object[key] = value;
});
}
return object;
}
/**
* Creates a clone of `value`. If `deep` is `true`, nested objects will also
* be cloned, otherwise they will be assigned by reference.
@@ -859,6 +848,8 @@
* @category Objects
* @param {Object} object The destination object.
* @param {Object} [source1, source2, ...] The source objects.
* @param- {Object} [guard] Internally used to allow this method to work with
* `_.reduce` without using its callback's `key` and `object` arguments as sources.
* @returns {Object} Returns the destination object.
* @example
*
@@ -866,19 +857,17 @@
* _.defaults(iceCream, { 'flavor': 'vanilla', 'sprinkles': 'rainbow' });
* // => { 'flavor': 'chocolate', 'sprinkles': 'rainbow' }
*/
function defaults(object) {
if (!object) {
return object;
}
for (var argsIndex = 1, argsLength = arguments.length; argsIndex < argsLength; argsIndex++) {
var iteratee = arguments[argsIndex];
if (iteratee) {
for (var key in iteratee) {
if (object[key] == null) {
object[key] = iteratee[key];
}
function defaults(object, source, guard) {
var args = arguments,
index = 0,
length = typeof guard == 'number' ? 2 : args.length;
while (++index < length) {
(isArray(args[index]) ? forEach : forOwn)(args[index], function(value, key) {
if (object[key] == null) {
object[key] = value;
}
}
});
}
return object;
}
@@ -2043,7 +2032,8 @@
}
/**
* The right-associative version of `_.reduce`.
* This method is similar to `_.reduce`, except that it iterates over a
* `collection` from right to left.
*
* @static
* @memberOf _
@@ -2677,8 +2667,7 @@
/**
* Creates an array of numbers (positive and/or negative) progressing from
* `start` up to but not including `end`. This method is a port of Python's
* `range()` function. See http://docs.python.org/library/functions.html#range.
* `start` up to but not including `end`.
*
* @static
* @memberOf _