長音あ行変換 (脱力用)
by
ephys
2012-02-16 [2012/02/16 11:24:38]
text node 中の長音をあ行に変換する
/*
* @title 長音あ行変換 (脱力用)
* @description text node 中の長音をあ行に変換する
* @include
* @license MIT License
* @require
*/
(function(){
var regex = /([ぁ-ゖァ-ヶ])ー/g;
search(document.getElementsByTagName('body')[0]);
function search(elem) {
if (elem.nodeType == 3) {
elem.nodeValue = elem.nodeValue.replace(regex, convertLongVowel);
}
else if (elem.nodeType == 1 && elem.hasChildNodes()) {
for (var i = 0; i < elem.childNodes.length; i++) {
search(elem.childNodes[i]);
}
}
}
function convertLongVowel(match, char) {
var h = char;
var gap = 0;
var per = 2;
if (char >= 'ァ') {
h = String.fromCharCode(char.charCodeAt() - 'ァ'.charCodeAt() + 'ぁ'.charCodeAt());
}
if (h < 'づ') {
gap = h.charCodeAt() - 'ぁ'.charCodeAt();
}
else if (h < 'な') {
gap = h.charCodeAt() - 'づ'.charCodeAt() + 5;
}
else if (h < 'は') {
gap = h.charCodeAt() - 'な'.charCodeAt();
per = 1;
}
else if (h < 'ま') {
gap = h.charCodeAt() - 'は'.charCodeAt();
per = 3;
}
else if (h < 'ゃ') {
gap = h.charCodeAt() - 'ま'.charCodeAt();
per = 1;
}
else if (h < 'ら') {
gap = h.charCodeAt() - 'ゃ'.charCodeAt();
gap += Math.floor(gap / 2) * 2;
}
else if (h < 'ゎ') {
gap = h.charCodeAt() - 'ら'.charCodeAt();
per = 1;
}
else if (h < 'ゐ') {
gap = 0;
}
else if (h <= 'ん') {
return char + char;
}
else {
per = 1;
if (h == 'ゔ') {
gap = 2;
}
else if (h == 'ゕ') {
gap = 0;
}
else {
gap = 3;
}
}
return char + (char < 'ァ' ? 'あいうえお' : 'アイウエオ').charAt(Math.floor(gap / per % 5));
}
}())
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。