UNPKG

@favware/zalgo

Version:
1 lines 9.55 kB
{"version":3,"file":"index.mjs","sources":["../src/chars.ts","../src/utils.ts","../src/zalgo.ts"],"sourcesContent":["/* eslint-disable max-len */\nimport type { Chars } from './interfaces';\n\n/**\n * Supported Zalgo characters, based on {@link https://metacpan.org/pod/Acme::Zalgo | Acme::Zalgo}\n *\n * ```ts\n * const { chars } = require('@favware/zalgo');\n *\n * console.log(chars);\n * ```\n */\nexport const chars: Chars = {\n up: [\n '̍',\n '̎',\n '̄',\n '̅',\n '̿',\n '̑',\n '̆',\n '̐',\n '͒',\n '͗',\n '͑',\n '̇',\n '̈',\n '̊',\n '͂',\n '̓',\n '̈́',\n '͊',\n '͋',\n '͌',\n '̃',\n '̂',\n '̌',\n '͐',\n '̀',\n '́',\n '̋',\n '̏',\n '̒',\n '̓',\n '̔',\n '̽',\n '̉',\n 'ͣ',\n 'ͤ',\n 'ͥ',\n 'ͦ',\n 'ͧ',\n 'ͨ',\n 'ͩ',\n 'ͪ',\n 'ͫ',\n 'ͬ',\n 'ͭ',\n 'ͮ',\n 'ͯ',\n '̾',\n '͛',\n '͆',\n '̚'\n ],\n middle: ['̕', '̛', '̀', '́', '͘', '̡', '̢', '̧', '̨', '̴', '̵', '̶', '͏', '͜', '͝', '͞', '͟', '͠', '͢', '̸', '̷', '͡', '҉'],\n down: [\n '̖',\n '̗',\n '̘',\n '̙',\n '̜',\n '̝',\n '̞',\n '̟',\n '̠',\n '̤',\n '̥',\n '̦',\n '̩',\n '̪',\n '̫',\n '̬',\n '̭',\n '̮',\n '̯',\n '̰',\n '̱',\n '̲',\n '̳',\n '̹',\n '̺',\n '̻',\n '̼',\n 'ͅ',\n '͇',\n '͈',\n '͉',\n '͍',\n '͎',\n '͓',\n '͔',\n '͕',\n '͖',\n '͙',\n '͚',\n '̣'\n ]\n};\n\nchars.all = [...chars.up, ...chars.middle, ...chars.down];\nchars.pattern = RegExp(`(${chars.all.join('|')})`, 'g');\n","/**\n * Splits a string into unicode compatible characters\n *\n * @param splittable The text to start splitting\n */\nexport const unicodeStringSplitter = (splittable: string): string[] => {\n // eslint-disable-next-line max-len, no-misleading-character-class\n const multicharRegex =\n /([\\uD800-\\uDBFF])([\\uDC00-\\uDFFF])([\\uD800-\\uDBFF])?([\\uDC00-\\uDFFF])?|([0-9])?([\\0-\\u02FF\\u0370-\\u1AAF\\u1B00-\\u1DBF\\u1E00-\\u20CF\\u2100-\\uD7FF\\uE000-\\uFE1F\\uFE30-\\uFFFF]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])|(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF])([\\u0300-\\u036F\\u1AB0-\\u1AFF\\u1DC0-\\u1DFF\\u20D0-\\u20FF\\uFE20-\\uFE2F\\uFE0F]+)/g;\n const characters = splittable.replace(multicharRegex, '•').split('');\n\n let m: RegExpExecArray | null;\n let ri = 0;\n\n // eslint-disable-next-line no-cond-assign\n while ((m = multicharRegex.exec(splittable))) {\n m.index -= ri;\n ri += m[0].length - 1;\n characters.splice(m.index, 1, m[0]);\n }\n\n return characters;\n};\n\n/**\n * Draws a random number given any maximum\n *\n * @param maximum Maximum to randomize towards\n */\nexport const randomizer = (maximum: number): number => ~~(Math.random() * maximum);\n","import { chars } from './chars';\nimport type { ZalgoCountsMap, ZalgoOptions } from './interfaces';\nimport { randomizer, unicodeStringSplitter } from './utils';\n\n/**\n * Zalgofies any given text\n *\n * @param text Input text to zalgolize\n * @param options Options for the Zalgo\n * @returns The door to hell\n * @example\n * ```ts\n * zalgo('some text') // ==> ŝ̜̩͇̼̥̼́̏͢o͎͊͜ḿ̛̩̳̖͕̞̩̭ͪe͖̺̣̹̺̋̀͛̽͝ ̖͍̭͓̯̠͑͑͢t̼̪̋͌͢eͯ̋͏͖͎͍̩̭̮x̢͚̄̾̀̈ͧ̓ͩ̚t̪ͫ͝\n *```\n */\nexport const zalgo = (\n text: string,\n options: ZalgoOptions = {\n up: true,\n middle: true,\n down: true,\n size: ''\n }\n): string => {\n try {\n if (!text) throw new Error('no_input');\n if (typeof text !== 'string') throw new Error('not_a_string');\n if (!Reflect.has(options, 'up')) options.up = true;\n if (!Reflect.has(options, 'middle')) options.middle = true;\n if (!Reflect.has(options, 'down')) options.down = true;\n if (!Reflect.has(options, 'size')) options.size = '';\n\n const splitText = unicodeStringSplitter(text);\n const splitTextLength = splitText.length;\n\n let counts: ZalgoCountsMap;\n let result = '';\n const types: string[] = [];\n\n if (options.up) types.push('up');\n if (options.middle) types.push('middle');\n if (options.down) types.push('down');\n\n for (let i = 0; i < splitTextLength; i++) {\n if (chars.pattern!.test(splitText[i])) continue;\n\n // Skip Emojis\n if (splitText[i].length > 1) {\n result += splitText[i];\n continue;\n }\n\n counts = { up: 0, middle: 0, down: 0 };\n\n if (options.size === 'mini') counts = { up: randomizer(8), middle: randomizer(2), down: randomizer(8) };\n else if (options.size === 'maxi') counts = { up: randomizer(16) + 3, middle: randomizer(4) + 1, down: randomizer(64) + 3 };\n else counts = { up: randomizer(8) + 1, middle: randomizer(3), down: randomizer(8) + 1 };\n\n result += text[i];\n\n for (let j = 0, m = types.length; j < m; j++) {\n const type = types[j];\n let count = counts[type];\n const tchars = chars[type] as string[];\n const max = tchars.length - 1;\n\n while (count--) {\n result += tchars[randomizer(max)];\n }\n }\n }\n\n return result;\n } catch (err) {\n if (/(?:no_input)/i.test((err as Error).message)) throw SyntaxError('The zalgo function at least requires some text as input!');\n if (/(?:not_a_string)/i.test((err as Error).message)) throw new SyntaxError('The zalgo function expects input of type string as first argument!');\n throw err;\n }\n};\n\n/**\n * De-zalgolize any text\n * @param purgeable Text to remove zalgo from\n * @returns The door to heaven\n * @example\n * ```ts\n * banish('ŝ̜̩͇̼̥̼́̏͢o͎͊͜ḿ̛̩̳̖͕̞̩̭ͪe͖̺̣̹̺̋̀͛̽͝ ̖͍̭͓̯̠͑͑͢t̼̪̋͌͢eͯ̋͏͖͎͍̩̭̮x̢͚̄̾̀̈ͧ̓ͩ̚t̪ͫ͝') // ==> some text\n * ```\n */\nexport const banish = (purgeable: string): string => {\n try {\n if (!purgeable) throw new Error('no_input');\n if (typeof purgeable !== 'string') throw new Error('not_a_string');\n\n return purgeable.replace(chars.pattern!, '');\n } catch (err) {\n if (/(?:no_input)/i.test((err as Error).message)) throw new SyntaxError('The banish function at least requires some text as input!');\n if (/(?:not_a_string)/i.test((err as Error).message))\n throw new SyntaxError('The banish function expects input of type string as first argument!');\n throw err;\n }\n};\n"],"names":["chars","up","middle","down","all","pattern","RegExp","join","randomizer","maximum","Math","random","zalgo","text","options","size","Error","Reflect","has","splitText","splittable","multicharRegex","characters","replace","split","m","ri","exec","index","length","splice","unicodeStringSplitter","splitTextLength","counts","result","types","push","i","test","j","type","count","tchars","max","err","message","SyntaxError","banish","purgeable"],"mappings":"MAYaA,EAAe,CAC1BC,GAAI,CACF,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,KAEFC,OAAQ,CAAC,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,IAAK,KACvHC,KAAM,CACJ,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,IACA,MAIJH,EAAMI,IAAM,IAAIJ,EAAMC,MAAOD,EAAME,UAAWF,EAAMG,MACpDH,EAA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