Last active
April 2, 2018 11:00
-
-
Save SuperOleg39/08c0cd1507809e2c7fc22f641d9e7f16 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Пример использования: | |
| * | |
| * const source = { | |
| * name: 'Test', | |
| * last_name: 'Test', | |
| * phone: '123456789' | |
| * }; | |
| * | |
| * const dist = { | |
| * name: '', | |
| * lastName: '' | |
| * }; | |
| * | |
| * const mapping = { | |
| * last_name: 'lastName' | |
| * } | |
| * | |
| * const result = remap(source, dist, mapping); // { name: 'Test', lastName: 'Test', phone: '123456789' } | |
| * | |
| */ | |
| export default function remap(source: any, dest: any, mapping: any): any { | |
| const result = {}; | |
| const sourceCopy = { ...source }; | |
| const destCopy = { ...dest }; | |
| Object.keys(destCopy).forEach((key) => { | |
| result[key] = destCopy[key]; | |
| }); | |
| Object.keys(sourceCopy).forEach((key) => { | |
| if (key in mapping) { | |
| result[mapping[key]] = sourceCopy[key]; | |
| } else { | |
| result[key] = sourceCopy[key]; | |
| } | |
| }); | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment