UNPKG

7.96 kBJavaScriptView Raw
1(function(){"use strict";var CHO=["ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"],JUNG=["ㅏ","ㅐ","ㅑ","ㅒ","ㅓ","ㅔ","ㅕ","ㅖ","ㅗ",["ㅗ","ㅏ"],["ㅗ","ㅐ"],["ㅗ","ㅣ"],"ㅛ","ㅜ",["ㅜ","ㅓ"],["ㅜ","ㅔ"],["ㅜ","ㅣ"],"ㅠ","ㅡ",["ㅡ","ㅣ"],"ㅣ"],JONG=["","ㄱ","ㄲ",["ㄱ","ㅅ"],"ㄴ",["ㄴ","ㅈ"],["ㄴ","ㅎ"],"ㄷ","ㄹ",["ㄹ","ㄱ"],["ㄹ","ㅁ"],["ㄹ","ㅂ"],["ㄹ","ㅅ"],["ㄹ","ㅌ"],["ㄹ","ㅍ"],["ㄹ","ㅎ"],"ㅁ","ㅂ",["ㅂ","ㅅ"],"ㅅ","ㅆ","ㅇ","ㅈ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"],HANGUL_OFFSET=44032,CONSONANTS=["ㄱ","ㄲ","ㄳ","ㄴ","ㄵ","ㄶ","ㄷ","ㄸ","ㄹ","ㄺ","ㄻ","ㄼ","ㄽ","ㄾ","ㄿ","ㅀ","ㅁ","ㅂ","ㅃ","ㅄ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"],COMPLETE_CHO=["ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ","ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ","ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"],COMPLETE_JUNG=["ㅏ","ㅐ","ㅑ","ㅒ","ㅓ","ㅔ","ㅕ","ㅖ","ㅗ","ㅘ","ㅙ","ㅚ","ㅛ","ㅜ","ㅝ","ㅞ","ㅟ","ㅠ","ㅡ","ㅢ","ㅣ"],COMPLETE_JONG=["","ㄱ","ㄲ","ㄳ","ㄴ","ㄵ","ㄶ","ㄷ","ㄹ","ㄺ","ㄻ","ㄼ","ㄽ","ㄾ","ㄿ","ㅀ","ㅁ","ㅂ","ㅄ","ㅅ","ㅆ","ㅇ","ㅈ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"],COMPLEX_CONSONANTS=[["ㄱ","ㅅ","ㄳ"],["ㄴ","ㅈ","ㄵ"],["ㄴ","ㅎ","ㄶ"],["ㄹ","ㄱ","ㄺ"],["ㄹ","ㅁ","ㄻ"],["ㄹ","ㅂ","ㄼ"],["ㄹ","ㅅ","ㄽ"],["ㄹ","ㅌ","ㄾ"],["ㄹ","ㅍ","ㄿ"],["ㄹ","ㅎ","ㅀ"],["ㅂ","ㅅ","ㅄ"]],COMPLEX_VOWELS=[["ㅗ","ㅏ","ㅘ"],["ㅗ","ㅐ","ㅙ"],["ㅗ","ㅣ","ㅚ"],["ㅜ","ㅓ","ㅝ"],["ㅜ","ㅔ","ㅞ"],["ㅜ","ㅣ","ㅟ"],["ㅡ","ㅣ","ㅢ"]],CONSONANTS_HASH,CHO_HASH,JUNG_HASH,JONG_HASH,COMPLEX_CONSONANTS_HASH,COMPLEX_VOWELS_HASH;function _makeHash(array){var length=array.length,hash={0:0};for(var i=0;i<length;i++){if(array[i])hash[array[i].charCodeAt(0)]=i}return hash}CONSONANTS_HASH=_makeHash(CONSONANTS);CHO_HASH=_makeHash(COMPLETE_CHO);JUNG_HASH=_makeHash(COMPLETE_JUNG);JONG_HASH=_makeHash(COMPLETE_JONG);function _makeComplexHash(array){var length=array.length,hash={},code1,code2;for(var i=0;i<length;i++){code1=array[i][0].charCodeAt(0);code2=array[i][1].charCodeAt(0);if(typeof hash[code1]==="undefined"){hash[code1]={}}hash[code1][code2]=array[i][2].charCodeAt(0)}return hash}COMPLEX_CONSONANTS_HASH=_makeComplexHash(COMPLEX_CONSONANTS);COMPLEX_VOWELS_HASH=_makeComplexHash(COMPLEX_VOWELS);function _isConsonant(c){return CONSONANTS_HASH[c]}function _isCho(c){return typeof CHO_HASH[c]!=="undefined"}function _isJung(c){return typeof JUNG_HASH[c]!=="undefined"}function _isJong(c){return typeof JONG_HASH[c]!=="undefined"}function _isHangul(c){return 44032<=c&&c<=55203}function _isJungJoinable(a,b){return COMPLEX_VOWELS_HASH[a]&&COMPLEX_VOWELS_HASH[a][b]?COMPLEX_VOWELS_HASH[a][b]:false}function _isJongJoinable(a,b){return COMPLEX_CONSONANTS_HASH[a]&&COMPLEX_CONSONANTS_HASH[a][b]?COMPLEX_CONSONANTS_HASH[a][b]:false}var disassemble=function(string){if(typeof string==="object"){string=string.join("")}var result=[],length=string.length,cho,jung,jong,code;for(var i=0;i<length;i++){code=string.charCodeAt(i);if(_isHangul(code)){code-=HANGUL_OFFSET;jong=code%28;jung=(code-jong)/28%21;cho=parseInt((code-jong)/28/21);result.push(CHO[cho]);if(typeof JUNG[jung]==="object"){result=result.concat(JUNG[jung])}else{result.push(JUNG[jung])}if(jong>0){if(typeof JONG[jong]==="object"){result=result.concat(JONG[jong])}else{result.push(JONG[jong])}}}else if(_isConsonant(code)){var r;if(_isCho(code)){r=CHO[CHO_HASH[code]]}else{r=JONG[JONG_HASH[code]]}if(typeof r=="string"){result.push(r)}else{result=result.concat(r)}}else if(_isJung(code)){var r=JUNG[JUNG_HASH[code]];if(typeof r=="string"){result.push(r)}else{result=result.concat(r)}}else{result.push(string.charAt(i))}}return result};var assemble=function(array){if(typeof array==="string"){array=disassemble(array)}var result=[],length=array.length,code,stage=0,complete_index=-1,previous_code;function _makeHangul(index){var code,cho,jung1,jung2,jong1=0,jong2,hangul="";if(complete_index+1>index){return}for(var step=1;;step++){if(step===1){cho=array[complete_index+step].charCodeAt(0);if(_isJung(cho)){if(complete_index+step+1<=index&&_isJung(jung1=array[complete_index+step+1].charCodeAt(0))){result.push(String.fromCharCode(_isJungJoinable(cho,jung1)));complete_index=index;return}else{result.push(array[complete_index+step]);complete_index=index;return}}else if(!_isCho(cho)){result.push(array[complete_index+step]);complete_index=index;return}hangul=array[complete_index+step]}else if(step===2){jung1=array[complete_index+step].charCodeAt(0);if(_isCho(jung1)){cho=_isJongJoinable(cho,jung1);hangul=String.fromCharCode(cho);result.push(hangul);complete_index=index;return}else{hangul=String.fromCharCode((CHO_HASH[cho]*21+JUNG_HASH[jung1])*28+HANGUL_OFFSET)}}else if(step===3){jung2=array[complete_index+step].charCodeAt(0);if(_isJungJoinable(jung1,jung2)){jung1=_isJungJoinable(jung1,jung2)}else{jong1=jung2}hangul=String.fromCharCode((CHO_HASH[cho]*21+JUNG_HASH[jung1])*28+JONG_HASH[jong1]+HANGUL_OFFSET)}else if(step===4){jong2=array[complete_index+step].charCodeAt(0);if(_isJongJoinable(jong1,jong2)){jong1=_isJongJoinable(jong1,jong2)}else{jong1=jong2}hangul=String.fromCharCode((CHO_HASH[cho]*21+JUNG_HASH[jung1])*28+JONG_HASH[jong1]+HANGUL_OFFSET)}else if(step===5){jong2=array[complete_index+step].charCodeAt(0);jong1=_isJongJoinable(jong1,jong2);hangul=String.fromCharCode((CHO_HASH[cho]*21+JUNG_HASH[jung1])*28+JONG_HASH[jong1]+HANGUL_OFFSET)}if(complete_index+step>=index){result.push(hangul);complete_index=index;return}}}for(var i=0;i<length;i++){code=array[i].charCodeAt(0);if(!_isCho(code)&&!_isJung(code)&&!_isJong(code)){_makeHangul(i-1);_makeHangul(i);stage=0;continue}if(stage==0){if(_isCho(code)){stage=1}else if(_isJung(code)){stage=4}}else if(stage==1){if(_isJung(code)){stage=2}else{if(_isJongJoinable(previous_code,code)){stage=5}else{_makeHangul(i-1)}}}else if(stage==2){if(_isJong(code)){stage=3}else if(_isJung(code)){if(_isJungJoinable(previous_code,code)){}else{_makeHangul(i-1);stage=4}}else{_makeHangul(i-1);stage=1}}else if(stage==3){if(_isJong(code)){if(_isJongJoinable(previous_code,code)){}else{_makeHangul(i-1);stage=1}}else if(_isCho(code)){_makeHangul(i-1);stage=1}else if(_isJung(code)){_makeHangul(i-2);stage=2}}else if(stage==4){if(_isJung(code)){if(_isJungJoinable(previous_code,code)){_makeHangul(i);stage=0}else{_makeHangul(i-1)}}else{_makeHangul(i-1);stage=1}}else if(stage==5){if(_isJung(code)){_makeHangul(i-2);stage=2}else{_makeHangul(i-1);stage=1}}previous_code=code}_makeHangul(i-1);return result.join("")};var search=function(a,b){var ad=disassemble(a).join(""),bd=disassemble(b).join("");return ad.indexOf(bd)};function Searcher(string){this.string=string;this.disassembled=disassemble(string).join("")}Searcher.prototype.search=function(string){return disassemble(string).join("").indexOf(this.disassembled)};var endsWithConsonant=function(string){if(typeof string==="object"){string=string.join("")}var code=string.charCodeAt(string.length-1);if(_isHangul(code)){code-=HANGUL_OFFSET;var jong=code%28;if(jong>0){return true}}else if(typeof _isConsonant(code)!=="undefined"){return true}return false};var hangul={disassemble:disassemble,assemble:assemble,search:search,Searcher:Searcher,endsWithConsonant:endsWithConsonant,isHangul:function(c){if(typeof c==="string")c=c.charCodeAt(0);return _isHangul(c)},isConsonant:function(c){if(typeof c==="string")c=c.charCodeAt(0);return typeof _isConsonant(c)!=="undefined"},isVowel:function(c){if(typeof c==="string")c=c.charCodeAt(0);return _isJung(c)},isCho:function(c){if(typeof c==="string")c=c.charCodeAt(0);return _isCho(c)},isJong:function(c){if(typeof c==="string")c=c.charCodeAt(0);return _isJong(c)}};if(typeof define=="function"&&define.amd){define(function(){return hangul})}else if(typeof module!=="undefined"){module.exports=hangul}else{window.Hangul=hangul}})();
\No newline at end of file