hangulx
Version:
Providing various utilities for Hangul
100 lines (99 loc) • 4.18 kB
JavaScript
import { compoundSurnames } from "./constants/korean-name/compound-surname/compound-surname";
import { Random } from "./utils/Random";
import { hangulSurnames } from "./constants/korean-name/hangul-surnames";
import { chineseSurnames } from "./constants/korean-name/chinese-surnames";
import { feminineNames, masculineNames, unisexNames } from "./constants/korean-name/names";
export var KoreanName = /** @class */ (function () {
function KoreanName() {
}
KoreanName.splitCandidates = function (fullName, option) {
if (typeof fullName !== 'string') {
throw new Error('fullName should be string type');
}
var temp = fullName;
var results = [];
// Replace not Hangul and Chinese and Space
temp = temp.replace(this.REG_EXP_NOT_HANGUL_AND_CHINESE_AND_SPACE, '');
// Replace spaces to single space
temp = temp.replace(/\s{2,}/g, ' ');
// Trim
temp = temp.trim();
var length = temp.length;
if (length < 2) {
// console.warn('length is less than 2', `"${temp}"`);
return results;
}
// Space in middle
if (/^[ㄱ-힣]+\s[ㄱ-힣]+$/g.test(temp)) {
var split = temp.split(/\s/);
results.push({ surname: split[0], givenName: split[1] });
return results;
}
// Compound surname
if ((option === null || option === void 0 ? void 0 : option.compoundSurname) !== false) {
var csTemp = temp.replace(/\s/g, '');
for (var _i = 0, compoundSurnames_1 = compoundSurnames; _i < compoundSurnames_1.length; _i++) {
var surname_1 = compoundSurnames_1[_i];
var surnameLength = surname_1.length;
if (csTemp.length <= surnameLength) {
continue;
}
if (csTemp.startsWith(surname_1)) {
var givenName_1 = csTemp.substring(surnameLength).replace(/\s/g, '');
results.push({ surname: surname_1, givenName: givenName_1 });
break;
}
}
}
var surname = temp.substring(0, 1).replace(/\s/g, '');
var givenName = temp.substring(1).replace(/\s/g, '');
results.push({ surname: surname, givenName: givenName });
return results;
};
KoreanName.splitCandidate = function (fullName, option) {
var _a;
return ((_a = this.splitCandidates(fullName, option)) === null || _a === void 0 ? void 0 : _a[0]) || null;
};
KoreanName.randomSurname = function (character, compoundSurname) {
if (character === void 0) { character = 'hangul'; }
if (compoundSurname === void 0) { compoundSurname = true; }
switch (character) {
case "hangul": return Random.getRandomItem(hangulSurnames);
case "chinese": return Random.getRandomItem(chineseSurnames);
case "all": {
var characters = ["hangul", "chinese"];
return this.randomSurname(Random.getRandomItem(characters));
}
}
};
KoreanName.randomGivenName = function (type) {
if (type === void 0) { type = 'all'; }
var candidates;
switch (type) {
case "unisex": {
candidates = unisexNames;
break;
}
case "masculine": {
candidates = masculineNames;
break;
}
case "feminine": {
candidates = feminineNames;
break;
}
case "all":
default: {
candidates = unisexNames.concat(masculineNames).concat(feminineNames);
}
}
return Random.getRandomItem(candidates);
};
KoreanName.randomFullName = function (givenNameType) {
if (givenNameType === void 0) { givenNameType = 'all'; }
return this.randomSurname() + this.randomGivenName(givenNameType);
};
// RegExp
KoreanName.REG_EXP_NOT_HANGUL_AND_CHINESE_AND_SPACE = /[^\u3131-\u314E\uAC00-\uD7A3\u4E00-\u9FFF\s]/g;
return KoreanName;
}());