UNPKG

string-to-unicode-variant

Version:

Javascript function to convert a string into different kind of ⓤⓝⓘⓒⓞⓓⓔ variants.

613 lines (611 loc) 21.4 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // toUnicodeVariant.js var require_toUnicodeVariant = __commonJS({ "toUnicodeVariant.js"(exports, module) { function toUnicodeVariant2(str, variant, combinings) { const string = String.fromCodePoint; const offsets = { m: [120432, 120822], b: [119808, 120782], i: [119860, 48], bi: [119912, 48], c: [119964, 48], bc: [120016, 48], g: [120068, 48], d: [120120, 120792], bg: [120172, 48], s: [120224, 120802], bs: [120276, 120812], is: [120328, 48], bis: [120380, 48], o: [9398, 9311], on: [127312, 9311], p: [127248, 120822], q: [127280, 48], qn: [127344, 48], w: [65313, 65296], // f: [127462, 120822], nd: [120432, 9351], nc: [120432, 127233], ndc: [120432, 9460], r: [120432, 9460] }; const variantOffsets = { "monospace": "m", "bold": "b", "italic": "i", "bold italic": "bi", "script": "c", "bold script": "bc", "gothic": "g", "gothic bold": "bg", "doublestruck": "d", "sans": "s", "bold sans": "bs", "italic sans": "is", "bold italic sans": "bis", "parenthesis": "p", "circled": "o", "circled negative": "on", "squared": "q", "squared negative": "qn", "fullwidth": "w", // "flags": "f", "numbers dot": "nd", "numbers comma": "nc", "numbers double circled": "ndc", "roman": "r" }; const special = { m: { " ": 8192, "-": 8211 }, i: { "h": 8462 }, c: { "B": 8492, "E": 8496, "F": 8497, "H": 8459, "I": 8464, "L": 8466, "M": 8499, "R": 8475, "e": 120046, "g": 120048, "o": 120056 }, g: { "C": 8493, "H": 8460, "I": 8465, "R": 8476, "Z": 8488 }, d: { "C": 8450, "H": 8461, "N": 8469, "P": 8473, "Q": 8474, "R": 8477, "Z": 8484 }, o: { "0": 9450, "10": 9321, "11": 9322, "12": 9323, "13": 9324, "14": 9325, "15": 9326, "16": 9327, "17": 9328, "18": 9329, "19": 9330, "20": 9331 }, on: { "0": 9471, "11": 9451, "12": 9452, "13": 9453, "14": 9454, "15": 9455, "16": 9456, "17": 9457, "18": 9458, "19": 9459, "20": 9460 }, p: { "1": 9332, "2": 9333, "3": 9334, "4": 9335, "5": 9336, "6": 9337, "7": 9338, "8": 9339, "9": 9340, "10": 9341, "11": 9342, "12": 9343, "13": 9344, "14": 9345, "15": 9346, "16": 9347, "17": 9348, "18": 9349, "19": 9350, "20": 9351 }, q: { "hv": 127306, "mv": 127307, "sd": 127308, "ss": 127309, "ppv": 127310, "wc": 127311, "cl": 127377, "cool": 127378, "free": 127379, "id": 127380, "new": 127381, "ng": 127382, "ok": 127383, "sos": 127384, "up!": 127385, "vs": 127386, "3d": 127387, "2ndscr": 127388, "2k": 127389, "4k": 127390, "8k": 127391, "5.1": 127392, "7.1": 127393, "22.2": 127394, "60p": 127395, "120p": 127396, "d": 127397, "hc": 127398, "hdr": 127399, "hi-res": 127400, "loss-less": 127401, "shv": 127402, "uhd": 127403, "vod": 127404 }, qn: { "ic": 127371, "pa": 127372, "sa": 127373, "ab": 127374, "wc": 127375 }, w: { "!": 65281, '"': 65282, "#": 65283, "$": 65284, "%": 65285, "&": 65286, "'": 65287, "(": 65288, ")": 65289, "*": 65290, "+": 65291, ",": 65292, "-": 65293, ".": 65294, "/": 65295, ":": 65306, ";": 65307, "<": 65308, "=": 65309, ">": 65310, "?": 65311, "@": 65312, "\\": 65340, "[": 65339, "]": 65341, "^": 65342, "_": 65343, "`": 65344, "{": 65371, "|": 65372, "}": 65373, "~": 65374, "\u2985": 65375, "\u2986": 65376, "\uFFE0": 65504, "\uFFE1": 65505, "\xA6": 65508, "\uFFE5": 65509, "\uFFE6": 65510, "\uFF70": 65392, "\uFF61": 65392, "\uFF64": 65380, "\uFF65": 65381, "\uFFE3": 65507, "\xAC": 65506 }, f: {}, nd: { "0": 127232, "10": 9361, "11": 9362, "12": 9363, "13": 9364, "14": 9365, "15": 9366, "16": 9367, "17": 9368, "18": 9369, "19": 9370, "20": 9371 }, ndc: { "0": 120822, "10": 9470 }, r: { "I": 8544, "II": 8545, "III": 8546, "IV": 8547, "V": 8548, "VI": 8549, "VII": 8550, "VIII": 8551, "IX": 8552, "X": 8553, "XI": 8554, "XII": 8555, "L": 8556, "C": 8557, "D": 8558, "M": 8559, "i": 8560, "ii": 8561, "iii": 8562, "iv": 8563, "v": 8564, "vi": 8565, "vii": 8566, "viii": 8567, "ix": 8568, "x": 8569, "xi": 8570, "xii": 8571, "l": 8572, "c": 8573, "d": 8574, "m": 8575 } }; for (var i = 97; i <= 122; i++) { special["p"][String.fromCharCode(i)] = 9372 + (i - 97); special["w"][String.fromCharCode(i)] = 65345 + (i - 97); } ; ["on", "q", "qn", "f"].forEach((t) => { for (var i2 = 97; i2 <= 122; i2++) { special[t][String.fromCharCode(i2)] = offsets[t][0] + (i2 - 97); } }); const diacritics = { "strike": { "short": "s", "code": 822 }, "strike-curly": { "short": "sc", "code": 820 }, "underline": { "short": "u", "code": 818 }, "underline-curly": { "short": "uc", "code": 816 }, "underline-sm": { "short": "u-sm", "code": 800 }, "underline-double": { "short": "ud", "code": 819 }, "underline-double-sm": { "short": "ud-sm", "code": 839 }, "overline": { "short": "o", "code": 773 }, "overline-curly": { "short": "oc", "code": 771 }, "overline-sm": { "short": "o-sm", "code": 772 }, "overline-double": { "short": "od", "code": 831 }, "slash": { "short": "sl", "code": 824 }, "cross-above": { "short": "ca", "code": 829 }, "plus-below": { "short": "pb", "code": 799 }, "a-above": { "short": "a-a", "code": 867 }, "c-above": { "short": "c-a", "code": 872 }, "d-above": { "short": "d-a", "code": 873 }, "e-above": { "short": "e-a", "code": 868 }, "h-above": { "short": "h-a", "code": 874 }, "i-above": { "short": "i-a", "code": 869 }, "m-above": { "short": "m-a", "code": 875 }, "o-above": { "short": "o-a", "code": 870 }, "r-above": { "short": "r-a", "code": 876 }, "u-above": { "short": "u-a", "code": 871 }, "v-above": { "short": "v-a", "code": 878 }, "x-above": { "short": "x-a", "code": 879 }, "halo-breve": { "short": "hb", "code": 1160 }, "halo-grave": { "short": "hg", "code": 1161 }, "enclose-circle": { "short": "en-c", "code": 8413 }, "enclose-backslash": { "short": "en-cb", "code": 8416 }, "enclose-circle-backslash": { "short": "en-cb", "code": 8416 }, "enclose-square": { "short": "en-s", "code": 8414 }, "enclose-diamond": { "short": "en-d", "code": 8415 }, "enclose-screen": { "short": "en-scr", "code": 8418 }, "enclose-keycap": { "short": "en-key", "code": 8419 }, //diacritics supporting special chars "diaeresis": { "code": 776 }, "caron": { "code": 780 }, "perispomeni": { "code": 834 }, "tilde": { "code": 771 }, "tildesm": { "code": 732 }, "circumflex": { "code": 770 }, "ringabove": { "code": 778 }, "dotabove": { "code": 775 }, "dotbelow": { "code": 803 }, "grave": { "code": 832 }, "gravedouble": { "code": 783 }, "acute": { "code": 833 }, "breve": { "code": 774 }, "breveinverted": { "code": 785 }, "commabelow": { "code": 806 }, "macron": { "code": 772 }, "cedilla": { "code": 807 }, "ogonek": { "code": 808 }, "solidus": { "code": 824 }, "solidussm": { "code": 823 }, //spacing combinings "space-zero": { "code": 65279 }, "space-hair": { "code": 8202 }, "space-thin": { "code": 8201 }, "space": { "code": 32 }, "space-en": { "code": 8192 }, "space-figure": { "code": 8199 }, "space-cjk": { "code": 12288 }, "space-em": { "code": 8193 }, "space-ogham": { "code": 5760 }, //combining grapheme joiner "CGJ": { "code": 847 } }; const special_chars = { "\xE4": { "char": "a", "combine": string(diacritics.diaeresis.code) }, "\xE2": { "char": "a", "combine": string(diacritics.circumflex.code) }, "\xE1": { "char": "a", "combine": string(diacritics.acute.code) }, "\xE5": { "char": "a", "combine": string(diacritics.ringabove.code) }, "\u0103": { "char": "a", "combine": string(diacritics.breve.code) }, "\u01DF": { "char": "a", "combine": string(diacritics.diaeresis.code) + string(diacritics.macron.code) }, "\xE3": { "char": "a", "combine": string(diacritics.tilde.code) }, "\u0101": { "char": "a", "combine": string(diacritics.macron.code) }, "\u0227": { "char": "a", "combine": string(diacritics.dotabove.code) }, "\u0203": { "char": "a", "combine": string(diacritics.breveinverted.code) }, "\u1E05": { "char": "b", "combine": string(diacritics.dotbelow.code) }, "\u010D": { "char": "c", "combine": string(diacritics.caron.code) }, "\u0107": { "char": "c", "combine": string(diacritics.acute.code) }, "\xE7": { "char": "c", "combine": string(diacritics.cedilla.code) }, "\u1E09": { "char": "c", "combine": string(diacritics.cedilla.code) + string(diacritics.acute.code) }, "\u010B": { "char": "c", "combine": string(diacritics.dotabove.code) }, "\u0109": { "char": "c", "combine": string(diacritics.circumflex.code) }, "\xE8": { "char": "e", "combine": string(diacritics.grave.code) }, "\xE9": { "char": "e", "combine": string(diacritics.acute.code) }, "\u0113": { "char": "e", "combine": string(diacritics.macron.code) }, "\u0115": { "char": "e", "combine": string(diacritics.breve.code) }, "\xEB": { "char": "e", "combine": string(diacritics.diaeresis.code) }, "\u011B": { "char": "e", "combine": string(diacritics.caron.code) }, "\u0119": { "char": "e", "combine": string(diacritics.ogonek.code) }, "\u0207": { "char": "e", "combine": string(diacritics.breveinverted.code) }, "\u0205": { "char": "e", "combine": string(diacritics.gravedouble.code) }, "\xEA": { "char": "e", "combine": string(diacritics.circumflex.code) }, "\u011F": { "char": "g", "combine": string(diacritics.breve.code) }, "\u01E7": { "char": "g", "combine": string(diacritics.caron.code) }, "\u0123": { "char": "g", "combine": string(diacritics.cedilla.code) }, "\u0121": { "char": "g", "combine": string(diacritics.dotabove.code) }, "\u1E25": { "char": "h", "combine": string(diacritics.dotbelow.code) }, "\xEE": { "char": "i", "combine": string(diacritics.circumflex.code) }, "\xED": { "char": "i", "combine": string(diacritics.acute.code) }, "\xEC": { "char": "i", "combine": string(diacritics.grave.code) }, "\u0129": { "char": "i", "combine": string(diacritics.tilde.code) }, "\u1E31": { "char": "k", "combine": string(diacritics.acute.code) }, "\u1E33": { "char": "k", "combine": string(diacritics.dotbelow.code) }, "\u0146": { "char": "n", "combine": string(diacritics.ogonek.code) }, "\u0144": { "char": "n", "combine": string(diacritics.acute.code) }, "\xF5": { "char": "o", "combine": string(diacritics.tilde.code) }, "\xF6": { "char": "o", "combine": string(diacritics.diaeresis.code) }, "\u014D": { "char": "o", "combine": string(diacritics.macron.code) }, "\xF4": { "char": "o", "combine": string(diacritics.circumflex.code) }, "\xF3": { "char": "o", "combine": string(diacritics.acute.code) }, "\xF2": { "char": "o", "combine": string(diacritics.grave.code) }, "\u0155": { "char": "r", "combine": string(diacritics.acute.code) }, "\u015F": { "char": "s", "combine": string(diacritics.cedilla.code) }, "\u0219": { "char": "s", "combine": string(diacritics.commabelow.code) }, "\u1E69": { "char": "s", "combine": string(diacritics.dotbelow.code) + string(diacritics.dotabove.code) }, "\u0161": { "char": "s", "combine": string(diacritics.caron.code) }, "\u015B": { "char": "s", "combine": string(diacritics.acute.code) }, "\xFC": { "char": "u", "combine": string(diacritics.diaeresis.code) }, "\xF9": { "char": "u", "combine": string(diacritics.grave.code) }, "\xFA": { "char": "u", "combine": string(diacritics.acute.code) }, "\xFB": { "char": "u", "combine": string(diacritics.circumflex.code) }, "\u016D": { "char": "u", "combine": string(diacritics.breve.code) }, "\u0215": { "char": "u", "combine": string(diacritics.gravedouble.code) }, "\u0217": { "char": "u", "combine": string(diacritics.breveinverted.code) }, "\u016F": { "char": "u", "combine": string(diacritics.ringabove.code) }, "\u016B": { "char": "u", "combine": string(diacritics.macron.code) }, "\u1E81": { "char": "w", "combine": string(diacritics.grave.code) }, "\u1E83": { "char": "w", "combine": string(diacritics.acute.code) }, "\xF8": { "char": "o", "combine": string(diacritics.solidussm.code) } //mimicks that somehow fails //'c̆': { 'char': 'c', 'combine': string(diacritics.breve.code) }, //'c̈': { 'char': 'c', 'combine': string(diacritics.diaeresis.code) }, //'ę́': { 'char': 'e', 'combine': string(diacritics.ogonek.code) + string(diacritics.acute.code) }, //'m̂': { 'char': 'm', 'combine': string(diacritics.circumflex.code) }, //'n̂': { 'char': 'n', 'combine': string(diacritics.circumflex.code) }, //'ñ': { 'char': 'n', 'combine': string(diacritics.tilde.code) }, //'q̄': { 'char': 'q', 'combine': string(diacritics.macron.code) }, //'r̃': { 'char': 'r', 'combine': string(diacritics.tilde.code) }, //'s̈': { 'char': 's', 'combine': string(diacritics.diaeresis.code) }, }; for (const char of Object.keys(special_chars)) { special_chars[char.toUpperCase()] = { "char": char, "combine": false }; } const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const numbers = "0123456789"; const type = function() { if (variantOffsets[variant]) return variantOffsets[variant]; if (offsets[variant]) return variant; return "m"; }(); const combine_with = function() { let array = null; if (Array.isArray(combinings)) array = combinings; if (typeof combinings === "string") array = combinings.split(","); if (!array) return false; let result2 = ""; array.forEach(function(diacritic) { diacritic = diacritic.trim().toLowerCase(); for (const d in diacritics) { if (diacritic === d || diacritic === diacritics[d].short) { result2 += string(diacritics[d].code); } } }); return result2; }(); if (typeof str === "string" && special[type] && (special[type][str] || special[type][str.toLowerCase()])) { return special[type][str] ? string(special[type][str]) : string(special[type][str.toLowerCase()]); } if (["roman", "r"].includes(type)) { if (typeof str === "number") { const parts = { M: 1e3, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }; let roman = ""; let num = str; for (let i2 in parts) { while (num >= parts[i2]) { if (special[type][i2]) { roman += i2; } else { for (let d of i2) roman += d; } num -= parts[i2]; } } str = roman; } let result2 = str; const romans = [ "VIII", "viii", "III", "iii", "XII", "xii", "VII", "vii", "IX", "ix", "XI", "xi", "IV", "iv", "VI", "vi", "II", "ii", "I", "i", "D", "d", "M", "m", "L", "l", "V", "v", "C", "c", "X", "x" ]; for (number of romans) { if (result2.indexOf(number.toString()) > -1) { result2 = result2.replaceAll(number, string(special[type][number])); } } return result2; } let result = ""; for (let c of str) { let index; const combine_special = c in special_chars ? special_chars[c].combine : false; c = combine_special ? special_chars[c].char : c.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); if (special[type] && special[type][c]) c = string(special[type][c]); if (type && (index = chars.indexOf(c)) > -1) { result += string(index + offsets[type][0]); } else if (type && (index = numbers.indexOf(c)) > -1) { result += string(index + offsets[type][1]); } else { result += c; } if (combine_special) result += combine_special; if (combine_with) result += combine_with; } return result; } if (typeof module === "object" && module && typeof module.exports === "object") { module.exports = toUnicodeVariant2; } } }); // index.ts var toUnicodeVariant = __toESM(require_toUnicodeVariant()); var string_to_unicode_variant = toUnicodeVariant.default; export { string_to_unicode_variant }; //# sourceMappingURL=index.mjs.map