I\'ve been trying to figure out how to map a set of characters in a string to another set similar to the tr function in Perl.
I found this site that sh
Here is a function that receives text orig dest and replaces in text each character for the one in the corresponding position in dest.
Is is not good enough for cases where more than one character must be replaced by only one or vice-versa. It is not good enough for removing accents from Portuguese texts, which is my use case.
function tr(text, orig, dest) {
console.assert(orig.length == dest.length);
const a = orig.split('').map(i=> new RegExp(i, 'g'));
const b = dest.split('');
return a.reduce((prev, curr, idx) => prev.replace(a[idx], b[idx]), text );
}
How to use it:
var port = "ÀÂÃÁÉÊÍÓÔÕÜÚÇáàãâêéíóõôúüç";
var ascii = "AAAAEEIOOOUUCaaaaeeiooouuc";
console.log(tr("não têm ações em seqüência", port, ascii)) ;