Ramda Adjunct 2.7.0

renameKeys.js

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