Security update for permissions_by_term
[yaffs-website] / node_modules / uncss / node_modules / lodash / invert.js
1 var arrayReduce = require('./internal/arrayReduce'),
2     keys = require('./keys');
3
4 /** Used for built-in method references. */
5 var objectProto = global.Object.prototype;
6
7 /** Used to check objects for own properties. */
8 var hasOwnProperty = objectProto.hasOwnProperty;
9
10 /**
11  * Creates an object composed of the inverted keys and values of `object`.
12  * If `object` contains duplicate values, subsequent values overwrite property
13  * assignments of previous values unless `multiVal` is `true`.
14  *
15  * @static
16  * @memberOf _
17  * @category Object
18  * @param {Object} object The object to invert.
19  * @param {boolean} [multiVal] Allow multiple values per key.
20  * @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
21  * @returns {Object} Returns the new inverted object.
22  * @example
23  *
24  * var object = { 'a': 1, 'b': 2, 'c': 1 };
25  *
26  * _.invert(object);
27  * // => { '1': 'c', '2': 'b' }
28  *
29  * // with `multiVal`
30  * _.invert(object, true);
31  * // => { '1': ['a', 'c'], '2': ['b'] }
32  */
33 function invert(object, multiVal, guard) {
34   return arrayReduce(keys(object), function(result, key) {
35     var value = object[key];
36     if (multiVal && !guard) {
37       if (hasOwnProperty.call(result, value)) {
38         result[value].push(key);
39       } else {
40         result[value] = [key];
41       }
42     }
43     else {
44       result[value] = key;
45     }
46     return result;
47   }, {});
48 }
49
50 module.exports = invert;