Cleanup _.pairs and _.invert.

This commit is contained in:
John-David Dalton
2015-08-30 04:45:32 -07:00
parent 98a97b1eaf
commit c396b60612

View File

@@ -9105,6 +9105,7 @@
* @category Object * @category Object
* @param {Object} object The object to invert. * @param {Object} object The object to invert.
* @param {boolean} [multiValue] Allow multiple values per key. * @param {boolean} [multiValue] Allow multiple values per key.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {Object} Returns the new inverted object. * @returns {Object} Returns the new inverted object.
* @example * @example
* *
@@ -9117,11 +9118,10 @@
* _.invert(object, true); * _.invert(object, true);
* // => { '1': ['a', 'c'], '2': ['b'] } * // => { '1': ['a', 'c'], '2': ['b'] }
*/ */
function invert(object, multiValue) { function invert(object, multiValue, guard) {
multiValue = typeof multiValue == 'boolean' && multiValue;
return arrayReduce(keys(object), function(result, key) { return arrayReduce(keys(object), function(result, key) {
var value = object[key]; var value = object[key];
if (multiValue) { if (multiValue && !guard) {
if (hasOwnProperty.call(result, value)) { if (hasOwnProperty.call(result, value)) {
result[value].push(key); result[value].push(key);
} else { } else {
@@ -9416,7 +9416,6 @@
* // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed) * // => [['barney', 36], ['fred', 40]] (iteration order is not guaranteed)
*/ */
function pairs(object) { function pairs(object) {
object = Object(object);
return arrayMap(keys(object), function(key) { return arrayMap(keys(object), function(key) {
return [key, object[key]]; return [key, object[key]];
}); });