Remove multiVal param from _.invert.

This commit is contained in:
John-David Dalton
2016-01-23 15:11:44 -06:00
parent d99954133c
commit 552be2f5fb
2 changed files with 10 additions and 51 deletions

View File

@@ -10964,14 +10964,12 @@
/**
* Creates an object composed of the inverted keys and values of `object`.
* If `object` contains duplicate values, subsequent values overwrite property
* assignments of previous values unless `multiVal` is `true`.
* assignments of previous values.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to invert.
* @param {boolean} [multiVal] Allow multiple values per key.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {Object} Returns the new inverted object.
* @example
*
@@ -10979,24 +10977,10 @@
*
* _.invert(object);
* // => { '1': 'c', '2': 'b' }
*
* // with `multiVal`
* _.invert(object, true);
* // => { '1': ['a', 'c'], '2': ['b'] }
*/
function invert(object, multiVal, guard) {
function invert(object) {
return arrayReduce(keys(object), function(result, key) {
var value = object[key];
if (multiVal && !guard) {
if (hasOwnProperty.call(result, value)) {
result[value].push(key);
} else {
result[value] = [key];
}
}
else {
result[value] = key;
}
result[object[key]] = key;
return result;
}, {});
}

View File

@@ -7374,6 +7374,13 @@
assert.deepEqual(_.invert(actual), { 'a': '1', 'b': '2' });
});
QUnit.test('should work with values that shadow keys on `Object.prototype`', function(assert) {
assert.expect(1);
var object = { 'a': 'hasOwnProperty', 'b': 'constructor' };
assert.deepEqual(_.invert(object), { 'hasOwnProperty': 'a', 'constructor': 'b' });
});
QUnit.test('should work with an object that has a `length` property', function(assert) {
assert.expect(1);
@@ -7381,38 +7388,6 @@
assert.deepEqual(_.invert(object), { 'a': '0', 'b': '1', '2': 'length' });
});
QUnit.test('should accept a `multiValue` flag', function(assert) {
assert.expect(1);
var object = { 'a': 1, 'b': 2, 'c': 1 };
assert.deepEqual(_.invert(object, true), { '1': ['a', 'c'], '2': ['b'] });
});
QUnit.test('should only add multiple values to own, not inherited, properties', function(assert) {
assert.expect(2);
var object = { 'a': 'hasOwnProperty', 'b': 'constructor' };
assert.deepEqual(_.invert(object), { 'hasOwnProperty': 'a', 'constructor': 'b' });
assert.ok(lodashStable.isEqual(_.invert(object, true), { 'hasOwnProperty': ['a'], 'constructor': ['b'] }));
});
QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) {
assert.expect(2);
var regular = { 'a': 1, 'b': 2, 'c': 1 },
inverted = { '1': 'c', '2': 'b' };
var array = [regular, regular, regular],
object = { 'a': regular, 'b': regular, 'c': regular },
expected = lodashStable.map(array, lodashStable.constant(inverted));
lodashStable.each([array, object], function(collection) {
var actual = lodashStable.map(collection, _.invert);
assert.deepEqual(actual, expected);
});
});
QUnit.test('should return a wrapped value when chaining', function(assert) {
assert.expect(2);