exapsy

Untitled

Oct 18th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const replaceSubtermsWithTranslatedValues = (preparedTerms: PrepareTermsResult['preparedTerms'], storedTranslations: ITranslation[]) => {
  2.   const termsWithStoredTranslations = _.cloneDeep(preparedTerms);
  3.   storedTranslations.forEach(({ source, translation }) =>
  4.     Object.values(termsWithStoredTranslations).forEach((term) => {
  5.       term.subterms = term.subterms.map((subterm) => (subterm === source ? translation : subterm));
  6.     }),
  7.   );
  8.  
  9.   return termsWithStoredTranslations;
  10. };
  11.  
  12. const replaceSubtermsWithTranslatedValuesV2 = (preparedTerms: PrepareTermsResult['preparedTerms'], storedTranslations: ITranslation[]) => {
  13.   const termsWithStoredTranslations = _.cloneDeep(preparedTerms);
  14.   const translationsMap = new Map<string, string>(storedTranslations.map(({ source, translation }) => [source, translation]));
  15.   Object.values(termsWithStoredTranslations).forEach((term) => {
  16.     term.subterms = term.subterms.map((subterm) => translationsMap.get(subterm) || subterm);
  17.   });
  18.  
  19.   return termsWithStoredTranslations;
  20. };
  21.  
Advertisement
Add Comment
Please, Sign In to add comment