Skip to content

Commit

Permalink
feat: add renameKeys
Browse files Browse the repository at this point in the history
Ref #27
  • Loading branch information
char0n committed May 5, 2017
1 parent 23e56fe commit 1a2538e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ declare namespace RamdaAdjunct {
(either: Catamorphism<V1|V2>): T1|T2;
}
}

/**
* Creates a new object with the own properties of the provided object, but the
* keys renamed according to the keysMap object as `{oldKey: newKey}`.
* When some key is not found in the keysMap, then it's passed as-is.
*/
renameKeys(keysMap: Object, obj: Object): Object
renameKeys(keysMap: Object): (obj: Object) => Object

}

}
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import list from './list';
import defaults from './defaults';
import resetToDefault from './resetToDefault';
import paths from './paths';
import renameKeys from './renameKeys';

// Type
export { default as isNotUndefined } from './isNotUndefined';
Expand Down Expand Up @@ -99,6 +100,7 @@ export { default as list } from './list';
export { default as defaults } from './defaults';
export { default as resetToDefault } from './resetToDefault';
export { default as paths } from './paths';
export { default as renameKeys } from './renameKeys';

/**
* @namespace RA
Expand Down Expand Up @@ -154,6 +156,7 @@ const RA = {
defaults,
resetToDefault,
paths,
renameKeys,
};

export default RA;
35 changes: 35 additions & 0 deletions src/renameKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { curry, reduce, assoc, keys, has } from 'ramda';

/**
* Creates a new object with the own properties of the provided object, but the
* keys renamed according to the keysMap object as `{oldKey: newKey}`.
* When some key is not found in the keysMap, then it's passed as-is.
*
* Keep in mind that in the case of keys conflict is behaviour undefined and
* the result may vary between various JS engines!
*
* @func renameKeys
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/1.5.0|v1.5.0}
* @category Object
* @sig {a: b} -> {a: *} -> {b: *}
* @param {!Object} keysMap
* @param {!Object} obj
* @return {!Object} New object with renamed keys
* @see {@link RA.renameKeysWith|renameKeysWith}
* @example
*
* const input = { firstName: 'Elisia', age: 22, type: 'human' };
*
* renameKeys({ firstName: 'name', type: 'kind', foo: 'bar' })(input);
* //=> { name: 'Elisia', age: 22, kind: 'human' }
*/
const renameKeys = curry((keysMap, obj) =>
reduce((accumulator, key) => {
const newKeyName = has(key, keysMap) ? keysMap[key] : key;

return assoc(newKeyName, obj[key], accumulator);
}, {}, keys(obj))
);

export default renameKeys;
23 changes: 23 additions & 0 deletions test/renameKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import RA from '../src/index';
import eq from './shared/eq';


describe('renameKeys', function() {
it('tests renaming object keys', function() {
eq(RA.renameKeys({ key1: 'key2', key2: 'key3' }, { key1: 1, key2: 2 }), { key2: 1, key3: 2 });
});

it('tests renaming non existing keys', function() {
eq(RA.renameKeys({ nonExistingKey: 'key2' }, { key1: 1 }), { key1: 1 });
});

it('tests renaming keys on non object', function() {
eq(RA.renameKeys({ key1: 'key2' }, null), {});
eq(RA.renameKeys({ key1: 'key2' }, undefined), {});
});

it('tests currying', function() {
eq(RA.renameKeys({ key1: 'key2', key2: 'key3' }, { key1: 1, key2: 2 }), { key2: 1, key3: 2 });
eq(RA.renameKeys({ key1: 'key2', key2: 'key3' })({ key1: 1, key2: 2 }), { key2: 1, key3: 2 });
});
});

0 comments on commit 1a2538e

Please sign in to comment.