source: trip-planner-front/node_modules/lodash/invertBy.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1var baseIteratee = require('./_baseIteratee'),
2 createInverter = require('./_createInverter');
3
4/** Used for built-in method references. */
5var objectProto = Object.prototype;
6
7/** Used to check objects for own properties. */
8var 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 */
15var 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 */
43var 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
56module.exports = invertBy;
Note: See TracBrowser for help on using the repository browser.