Skip to content

Instantly share code, notes, and snippets.

@leandrojo
Created March 10, 2020 18:30
Show Gist options
  • Select an option

  • Save leandrojo/ae125c031a99b4c33b336eb4156341a4 to your computer and use it in GitHub Desktop.

Select an option

Save leandrojo/ae125c031a99b4c33b336eb4156341a4 to your computer and use it in GitHub Desktop.
function translate(document: any) {
if (typeof document !== 'object') return {};
return ({ raw, ...dictionary }: { raw: string[] }) => {
if (Array.isArray(document)) {
// @ts-ignore
return document.map((v: {}) => translate(v)({ raw, ...dictionary }));
}
if (raw !== undefined) {
// @ts-ignore
const arr = raw
.find((v: string) => (typeof v === 'string' ? v : ''))
.split(';')
.map((param) => param
.split(':')
.map((v) => v.trim()))
.filter(([v]) => v);
// @ts-ignore
return arr.reduce((before, [from, to]) => ({ ...before, [to]: document[from] }), {});
}
return {};
};
}
export default translate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment