Skip to content

Instantly share code, notes, and snippets.

@iswanj
Last active June 11, 2018 06:42
Show Gist options
  • Save iswanj/826146c15daee6dc9dd8644e010ed4dc to your computer and use it in GitHub Desktop.
Save iswanj/826146c15daee6dc9dd8644e010ed4dc to your computer and use it in GitHub Desktop.
Remove language specification from data
/**
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