Skip to content

Commit

Permalink
Add _.constant, _.mapValues, and _.xor.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Nov 21, 2013
1 parent 6291a58 commit 8427ca1
Show file tree
Hide file tree
Showing 9 changed files with 939 additions and 394 deletions.
174 changes: 140 additions & 34 deletions dist/lodash.compat.js
Expand Up @@ -2483,15 +2483,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;
}

/**
Expand Down Expand Up @@ -2896,6 +2896,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
Expand Down Expand Up @@ -3111,11 +3157,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 _
Expand Down Expand Up @@ -4740,29 +4786,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];
Expand Down Expand Up @@ -5179,13 +5230,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));
}

Expand Down Expand Up @@ -5265,6 +5316,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
Expand Down Expand Up @@ -5971,6 +6053,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.
Expand Down Expand Up @@ -6200,14 +6303,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 = [
Expand All @@ -6223,9 +6326,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];
};
}

Expand Down Expand Up @@ -6288,7 +6391,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.
Expand All @@ -6297,7 +6400,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
*
Expand All @@ -6314,10 +6417,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;
}
}

Expand Down Expand Up @@ -6714,6 +6817,7 @@
lodash.chain = chain;
lodash.compact = compact;
lodash.compose = compose;
lodash.constant = constant;
lodash.countBy = countBy;
lodash.create = create;
lodash.createCallback = createCallback;
Expand All @@ -6740,6 +6844,7 @@
lodash.invoke = invoke;
lodash.keys = keys;
lodash.map = map;
lodash.mapValues = mapValues;
lodash.max = max;
lodash.memoize = memoize;
lodash.merge = merge;
Expand Down Expand Up @@ -6770,6 +6875,7 @@
lodash.where = where;
lodash.without = without;
lodash.wrap = wrap;
lodash.xor = xor;
lodash.zip = zip;
lodash.zipObject = zipObject;

Expand Down

0 comments on commit 8427ca1

Please sign in to comment.