UNPKG

1.16 kBJavaScriptView Raw
1var _curry1 =
2/*#__PURE__*/
3require("./internal/_curry1");
4
5var keys =
6/*#__PURE__*/
7require("./keys");
8/**
9 * Returns a new object with the keys of the given object as values, and the
10 * values of the given object, which are coerced to strings, as keys. Note
11 * that the last key found is preferred when handling the same value.
12 *
13 * @func
14 * @memberOf R
15 * @since v0.9.0
16 * @category Object
17 * @sig {s: x} -> {x: s}
18 * @param {Object} obj The object or array to invert
19 * @return {Object} out A new object
20 * @see R.invert
21 * @example
22 *
23 * const raceResults = {
24 * first: 'alice',
25 * second: 'jake'
26 * };
27 * R.invertObj(raceResults);
28 * //=> { 'alice': 'first', 'jake':'second' }
29 *
30 * // Alternatively:
31 * const raceResults = ['alice', 'jake'];
32 * R.invertObj(raceResults);
33 * //=> { 'alice': '0', 'jake':'1' }
34 */
35
36
37var invertObj =
38/*#__PURE__*/
39_curry1(function invertObj(obj) {
40 var props = keys(obj);
41 var len = props.length;
42 var idx = 0;
43 var out = {};
44
45 while (idx < len) {
46 var key = props[idx];
47 out[obj[key]] = key;
48 idx += 1;
49 }
50
51 return out;
52});
53
54module.exports = invertObj;
\No newline at end of file