Last active
June 11, 2018 06:42
-
-
Save iswanj/826146c15daee6dc9dd8644e010ed4dc to your computer and use it in GitHub Desktop.
Remove language specification from data
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
/** | |
const data = { | |
firstName_en: "Iswan", | |
lastName_en: "Jumat", | |
age: 30 | |
} | |
* | |
const exprectedData = { | |
firstName: "Iswan", | |
lastName: "Jumat", | |
age: 30 | |
} | |
*/ | |
export const removeLanguageSpec = (data: Object) => { | |
let keys = ["si", "en", "ta"]; | |
const findKeys = (data: string): any => { | |
return keys.reduce((value, key) => { | |
if (data.indexOf(`_${key}`) > -1) { | |
return `_${key}`; | |
} | |
return value; | |
}, -1); | |
}; | |
Object.keys(data).reduce((obj, item) => { | |
const findKey = findKeys(item); | |
if (findKey !== -1) { | |
const removedKey = item.split(findKey); | |
return { | |
...obj, | |
[removedKey[0]]: data[item] | |
}; | |
} | |
return { | |
...obj, | |
[item]: data[item] | |
}; | |
}, {}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment