Last active
February 1, 2018 19:57
-
-
Save dwhieb/d0469524bbf9b47b375540543a376674 to your computer and use it in GitHub Desktop.
An intermediate transliteration function that handles substrings during substitution but not other edge cases
This file contains 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
// An intermediate transliteration function that handles substrings | |
// but does not handle other edge cases | |
const sortedTransliterate = (string = '', substitutions = {}) => { | |
// save the string to a new variable for readability | |
let str = string; | |
// get the list of substitutions | |
Object.entries(substitutions) | |
// sort the substitutions by longest string (avoids the substring problem) | |
.sort(([a], [b]) => b.length - a.length) | |
// 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