UNPKG

dotbit

Version:

A complete .bit SDK and utilities in TypeScript

142 lines 5.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountCharsetTypes = exports.graphemesAccount = exports.digitalEmojiUnifiedHandle = exports.trimAccountSuffix = exports.isSubAccount = exports.toRecordExtended = exports.toDottedStyle = exports.isSupportedAccount = void 0; const grapheme_splitter_1 = __importDefault(require("grapheme-splitter")); const emoji_list_json_1 = __importDefault(require("./char_set/emoji_list.json")); const digit_list_json_1 = __importDefault(require("./char_set/digit_list.json")); const en_list_json_1 = __importDefault(require("./char_set/en_list.json")); const tr_list_json_1 = __importDefault(require("./char_set/tr_list.json")); const th_list_json_1 = __importDefault(require("./char_set/th_list.json")); const ko_list_json_1 = __importDefault(require("./char_set/ko_list.json")); const vi_list_json_1 = __importDefault(require("./char_set/vi_list.json")); const const_1 = require("../const"); function isSupportedAccount(account) { return /^([^.\s]+\.){1,}bit$/.test(account) && account.split(/.#/).every(v => Boolean(v.length)); } exports.isSupportedAccount = isSupportedAccount; function toDottedStyle(inputAccount) { if (!isSupportedAccount(inputAccount)) { return inputAccount; } if (!inputAccount.includes('#')) { return inputAccount; } const [account, suffix] = inputAccount.split('.'); const [main, sub] = account.split('#'); return `${sub}.${main}.${suffix}`; } exports.toDottedStyle = toDottedStyle; function toRecordExtended(record) { return Object.assign(Object.assign({}, record), { type: record.key.split('.')[0], subtype: record.key.split('.')[1] }); } exports.toRecordExtended = toRecordExtended; function isSubAccount(account) { const subAccountPattern = /^([^.\s]+\.){2,}bit$/; return subAccountPattern.test(account); } exports.isSubAccount = isSubAccount; function trimAccountSuffix(account) { return account.replace(/\.bit$/, ''); } exports.trimAccountSuffix = trimAccountSuffix; function getLanguageGraphemes(charSplit, language) { const languageToCharList = { en: en_list_json_1.default, tr: tr_list_json_1.default, vi: vi_list_json_1.default, th: th_list_json_1.default, ko: ko_list_json_1.default }; const charList = languageToCharList[language]; const charInfos = []; const charTypes = [const_1.CHAR_TYPE.emoji, const_1.CHAR_TYPE.number, const_1.languageToCharType[language]]; for (const char of charSplit) { let included = false; for (const [index, list] of [emoji_list_json_1.default, digit_list_json_1.default, charList].entries()) { if (list.includes(char)) { charInfos.push({ char_set_name: charTypes[index], char }); included = true; break; } } if (!included) { return null; } } return charInfos; } function getLanguage(language) { for (const languageStr of const_1.languages) { if (language.startsWith(languageStr)) { return language; } } return 'en'; } function digitalEmojiUnifiedHandle(str) { const splitter = new grapheme_splitter_1.default(); const split = splitter.splitGraphemes(str); const list = split.map((item) => { return const_1.DigitalEmojiUnifiedMap[item] || item; }); return list.join(''); } exports.digitalEmojiUnifiedHandle = digitalEmojiUnifiedHandle; function graphemesAccount(account, addSuffix = false, language = 'en') { const splitter = new grapheme_splitter_1.default(); let split = splitter.splitGraphemes(account); split = split.map((item) => { if (const_1.DigitalEmojiUnifiedMap[item]) { return const_1.DigitalEmojiUnifiedMap[item]; } else { return item; } }); language = getLanguage(language); let splitArr = null; const languageList = const_1.languages.filter(lang => lang !== language); languageList.unshift(language); for (const languageItem of languageList) { splitArr = getLanguageGraphemes(split, languageItem); if (splitArr !== null) { break; } } if (!splitArr) { splitArr = split.map((char) => { return { char_set_name: const_1.CHAR_TYPE.unknown, char }; }); } if (addSuffix) { const_1.ACCOUNT_SUFFIX.split('') .forEach((char) => { splitArr.push({ char_set_name: const_1.CHAR_TYPE.english, char }); }); } return splitArr; } exports.graphemesAccount = graphemesAccount; function getAccountCharsetTypes(account) { const name = trimAccountSuffix(account); const graphemes = graphemesAccount(name); const charTypes = {}; graphemes.forEach(grapheme => { charTypes[grapheme.char_set_name] = true; }); return charTypes; } exports.getAccountCharsetTypes = getAccountCharsetTypes; //# sourceMappingURL=account.js.map