Skip to content

Instantly share code, notes, and snippets.

@to
Last active February 1, 2018 11:36
Show Gist options
  • Save to/71e875a61bf09d45cfe30ad78712e422 to your computer and use it in GitHub Desktop.
Save to/71e875a61bf09d45cfe30ad78712e422 to your computer and use it in GitHub Desktop.
NFD/NFC
var Normalization = {
conds : [
['\u3099', 78, '\u3094\u30F4'],
['\u3099', 1, 'がぎぐげござじずぜぞだぢづでどばびぶべぼガギグゲゴザジズゼゾダヂヅデドバビブベボ'],
['\u309A', 2, 'ぱぴぷぺぽパピプペポ']
]
,
decompress : function(text){
// 濁音/半濁音を分離する
Normalization.conds.forEach(function(cond){
text = text.replace(new RegExp('[' + cond[2] + ']', 'g'),
function(part){
return String.fromCharCode(part.charCodeAt(0) - cond[1]) + cond[0];
});
});
return text;
}
,
compress : function(text){
// 濁音/半濁音を合成する
Normalization.conds.forEach(function(cond){
text = text.replace(
new RegExp('[' + cond[3] + ']' + cond[0], 'g'),
function(part){
return String.fromCharCode(part.charCodeAt(0) + cond[1]);
});
});
return text;
}
}
// 濁音/半濁音を取り除いた文字列を準備する
Normalization.conds.forEach(function(cond){
cond[3] = cond[2].replace(/./g, function(c){
return String.fromCharCode(c.charCodeAt(0) - cond[1]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment