-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkana.js
33 lines (33 loc) · 1.77 KB
/
kana.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
"use strict";
let hiragana =
"ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとどなに" +
"ぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろゎわゐゑをんゔゕゖ";
let katakana =
"ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニ" +
"ヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶ";
if (hiragana.length !== katakana.length) { throw new Error('Kana strings not same length?'); }
exports.kata2hiraMap = new Map([]);
exports.hira2kataMap = new Map([]);
hiragana.split('').forEach((h, i) => {
exports.kata2hiraMap.set(katakana[i], h);
exports.hira2kataMap.set(h, katakana[i]);
});
function kata2hira(s) { return s.split('').map(c => exports.kata2hiraMap.get(c) || c).join(''); }
exports.kata2hira = kata2hira;
function hira2kata(s) { return s.split('').map(c => exports.hira2kataMap.get(c) || c).join(''); }
exports.hira2kata = hira2kata;
const hiraganaRe = /[ぁ-ゖ]/;
const katakanaRe = /[ァ-ヺ]/;
/**
* Omits nakaguro (・), chouonpu (ー), and コト (ヿ)!
*/
function hasKatakana(s) { return katakanaRe.test(s); }
exports.hasKatakana = hasKatakana;
function hasHiragana(s) { return hiraganaRe.test(s); }
exports.hasHiragana = hasHiragana;
/*
There are other ways of doing this. In Unicode, katakana is 96 codepoints above hiragana. So
`String.fromCharCode(hiragana.charCodeAt(0) + 96)` will produce katakana. In speed tests though, the
above Map-based approach had the least variability in runtime (200 to 800 microseconds), while
arithmetic-based approaches used 100 to 1500 microseconds.
*/