Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / object / invert.js
1 var isIterateeCall = require('../internal/isIterateeCall'),
2     keys = require('./keys');
3
4 /** Used for native method references. */
5 var objectProto = 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 `multiValue` is `true`.
14  *
15  * @static
16  * @memberOf _
17  * @category Object
18  * @param {Object} object The object to invert.
19  * @param {boolean} [multiValue] Allow multiple values per key.
20  * @param- {Object} [guard] Enables use as a callback 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 `multiValue`
30  * _.invert(object, true);
31  * // => { '1': ['a', 'c'], '2': ['b'] }
32  */
33 function invert(object, multiValue, guard) {
34   if (guard && isIterateeCall(object, multiValue, guard)) {
35     multiValue = undefined;
36   }
37   var index = -1,
38       props = keys(object),
39       length = props.length,
40       result = {};
41
42   while (++index < length) {
43     var key = props[index],
44         value = object[key];
45
46     if (multiValue) {
47       if (hasOwnProperty.call(result, value)) {
48         result[value].push(key);
49       } else {
50         result[value] = [key];
51       }
52     }
53     else {
54       result[value] = key;
55     }
56   }
57   return result;
58 }
59
60 module.exports = invert;