UNPKG

hangulx

Version:

Providing various utilities for Hangul

103 lines (102 loc) 4.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KoreanName = void 0; var compound_surname_1 = require("./constants/korean-name/compound-surname/compound-surname"); var Random_1 = require("./utils/Random"); var hangul_surnames_1 = require("./constants/korean-name/hangul-surnames"); var chinese_surnames_1 = require("./constants/korean-name/chinese-surnames"); var names_1 = require("./constants/korean-name/names"); var KoreanName = exports.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 = compound_surname_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_1.Random.getRandomItem(hangul_surnames_1.hangulSurnames); case "chinese": return Random_1.Random.getRandomItem(chinese_surnames_1.chineseSurnames); case "all": { var characters = ["hangul", "chinese"]; return this.randomSurname(Random_1.Random.getRandomItem(characters)); } } }; KoreanName.randomGivenName = function (type) { if (type === void 0) { type = 'all'; } var candidates; switch (type) { case "unisex": { candidates = names_1.unisexNames; break; } case "masculine": { candidates = names_1.masculineNames; break; } case "feminine": { candidates = names_1.feminineNames; break; } case "all": default: { candidates = names_1.unisexNames.concat(names_1.masculineNames).concat(names_1.feminineNames); } } return Random_1.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; }());