UNPKG

koishi-plugin-parenthesis-balancer

Version:

A plugin for Koishi to balance parentheses in messages

165 lines (161 loc) 4.3 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { apply: () => apply }); module.exports = __toCommonJS(index_exports); // src/balancer.ts var normalPairs = [ "()", "[]", "{}", // English parenthesis, brackets, braces "\uFF08\uFF09", "\u239C\u239F", "\u207D\u207E", "\u208D\u208E", "\u239B\u239E", "\u239D\u23A0", "\u2985\u2986", "\u2E28\u2E29", "\u276A\u276B", "\u2E28\u2E29", "\uFD3E\uFD3F", "\uFE5B\uFE5C", "\uFE5D\uFE5E", "\uFF5F\uFF60", // alternative parenthesis "\u27E8\u27E9", "\u2991\u2992", "\u2045\u2046", "\u3008\u3009", "\u276C\u276D", "\u2772\u2773", "\u2774\u2775", "\u27E6\u27E7", "\u27E8\u27E9", "\u27EA\u27EB", "\u27EC\u27ED", "\u2987\u2988", "\u2989\u298A", "\u298B\u298C", "\u298D\u298E", "\u298F\u2990", "\u2991\u2992", "\u2997\u2998", "\u276E\u276F", "\u2826\u2834", // quotation marks that is easier to handle "\u300A\u300B", "\u3008\u3009", "\u300C\u300D", "\uFF62\uFF63", "\u3010\u3011", "\u3014\u3015", "\uFF3B\uFF3D", "\u300E\u300F", "\u3016\u3017", "\uFF5B\uFF5D", // brackets and braces in CJK "\u23DC\u23DD", "\uFE35\uFE36", "\uFE43\uFE44", "\uFE41\uFE42", "\uFE17\uFE18", "\uFE35\uFE36", "\uFE37\uFE38", "\uFE39\uFE3A", "\uFE3B\uFE3C", "\uFE3D\uFE3E", "\uFE3F\uFE40", "\uFE41\uFE42", "\uFE47\uFE48", // vertical symbols "\u2774\u2775", "\u276C\u276D", "\u2768\u2769", "\u276A\u276B", "\u2772\u2773", "\u276E\u276F", // ornament symbols "\xAB\xBB", "\u2039\u203A", "\u0F3A\u0F3B", "\u0F3C\u0F3D", "\u169C\u169B" ]; var PairCannotMatchError = class extends Error { }; function balanceParenthesis(text) { const pairingMap = /* @__PURE__ */ new Map(); const reversePairingMap = /* @__PURE__ */ new Map(); for (const pair of normalPairs) { pairingMap.set(pair[0], pair[1]); reversePairingMap.set(pair[1], pair[0]); } const stack = []; for (let i = 0; i < text.length; i += 1) { const char = text[i]; if (pairingMap.get(char) !== void 0) { stack.push(char); } if (reversePairingMap.get(char) !== void 0) { if (stack.pop() !== reversePairingMap.get(char)) { throw new PairCannotMatchError("WORLD IN CRISIS! I cannot balance your symbols!"); } } } const returnChars = []; for (let i = stack.length - 1; i >= 0; i -= 1) { const char = stack[i]; returnChars.push(pairingMap.get(char)); } return returnChars.join(""); } // src/index.ts function apply(ctx) { ctx.middleware(async (session, next) => { if (false) { const t = session.event._data.message; const retStickerFileID = balancePusheen(session.content); if (retStickerFileID) { session.send("<sticker id=" + retStickerFileID + ">"); } else if (cannotBalance(session.content)) { session.send(`<reply id=${session.messageId}>\u{1F353} IN CRISIS, CANNOT balance your Pusheen!`); } } else if (session.content) { try { const responseText = balanceParenthesis(session.content); if (responseText) session.send(responseText); } catch (e) { if (e instanceof PairCannotMatchError) { session.send(e.message); } else { throw new Error(`Exception ${e} when handling message ${JSON.stringify(session.content, null, 2)}`); } } } await next(); }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { apply });