Skip to content

Instantly share code, notes, and snippets.

@Sankame
Last active September 22, 2019 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sankame/5b2422163cb12a10adfcb452d2843556 to your computer and use it in GitHub Desktop.
Save Sankame/5b2422163cb12a10adfcb452d2843556 to your computer and use it in GitHub Desktop.
A sample code to change the half-sized kana to full-sized one in Japanese
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function Kana(){
//The change pattern
const HALF_2_FULL = 0;
const FULL_2_HALF = 1;
/**
* Reverse keys and values of the map
* @param {Object} targetMap
* @param {Boolean} [areKeysNumeric=false] Are keys numeric?
* @return {Object} The map that keys and values were reversed
*/
const reverseMap = function(targetMap, areKeysNumeric) {
return Object.keys(targetMap).reduceRight(function (newMap, key) {
return (newMap[targetMap[key]] = areKeysNumeric ? parseInt(key, 10) : key, newMap);
}, {});
};
/**
* Change half-sized kana to full-sized one
* @param {String} targetString
* @param {Number} pattern Specify the change pattern.
* @return {String} The changed string
*/
const changeKana = function(targetString, pattern) {
const originalKanaMap = {
'ガ': 'ガ', 'ギ': 'ギ', 'グ': 'グ', 'ゲ': 'ゲ', 'ゴ': 'ゴ',
'ザ': 'ザ', 'ジ': 'ジ', 'ズ': 'ズ', 'ゼ': 'ゼ', 'ゾ': 'ゾ',
'ダ': 'ダ', 'ヂ': 'ヂ', 'ヅ': 'ヅ', 'デ': 'デ', 'ド': 'ド',
'バ': 'バ', 'ビ': 'ビ', 'ブ': 'ブ', 'ベ': 'ベ', 'ボ': 'ボ',
'パ': 'パ', 'ピ': 'ピ', 'プ': 'プ', 'ペ': 'ペ', 'ポ': 'ポ',
'ヴ': 'ヴ', 'ヷ': 'ヷ', 'ヺ': 'ヺ',
'ア': 'ア', 'イ': 'イ', 'ウ': 'ウ', 'エ': 'エ', 'オ': 'オ',
'カ': 'カ', 'キ': 'キ', 'ク': 'ク', 'ケ': 'ケ', 'コ': 'コ',
'サ': 'サ', 'シ': 'シ', 'ス': 'ス', 'セ': 'セ', 'ソ': 'ソ',
'タ': 'タ', 'チ': 'チ', 'ツ': 'ツ', 'テ': 'テ', 'ト': 'ト',
'ナ': 'ナ', 'ニ': 'ニ', 'ヌ': 'ヌ', 'ネ': 'ネ', 'ノ': 'ノ',
'ハ': 'ハ', 'ヒ': 'ヒ', 'フ': 'フ', 'ヘ': 'ヘ', 'ホ': 'ホ',
'マ': 'マ', 'ミ': 'ミ', 'ム': 'ム', 'メ': 'メ', 'モ': 'モ',
'ヤ': 'ヤ', 'ユ': 'ユ', 'ヨ': 'ヨ',
'ラ': 'ラ', 'リ': 'リ', 'ル': 'ル', 'レ': 'レ', 'ロ': 'ロ',
'ワ': 'ワ', 'ヲ': 'ヲ', 'ン': 'ン',
'ァ': 'ァ', 'ィ': 'ィ', 'ゥ': 'ゥ', 'ェ': 'ェ', 'ォ': 'ォ',
'ッ': 'ッ', 'ャ': 'ャ', 'ュ': 'ュ', 'ョ': 'ョ',
'。': '。', '、': '、', 'ー': 'ー', '「': '「', '」': '」', '・': '・'
};
switch (pattern) {
case HALF_2_FULL:
kanaMap = originalKanaMap;
break;
case FULL_2_HALF:
kanaMap = reverseMap(originalKanaMap, false);
break;
default:
throw new Error("Illegal Argument Exception");
break;
}
var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g');
return targetString
.replace(reg, function (match) {
return kanaMap[match];
});
};
/**
* Change half-sized kana to full-sized one
* @param {String} targetString
* @return {String} The changed string
*/
this.half2FullKana = function(targetString) {
return changeKana(targetString, HALF_2_FULL);
};
/**
* Change full-sized kana to half-sized one
* @param {String} targetString
* @return {String} The changed string
*/
this.full2HalfKana = function(targetString) {
return changeKana(targetString, FULL_2_HALF);
};
}
// Test
var kana = new Kana();
document.write("<hr>");
document.write("<b>Test : half2FullKana()</b><br>");
document.write(kana.half2FullKana("アイウエオ、タナカ、スズキ、ヤマモト")==="アイウエオ、タナカ、スズキ、ヤマモト");
document.write("<br>");
document.write(kana.half2FullKana("この文字は変わらない")==="この文字は変わらない");
document.write("<br>");
document.write(kana.half2FullKana("コノモジハカワラナイ。")==="コノモジハカワラナイ。");
document.write("<br>");
document.write(kana.half2FullKana("This characters don't change.")==="This characters don't change.");
document.write("<br>");
document.write(kana.half2FullKana("「ブブン的に変わるパターン」")==="「ブブン的に変わるパターン」");
document.write("<br>");
document.write(kana.half2FullKana("")==="");
document.write("<br>");
document.write("<hr>");
document.write("<b>Test : full2HalfKana()</b><br>");
document.write(kana.full2HalfKana("アイウエオ、タナカ、スズキ、ヤマモト")==="アイウエオ、タナカ、スズキ、ヤマモト");
document.write("<br>");
document.write(kana.full2HalfKana("この文字は変わらない")==="この文字は変わらない");
document.write("<br>");
document.write(kana.full2HalfKana("コノモジハカワラナイ。")==="コノモジハカワラナイ。");
document.write("<br>");
document.write(kana.full2HalfKana("This characters don't change.")==="This characters don't change.");
document.write("<br>");
document.write(kana.full2HalfKana("「ブブン的に変わるパターン」")==="「ブブン的に変わるパターン」");
document.write("<br>");
document.write(kana.full2HalfKana("")==="");
document.write("<br>");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment