hangul-util
Version:
Korean Hangul Utils
81 lines (79 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.divide = divide;
exports.divideByJong = divideByJong;
exports.divideByJung = divideByJung;
exports.divideHangul = divideHangul;
exports.divideHangulByGroups = divideHangulByGroups;
var _constant = require("./constant");
var _isHangul = require("./isHangul");
function divide() {
var word = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var isSplit = option.isSplit,
resultType = option.resultType;
var wordCode = word.charCodeAt(0);
var charCode = wordCode - _constant.HANGUL_START_CHARCODE;
if (!(0, _isHangul.isHangulByCode)(wordCode)) {
return [word[0]];
}
var choIndex = Math.floor(charCode / _constant.CHO_PERIOD);
var jungIndex = Math.floor(charCode % _constant.CHO_PERIOD / _constant.JONG_PERIOD);
var jongIndex = charCode % _constant.JONG_PERIOD;
var cho = _constant.CHO_HANGUL[choIndex] || "";
var jung = _constant.JUNG_HANGUL[jungIndex] || "";
var jong = _constant.JONG_HANGUL[jongIndex] || "";
// 더 세분하게 분리하기 ㅙ -> ㅗㅐ
var dividedJung = isSplit ? divideByJung(jung) : jung;
var dividedJong = isSplit ? divideByJong(jong) : jong;
if (resultType === "index") {
return {
cho: choIndex,
jung: jungIndex,
jong: jongIndex
};
}
if (resultType === "object") {
return {
cho: cho,
jung: dividedJung,
jong: dividedJong
};
}
if (resultType === "string") {
return cho + dividedJung + dividedJong;
}
return (cho + dividedJung + dividedJong).split("");
}
function divideHangulByGroups() {
var _option$isSplit, _option$resultType;
var word = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var isSplit = (_option$isSplit = option === null || option === void 0 ? void 0 : option.isSplit) !== null && _option$isSplit !== void 0 ? _option$isSplit : true;
var resultType = (_option$resultType = option === null || option === void 0 ? void 0 : option.resultType) !== null && _option$resultType !== void 0 ? _option$resultType : "array";
return word.toString().split("").map(function (_char) {
return divide(_char, {
isSplit: isSplit,
resultType: resultType
});
});
}
function divideHangul() {
var word = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var isSplit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var divided = word.toString().split("").map(function (_char2) {
return divide(_char2, {
isSplit: isSplit,
resultType: "string"
});
}).join("");
return divided.split("");
}
function divideByJung(jung) {
return _constant.JUNG_COMPLETE_HANGUL[jung] || jung;
}
function divideByJong(jong) {
return _constant.JONG_COMPLETE_HANGUL[jong] || jong;
}