Skip to content

Instantly share code, notes, and snippets.

@SuperOleg39
Last active April 2, 2018 11:00
Show Gist options
  • Select an option

  • Save SuperOleg39/08c0cd1507809e2c7fc22f641d9e7f16 to your computer and use it in GitHub Desktop.

Select an option

Save SuperOleg39/08c0cd1507809e2c7fc22f641d9e7f16 to your computer and use it in GitHub Desktop.
/**
* Пример использования:
*
* 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