Add _.constant, _.mapValues, and _.xor.

This commit is contained in:
John-David Dalton
2013-11-21 09:31:55 -08:00
parent f74b206864
commit 33e5d46c87
9 changed files with 939 additions and 394 deletions

174
lodash.js
View File

@@ -2501,15 +2501,15 @@
* @memberOf _
* @category Objects
* @param {Object} object The object to inspect.
* @param {string} prop The name of the property to check.
* @param {string} key The name of the property to check.
* @returns {boolean} Returns `true` if key is a direct property, else `false`.
* @example
*
* _.has({ 'a': 1, 'b': 2, 'c': 3 }, 'b');
* // => true
*/
function has(object, prop) {
return object ? hasOwnProperty.call(object, prop) : false;
function has(object, key) {
return object ? hasOwnProperty.call(object, key) : false;
}
/**
@@ -2914,6 +2914,52 @@
return typeof value == 'undefined';
}
/**
* Creates an object with the same keys as `object` and values generated by
* running each own enumerable property of `object` through the callback.
* The callback is bound to `thisArg` and invoked with three arguments;
* (value, key, object).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Objects
* @param {Object} object The object to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new object with values of the results of each `callback` execution.
* @example
*
* _.mapValues({ 'a': 1, 'b': 2, 'c': 3} , function(num) { return num * 3; });
* // => { 'a': 3, 'b': 6, 'c': 9 }
*
* var characters = {
* 'fred': { 'name': 'fred', 'age': 40 },
* 'pebbles': { 'name': 'pebbles', 'age': 1 }
* };
*
* // using "_.pluck" callback shorthand
* _.mapValues(characters, 'age');
* // => { 'fred': 40, 'pebbles': 1 }
*/
function mapValues(object, callback, thisArg) {
var result = {};
callback = lodash.createCallback(callback, thisArg, 3);
forOwn(object, function(value, key, object) {
result[key] = callback(value, key, object);
});
return result;
}
/**
* Recursively merges own enumerable properties of the source object(s), that
* don't resolve to `undefined` into the destination object. Subsequent sources
@@ -3129,11 +3175,11 @@
/**
* An alternative to `_.reduce` this method transforms `object` to a new
* `accumulator` object which is the result of running each of its elements
* through a callback, with each callback execution potentially mutating
* the `accumulator` object. The callback is bound to `thisArg` and invoked
* with four arguments; (accumulator, value, key, object). Callbacks may exit
* iteration early by explicitly returning `false`.
* `accumulator` object which is the result of running each of its own
* enumerable properties through a callback, with each callback execution
* potentially mutating the `accumulator` object. The callback is bound to
* `thisArg` and invoked with four arguments; (accumulator, value, key, object).
* Callbacks may exit iteration early by explicitly returning `false`.
*
* @static
* @memberOf _
@@ -4758,29 +4804,34 @@
* @memberOf _
* @category Arrays
* @param {...Array} [array] The arrays to inspect.
* @returns {Array} Returns an array of composite values.
* @returns {Array} Returns an array of shared values.
* @example
*
* _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
* _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
* // => [1, 2]
*/
function intersection(array) {
var args = arguments,
argsLength = args.length,
function intersection() {
var args = [],
argsIndex = -1,
argsLength = arguments.length,
caches = getArray(),
index = -1,
indexOf = getIndexOf(),
length = array ? array.length : 0,
result = [],
trustIndexOf = indexOf === baseIndexOf,
seen = getArray();
while (++argsIndex < argsLength) {
var value = args[argsIndex];
caches[argsIndex] = indexOf === baseIndexOf &&
(value ? value.length : 0) >= largeArraySize &&
createCache(argsIndex ? args[argsIndex] : seen);
var value = arguments[argsIndex];
if (isArray(value) || isArguments(value)) {
args.push(value);
caches.push(trustIndexOf && value.length >= largeArraySize &&
createCache(argsIndex ? args[argsIndex] : seen));
}
}
var array = args[0],
index = -1,
length = array ? array.length : 0,
result = [];
outer:
while (++index < length) {
var cache = caches[0];
@@ -5197,13 +5248,13 @@
* @memberOf _
* @category Arrays
* @param {...Array} [array] The arrays to inspect.
* @returns {Array} Returns an array of composite values.
* @returns {Array} Returns an array of combined values.
* @example
*
* _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
* // => [1, 2, 3, 101, 10]
* _.union([1, 2, 3], [5, 2, 1, 4], [2, 1]);
* // => [1, 2, 3, 5, 4]
*/
function union(array) {
function union() {
return baseUniq(baseFlatten(arguments, true, true));
}
@@ -5283,6 +5334,37 @@
return baseDifference(array, slice(arguments, 1));
}
/**
* Creates an array that is the smymetric difference of the provided arrays.
* See http://en.wikipedia.org/wiki/Symmetric_difference.
*
* @static
* @memberOf _
* @category Arrays
* @param {...Array} [array] The arrays to inspect.
* @returns {Array} Returns an array of values.
* @example
*
* _.xor([1, 2, 3], [5, 2, 1, 4]);
* // => [3, 5, 4]
*
* _.xor([1, 2, 5], [2, 3, 5], [3, 4, 5]);
* // => [1, 4, 5]
*/
function xor() {
var index = 0,
length = arguments.length,
result = arguments[0] || [];
while (++index < length) {
var array = arguments[index];
if (isArray(array) || isArguments(array)) {
result = baseUniq(baseDifference(result, array).concat(baseDifference(array, result)));
}
}
return result;
}
/**
* Creates an array of grouped elements, the first of which contains the first
* elements of the given arrays, the second of which contains the second
@@ -5989,6 +6071,27 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a function that returns `value`.
*
* @static
* @memberOf _
* @category Utilities
* @param {*} value The value to return from the new function.
* @returns {Function} Returns the new function.
* @example
*
* var object = { 'name': 'fred' };
* var getter = _.constant(object);
* getter() === object;
* // => true
*/
function constant(value) {
return function() {
return value;
};
}
/**
* Produces a callback bound to an optional `thisArg`. If `func` is a property
* name the created callback will return the property value for a given element.
@@ -6218,14 +6321,14 @@
};
/**
* Creates a "_.pluck" style function, which returns the `prop` value of a
* Creates a "_.pluck" style function, which returns the `key` value of a
* given object.
*
* @static
* @memberOf _
* @category Utilities
* @param {string} prop The name of the property to retrieve.
* @returns {*} Returns the new function.
* @param {string} key The name of the property to retrieve.
* @returns {Function} Returns the new function.
* @example
*
* var characters = [
@@ -6241,9 +6344,9 @@
* _.sortBy(characters, getName);
* // => [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }]
*/
function property(prop) {
function property(key) {
return function(object) {
return object[prop];
return object[key];
};
}
@@ -6306,7 +6409,7 @@
}
/**
* Resolves the value of `prop` on `object`. If `prop` is a function
* Resolves the value of property `key` on `object`. If `key` is a function
* it will be invoked with the `this` binding of `object` and its result returned,
* else the property value is returned. If `object` is falsey then `undefined`
* is returned.
@@ -6315,7 +6418,7 @@
* @memberOf _
* @category Utilities
* @param {Object} object The object to inspect.
* @param {string} prop The name of the property to resolve.
* @param {string} key The name of the property to resolve.
* @returns {*} Returns the resolved value.
* @example
*
@@ -6332,10 +6435,10 @@
* _.result(object, 'stuff');
* // => 'nonsense'
*/
function result(object, prop) {
function result(object, key) {
if (object) {
var value = object[prop];
return isFunction(value) ? object[prop]() : value;
var value = object[key];
return isFunction(value) ? object[key]() : value;
}
}
@@ -6732,6 +6835,7 @@
lodash.chain = chain;
lodash.compact = compact;
lodash.compose = compose;
lodash.constant = constant;
lodash.countBy = countBy;
lodash.create = create;
lodash.createCallback = createCallback;
@@ -6758,6 +6862,7 @@
lodash.invoke = invoke;
lodash.keys = keys;
lodash.map = map;
lodash.mapValues = mapValues;
lodash.max = max;
lodash.memoize = memoize;
lodash.merge = merge;
@@ -6788,6 +6893,7 @@
lodash.where = where;
lodash.without = without;
lodash.wrap = wrap;
lodash.xor = xor;
lodash.zip = zip;
lodash.zipObject = zipObject;