@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
218 lines • 7.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const crypto = __importStar(require("crypto"));
String.prototype.encrypt = function ({ key, ivKey }) {
const raw = this.valueOf();
const iv = Buffer.from(ivKey);
const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(key), iv);
let encrypted = cipher.update(raw);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted.toString("hex");
};
String.prototype.decrypt = function ({ key, ivKey }) {
const encrypted = this.valueOf();
const iv = Buffer.from(ivKey);
const encryptedText = Buffer.from(encrypted, "hex");
const decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
};
String.prototype.toZenkakuNumericAndAlphabet = function () {
return this.valueOf().replace(/[A-Za-z0-9]/g, (s) => {
return String.fromCharCode(s.charCodeAt(0) + 65248);
});
};
String.prototype.toHankakuNumericAndAlphabet = function () {
return this.valueOf().replace(/[A-Za-z0-9]/g, (s) => {
return String.fromCharCode(s.charCodeAt(0) - 65248);
});
};
String.prototype.toHiragana = function () {
return this.valueOf().replace(/[\u30a1-\u30f6]/g, (s) => {
return String.fromCharCode(s.charCodeAt(0) - 0x60);
});
};
String.prototype.toKatakana = function () {
return this.valueOf().replace(/[\u3041-\u3096]/g, (s) => {
return String.fromCharCode(s.charCodeAt(0) + 0x60);
});
};
String.prototype.toZenkakuKatakana = function () {
let text = this.valueOf();
text = text.replace(/[ウカ-トハ-ホ]゙/g, (s) => {
var _a;
const dakuten = {
'ヴ': 'ヴ',
'ガ': 'ガ',
'ギ': 'ギ',
'グ': 'グ',
'ゲ': 'ゲ',
'ゴ': 'ゴ',
'ザ': 'ザ',
'ジ': 'ジ',
'ズ': 'ズ',
'ゼ': 'ゼ',
'ゾ': 'ゾ',
'ダ': 'ダ',
'ヂ': 'ヂ',
'ヅ': 'ヅ',
'デ': 'デ',
'ド': 'ド',
'バ': 'バ',
'ビ': 'ビ',
'ブ': 'ブ',
'ベ': 'ベ',
'ボ': 'ボ',
};
return (_a = dakuten[s]) !== null && _a !== void 0 ? _a : s;
});
text = text.replace(/[ハ-ホ]゚/g, (s) => {
var _a;
const handakuten = {
'パ': 'パ',
'ピ': 'ピ',
'プ': 'プ',
'ペ': 'ペ',
'ポ': 'ポ',
};
return (_a = handakuten[s]) !== null && _a !== void 0 ? _a : s;
});
text = text.replace(/[ヲ-ンー]/g, (s) => {
var _a;
const other = {
'ア': 'ア',
'イ': 'イ',
'ウ': 'ウ',
'エ': 'エ',
'オ': 'オ',
'ァ': 'ァ',
'ィ': 'ィ',
'ゥ': 'ゥ',
'ェ': 'ェ',
'ォ': 'ォ',
'カ': 'カ',
'キ': 'キ',
'ク': 'ク',
'ケ': 'ケ',
'コ': 'コ',
'サ': 'サ',
'シ': 'シ',
'ス': 'ス',
'セ': 'セ',
'ソ': 'ソ',
'タ': 'タ',
'チ': 'チ',
'ツ': 'ツ',
'テ': 'テ',
'ト': 'ト',
'ナ': 'ナ',
'ニ': 'ニ',
'ヌ': 'ヌ',
'ネ': 'ネ',
'ノ': 'ノ',
'ハ': 'ハ',
'ヒ': 'ヒ',
'フ': 'フ',
'ヘ': 'ヘ',
'ホ': 'ホ',
'マ': 'マ',
'ミ': 'ミ',
'ム': 'ム',
'メ': 'メ',
'モ': 'モ',
'ヤ': 'ヤ',
'ユ': 'ユ',
'ヨ': 'ヨ',
'ラ': 'ラ',
'リ': 'リ',
'ル': 'ル',
'レ': 'レ',
'ロ': 'ロ',
'ワ': 'ワ',
'ヲ': 'ヲ',
'ン': 'ン',
'ッ': 'ッ',
'ャ': 'ャ',
'ュ': 'ュ',
'ョ': 'ョ',
'ー': 'ー',
};
return (_a = other[s]) !== null && _a !== void 0 ? _a : s;
});
return text;
};
String.prototype.splitByCharacter = function () {
return this.valueOf().split("");
};
String.prototype.splitByBigram = function () {
const text = this.valueOf();
const result = [];
for (let i = 0; i < text.length - 1; i++) {
result.push(text.substring(i, Math.min(i + 2, text.length)));
}
return result;
};
String.prototype.splitByCharacterAndBigram = function () {
return [...this.splitByCharacter(), ...this.splitByBigram()];
};
String.prototype.splitByTrigram = function () {
const text = this.valueOf();
const result = [];
for (let i = 0; i < text.length - 2; i++) {
result.push(text.substring(i, Math.min(i + 3, text.length)));
}
return result;
};
String.prototype.removeOnlyEmoji = function () {
return this.valueOf().replace(/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F700}-\u{1F77F}\u{1F780}-\u{1F7FF}\u{1F800}-\u{1F8FF}\u{1F900}-\u{1F9FF}\u{1FA00}-\u{1FA6F}\u{1FA70}-\u{1FAFF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/gu, '');
};
String.prototype.toSearchableMap = function () {
const text = this.valueOf();
return text.replace(/ /g, " ")
.split(" ")
.flatMap((e) => e
.toLowerCase()
.replace(/\./g, "")
.toHankakuNumericAndAlphabet()
.toZenkakuKatakana()
.toKatakana()
.removeOnlyEmoji()
.splitByCharacterAndBigram())
.filter((value, index, self) => self.indexOf(value) === index)
.reduce((acc, e) => (Object.assign(Object.assign({}, acc), { [e]: true })), {});
};
//# sourceMappingURL=string.extension.js.map