mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Remove multiVal param from _.invert.
This commit is contained in:
22
lodash.js
22
lodash.js
@@ -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;
|
||||
}, {});
|
||||
}
|
||||
|
||||
39
test/test.js
39
test/test.js
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user