-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 834 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module.exports = function(map, opts) {
var fallback = (opts && opts.fallback) || function(char) { return char }
var keys = Object.keys(map).sort(function(a, b) {
return b.length - a.length
})
function peek(string, offset) {
for (var i = 0; i < keys.length; i++) {
if (match(keys[i], string, offset)) return keys[i]
}
}
return function transliterate(string) {
var out = ''
var offset = 0
while(offset < string.length) {
var key = peek(string, offset)
if (key) {
out += map[key]
offset += key.length
} else {
out += fallback(string[offset])
offset += 1
}
}
return out
}
}
function match(key, string, offset) {
for (var i = 0; i < key.length; i++) {
if (key[i] != string[i + offset]) return false
}
return true
}