Skip to content

Instantly share code, notes, and snippets.

@dwhieb
Last active February 1, 2018 19:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dwhieb/3787f148cb82a6967225752cf58672ef to your computer and use it in GitHub Desktop.
A basic transliteration function that makes string replacements but does not handle common edge cases
// A simple version of the transliterate method which makes replacements
// but does not handle common edge cases.
const simpleTransliterate = (string = '', substitutions = {}) => {
// save the string to a new variable for readability
let str = string;
// get the list of substitutions
Object.entries(substitutions)
// for each substitution...
.forEach(([input, replacement]) => {
// create a regular expression that searches globally for the string to replace
const regexp = new RegExp(input, 'gu');
// then replace all matched instances of the regular expression with the new string
str = str.replace(regexp, replacement);
});
// return the string with the substitutions made
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment