Last active
February 1, 2018 19:58
-
-
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
This file contains hidden or 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
// 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