-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
54 lines (43 loc) · 1.07 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const lowercase = require('./lowercase');
const capital = require('./capital');
const other = require('./other');
const diacritics = require('./diacritics');
const _table = [
...lowercase,
...capital,
...other,
...diacritics
];
const _xsampa = {};
const _ipa = {};
for(const [i, e] of _table.entries()){
_xsampa[e['X-SAMPA']] = i;
}
for(const [i, e] of _table.entries()){
_ipa[e['IPA']] = i;
}
_escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
let xsRE = new RegExp('('+Object.keys(_xsampa).filter(x => !!x).map(_escape).join('|')+')', 'g');
let ipaRE = new RegExp('('+Object.keys(_ipa).filter(x => !!x).map(_escape).join('|')+')', 'g');
function xsampa2ipa(text){
return text.replace(xsRE, xs => {
const i = _xsampa[xs];
const x = _table[i]['IPA'];
if(!xs){ return ''; }
return x;
});
}
function ipa2xsampa(text){
return text.replace(ipaRE, ipa => {
const i = _ipa[ipa];
const x = _table[i]['X-SAMPA'];
if(!ipa){ return ''; }
return x;
});
}
module.exports = {
xsampa2ipa,
ipa2xsampa
};