Projekt

Obecné

Profil

Stáhnout (1.61 KB) Statistiky
| Větev: | Revize:
1
var baseIteratee = require('./_baseIteratee'),
2
    createInverter = require('./_createInverter');
3

    
4
/** Used for built-in method references. */
5
var objectProto = Object.prototype;
6

    
7
/** Used to check objects for own properties. */
8
var hasOwnProperty = objectProto.hasOwnProperty;
9

    
10
/**
11
 * Used to resolve the
12
 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
13
 * of values.
14
 */
15
var nativeObjectToString = objectProto.toString;
16

    
17
/**
18
 * This method is like `_.invert` except that the inverted object is generated
19
 * from the results of running each element of `object` thru `iteratee`. The
20
 * corresponding inverted value of each inverted key is an array of keys
21
 * responsible for generating the inverted value. The iteratee is invoked
22
 * with one argument: (value).
23
 *
24
 * @static
25
 * @memberOf _
26
 * @since 4.1.0
27
 * @category Object
28
 * @param {Object} object The object to invert.
29
 * @param {Function} [iteratee=_.identity] The iteratee invoked per element.
30
 * @returns {Object} Returns the new inverted object.
31
 * @example
32
 *
33
 * var object = { 'a': 1, 'b': 2, 'c': 1 };
34
 *
35
 * _.invertBy(object);
36
 * // => { '1': ['a', 'c'], '2': ['b'] }
37
 *
38
 * _.invertBy(object, function(value) {
39
 *   return 'group' + value;
40
 * });
41
 * // => { 'group1': ['a', 'c'], 'group2': ['b'] }
42
 */
43
var invertBy = createInverter(function(result, value, key) {
44
  if (value != null &&
45
      typeof value.toString != 'function') {
46
    value = nativeObjectToString.call(value);
47
  }
48

    
49
  if (hasOwnProperty.call(result, value)) {
50
    result[value].push(key);
51
  } else {
52
    result[value] = [key];
53
  }
54
}, baseIteratee);
55

    
56
module.exports = invertBy;
(414-414/634)