참고 : http://d2.naver.com/helloworld/76650



String.prototype.normalizeEx = function () {
	if(typeof this.normalize == 'function') {
		return this.normalize('NFC');
	} else {
		return this.normalizeNFCForIE();
	}
}

String.prototype.normalizeNFCForIE = function() {
	
	var result = '';
	
	var i, c;
	var cho = -1, jung = -1, jong = -1;
	
	for (i = 0 ; i < this.length ; ++i) {
		
		c = this.charCodeAt(i);
		
		if (0x1100 <= c && c < 0x1161) {
			
			// 새 초성이 나오면 기존 초, 중, 종을 조합
			if(cho >= 0) {
				result += combineKorean(cho, jung, jong);
				cho = jung = jong = -1;
			}
			
			cho = c - 0x1100;
			
		} else if (0x1161 <= c && c < 0x11A8) {
			jung = c - 0x1161;
			
		} else if (0x11A8 <= c && c <= 0x11FF) {
			jong = c - 0x11A8;
			
		} else {
			
			// 한글이 아니면 기존 초, 중, 종을 조합
			if(cho >= 0 || jung >= 0 || jong >= 0) {
				result += combineKorean(cho, jung, jong);
				cho = jung = jong = -1;
			}
			
			result += String.fromCharCode(c);
		}
	}
	
	// 루프가 끝난 후 아직 한글이 남아있으면 초, 중, 종을 조합
	if(cho >= 0 || jung >= 0 || jong >= 0) {
		result += combineKorean(cho, jung, jong);
	}
	
	return result.isNullOrEmpty()? this : result;
}

combineKorean = function (cho, jung, jong) {
	
	var jongsung = (jong == 0 ? 0 : jong + 1);
	
	var code = 0xAC00 + ((cho * 21) + jung) * 28 + jongsung;
	
	return String.fromCharCode(code);
}

String.prototype.isNullOrEmpty = function() {
    return (this.constructor !== String || this.trimAll() === '');
};


'Programming' 카테고리의 다른 글

About "Expert Beginner"  (0) 2018.10.04
좋은 프로그램의 척도  (0) 2018.01.31
Linux 버전 확인 명령어  (0) 2016.10.14
기술자 등급분류 기준  (0) 2016.02.11
C# 과 C++에 관한 고찰...  (0) 2014.02.14

+ Recent posts