[d24f17c] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | exports.__esModule = true;
|
---|
| 4 | exports["default"] = void 0;
|
---|
| 5 | var _ramda = require("ramda");
|
---|
| 6 | var _renameKeysWith = _interopRequireDefault(require("./renameKeysWith"));
|
---|
| 7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
---|
| 8 | var valueOrKey = function valueOrKey(keysMap) {
|
---|
| 9 | return function (key) {
|
---|
| 10 | if ((0, _ramda.has)(key, keysMap)) {
|
---|
| 11 | return keysMap[key];
|
---|
| 12 | }
|
---|
| 13 | return key;
|
---|
| 14 | };
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * Creates a new object with the own properties of the provided object, but the
|
---|
| 19 | * keys renamed according to the keysMap object as `{oldKey: newKey}`.
|
---|
| 20 | * When some key is not found in the keysMap, then it's passed as-is.
|
---|
| 21 | *
|
---|
| 22 | * Keep in mind that in the case of keys conflict is behaviour undefined and
|
---|
| 23 | * the result may vary between various JS engines!
|
---|
| 24 | *
|
---|
| 25 | * @func renameKeys
|
---|
| 26 | * @memberOf RA
|
---|
| 27 | * @since {@link https://char0n.github.io/ramda-adjunct/1.5.0|v1.5.0}
|
---|
| 28 | * @category Object
|
---|
| 29 | * @sig {a: b} -> {a: *} -> {b: *}
|
---|
| 30 | * @param {!Object} keysMap
|
---|
| 31 | * @param {!Object} obj
|
---|
| 32 | * @return {!Object} New object with renamed keys
|
---|
| 33 | * @see {@link https://github.com/ramda/ramda/wiki/Cookbook#rename-keys-of-an-object|Ramda Cookbook}, {@link RA.renameKeysWith|renameKeysWith}
|
---|
| 34 | * @example
|
---|
| 35 | *
|
---|
| 36 | * const input = { firstName: 'Elisia', age: 22, type: 'human' };
|
---|
| 37 | *
|
---|
| 38 | * RA.renameKeys({ firstName: 'name', type: 'kind', foo: 'bar' })(input);
|
---|
| 39 | * //=> { name: 'Elisia', age: 22, kind: 'human' }
|
---|
| 40 | */
|
---|
| 41 | var renameKeys = (0, _ramda.curry)(function (keysMap, obj) {
|
---|
| 42 | return (0, _renameKeysWith["default"])(valueOrKey(keysMap), obj);
|
---|
| 43 | });
|
---|
| 44 | var _default = renameKeys;
|
---|
| 45 | exports["default"] = _default; |
---|