-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ref #27
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}); |