main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import _curry1 from "./internal/_curry1.js";
|
---|
| 2 | import _has from "./internal/_has.js";
|
---|
| 3 | import keys from "./keys.js";
|
---|
| 4 | /**
|
---|
| 5 | * Same as [`R.invertObj`](#invertObj), however this accounts for objects with
|
---|
| 6 | * duplicate values by putting the values into an array.
|
---|
| 7 | *
|
---|
| 8 | * @func
|
---|
| 9 | * @memberOf R
|
---|
| 10 | * @since v0.9.0
|
---|
| 11 | * @category Object
|
---|
| 12 | * @sig {s: x} -> {x: [ s, ... ]}
|
---|
| 13 | * @param {Object} obj The object or array to invert
|
---|
| 14 | * @return {Object} out A new object with keys in an array.
|
---|
| 15 | * @see R.invertObj
|
---|
| 16 | * @example
|
---|
| 17 | *
|
---|
| 18 | * const raceResultsByFirstName = {
|
---|
| 19 | * first: 'alice',
|
---|
| 20 | * second: 'jake',
|
---|
| 21 | * third: 'alice',
|
---|
| 22 | * };
|
---|
| 23 | * R.invert(raceResultsByFirstName);
|
---|
| 24 | * //=> { 'alice': ['first', 'third'], 'jake':['second'] }
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | var invert =
|
---|
| 28 | /*#__PURE__*/
|
---|
| 29 | _curry1(function invert(obj) {
|
---|
| 30 | var props = keys(obj);
|
---|
| 31 | var len = props.length;
|
---|
| 32 | var idx = 0;
|
---|
| 33 | var out = {};
|
---|
| 34 |
|
---|
| 35 | while (idx < len) {
|
---|
| 36 | var key = props[idx];
|
---|
| 37 | var val = obj[key];
|
---|
| 38 | var list = _has(val, out) ? out[val] : out[val] = [];
|
---|
| 39 | list[list.length] = key;
|
---|
| 40 | idx += 1;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | return out;
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | export default invert; |
---|
Note:
See
TracBrowser
for help on using the repository browser.