I have two functions that replace letters. In one function, I'm replacing a regular Hebrew letter with its final form, and the other function undoes this operation. (Consider it like lowercase/uppercase, except only a few letters have both forms.)
I created an object where the keys are the regular forms and the values are the final forms. In each function, I iterate over the object and run a regex - in one function it's map[i]
-> i
; the other is i
-> map[i]
.
Would it be worth it / even possible to abstract this for
loop into a function?
var map = {
כ: 'ך',
מ: 'ם',
נ: 'ן',
פ: 'ף',
צ: 'ץ'
};
exports.toFinal = function(text) {
for (var i in map) {
text = text.replace(new RegExp(i,'g'), map[i]);
}
return text;
}
exports.toRegular = function(text) {
for (var i in map) {
text = text.replace(new RegExp(map[i],'g'), i);
}
return text;
}
(exports
is Node.js's exports
object, and the code is implicitly running in a closure.)