/** * @author john19696 [john19696@protonmail.com] * @copyright Crown Copyright 2021 * @license Apache-2.0 */ import Operation from "../Operation.mjs"; // import OperationError from "../errors/OperationError.mjs"; /** * Escape Smart Characters operation */ class EscapeSmartCharacters extends Operation { /** * EscapeSmartCharacters constructor */ constructor() { super(); this.name = "Escape Smart Characters"; this.module = "Default"; this.description = "An operation to convert smart characters (quotes, dashes, apostrophes, \ arrows, copyright signs, ellipses etc.) back to plain ASCII.\
"; this.infoURL = "http://unicode.scarfboy.com/?s=quotation+mark"; this.inputType = "string"; this.outputType = "string"; this.args = [ /* Arguments. See the project wiki for full details.*/ { name: "Second arg", type: "option", value: ["Escape", "Remove", "Replace with '.'"] } ]; } /** * @param {string} input * @param {Object[]} args * @returns {string} */ run(input, args) { const [outArg] = args; /* JSON map of characters to escape generated by EscapeSmartCharacters.xls in doc */ const ESCAPE_MAP = { // Pasted from git:/doc/SmartEscape.xlsx "‘": "'", "’": "'", "“": "\"", "”": "\"", "©": "(C)", "®": "(R)", "™": "(TM)", "→": "-->", "←": "<--", "↔": "<->", "‐": "-", "‑": "-", "‒": "-", "–": "-", "—": "--", "―": "--", "‖": "||", "‗": "==", "‘": "'", "’": "'", "‚": "'", "‛": "'", "“": "\"", "”": "\"", "„": "\"", "‟": "\"", "•": ".", "‣": ">", "․": ".", "‥": "..", "…": "...", "‧": ".", "‰": "%0", "‱": "%00", "′": "'", "″": "''", "‴": "'''", "‵": "''", "‶": "''", "‷": "'''", "‸": "^", "‹": "<", "›": ">", "‼": "!!", "‽": "?!", "⁃": "-", "⁄": "/", "⁅": "[-", "⁆": "-]", "⁇": "??", "⁈": "?!", "⁉": "!?", "⁌": ".", "⁍": ".", "⁎": "*", "⁏": ";", "⁒": "%", "⁓": "~", "⁕": "*", "⁗": "''''", "⁢": "*", "⁤": "+", }; let output, result = ""; for (const char of input) { if (Object.keys(ESCAPE_MAP).includes(char)) { output = ESCAPE_MAP[char]; } else { // Do Something Sensible with the rest of the arrows const charCode = char.charCodeAt(0); if (charCode >= 8592 && charCode <= 8703) { output = "->"; } else { output = char; } } if (output == char) { result += output; } else { switch (outArg) { case "Remove": break; case "Replace with '.'": result += "."; break; default: { // Escape & no change are the same result += output; } } } } return result; } } export default EscapeSmartCharacters;