Source: lib/funTranslators.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2018 Campbell Crowley. All rights reserved.
// Author: Campbell Crowley (dev@campbellcrowley.com)

/**
 * @classdesc Converts text strings into different formats.
 * @class
 */
function FunTranslators() {
  const self = this;
  /**
   * Convert a string to a format based on it's name.
   *
   * @public
   *
   * @param {string} name The name of the translator.
   * @param {string} input The string to convert.
   * @returns {string} The formatted string.
   */
  this.to = function(name, input) {
    switch (name) {
      default:
        return input;
      case 'leet':
        return self.toLeetSpeak(input);
      case 'mocking':
        return self.toMockingFont(input);
      case 'smallcaps':
        return self.toSmallCaps(input);
      case 'superscript':
        return self.toSuperScript(input);
    }
  };
  /**
   * Convert a string to Leet Speak (1337 5p34k).
   *
   * @public
   *
   * @param {string} input The string to convert.
   * @returns {string} The formatted string.
   */
  this.toLeetSpeak = function(input) {
    let output = input.replace(/cker(s?)\b/g, 'xor$1');
    output = output.replace(/ate/g, '8');
    output = output.replace(/and/g, '&');
    output = output.replace(/[lL]|[eE]|[aA]|[sS]|[gG]|[tT]|[oO]/g, (m) => {
      m = m.toLowerCase();
      switch (m) {
        case 'l':
          return '1';
        case 'e':
          return '3';
        case 'a':
          return '4';
        case 's':
          return '5';
        case 'g':
          return '6';
        case 't':
          return '7';
        case 'o':
          return '0';
      }
    });
    return output;
  };

  /**
   * Convert a string to the SpongeBob mocking meme font (SpOngEBoB MoCKinG).
   *
   * @public
   *
   * @param {string} input The string to convert.
   * @returns {string} The formatted string.
   */
  this.toMockingFont = function(input) {
    const output = input.toLowerCase().split('');
    for (let i = 0; i < output.length / 2; i++) {
      const rand = Math.floor(Math.random() * output.length);
      output[rand] = output[rand].toUpperCase();
    }
    return output.join('');
  };

  /**
   * Convert string to small caps (Hᴇʟʟᴏ Wᴏʀʟᴅ!).
   *
   * @public
   *
   * @param {string} input The string to convert.
   * @returns {string} The formatted string.
   */
  this.toSmallCaps = function(input) {
    const map = {
      '0': '0',
      '1': '1',
      '2': '2',
      '3': '3',
      '4': '4',
      '5': '5',
      '6': '6',
      '7': '7',
      '8': '8',
      '9': '9',
      'a': 'ᴀ',
      'b': 'ʙ',
      'c': 'ᴄ',
      'd': 'ᴅ',
      'e': 'ᴇ',
      'f': 'ғ',
      'g': 'ɢ',
      'h': 'ʜ',
      'i': 'ɪ',
      'j': 'ᴊ',
      'k': 'ᴋ',
      'l': 'ʟ',
      'm': 'ᴍ',
      'n': 'ɴ',
      'o': 'ᴏ',
      'p': 'ᴘ',
      'q': 'ǫ',
      'r': 'ʀ',
      's': 's',
      't': 'ᴛ',
      'u': 'ᴜ',
      'v': 'ᴠ',
      'w': 'ᴡ',
      'x': 'x',
      'y': 'ʏ',
      'z': 'ᴢ',
      'A': 'A',
      'B': 'B',
      'C': 'C',
      'D': 'D',
      'E': 'E',
      'F': 'F',
      'G': 'G',
      'H': 'H',
      'I': 'I',
      'J': 'J',
      'K': 'K',
      'L': 'L',
      'M': 'M',
      'N': 'N',
      'O': 'O',
      'P': 'P',
      'Q': 'Q',
      'R': 'R',
      'S': 'S',
      'T': 'T',
      'U': 'U',
      'V': 'V',
      'W': 'W',
      'X': 'X',
      'Y': 'Y',
      'Z': 'Z',
    };
    let output = '';
    for (let i = 0; i < input.length; i++) {
      output += map[input[i]] || input[i];
    }
    return output;
  };

  /**
   * Convert string to superscript characters (ᴴᵉˡˡᵒ ᵂᵒʳˡᵈᵎ).
   *
   * @public
   *
   * @param {string} input The string to convert.
   * @returns {string} The formatted string.
   */
  this.toSuperScript = function(input) {
    const map = {
      '0': '⁰',
      '1': '¹',
      '2': '²',
      '3': '³',
      '4': '⁴',
      '5': '⁵',
      '6': '⁶',
      '7': '⁷',
      '8': '⁸',
      '9': '⁹',
      'a': 'ᵃ',
      'b': 'ᵇ',
      'c': 'ᶜ',
      'd': 'ᵈ',
      'e': 'ᵉ',
      'f': 'ᶠ',
      'g': 'ᵍ',
      'h': 'ʰ',
      'i': 'ᶦ',
      'j': 'ʲ',
      'k': 'ᵏ',
      'l': 'ˡ',
      'm': 'ᵐ',
      'n': 'ⁿ',
      'o': 'ᵒ',
      'p': 'ᵖ',
      'q': 'ᑫ',
      'r': 'ʳ',
      's': 'ˢ',
      't': 'ᵗ',
      'u': 'ᵘ',
      'v': 'ᵛ',
      'w': 'ʷ',
      'x': 'ˣ',
      'y': 'ʸ',
      'z': 'ᶻ',
      'A': 'ᴬ',
      'B': 'ᴮ',
      'C': 'ᶜ',
      'D': 'ᴰ',
      'E': 'ᴱ',
      'F': 'ᶠ',
      'G': 'ᴳ',
      'H': 'ᴴ',
      'I': 'ᴵ',
      'J': 'ᴶ',
      'K': 'ᴷ',
      'L': 'ᴸ',
      'M': 'ᴹ',
      'N': 'ᴺ',
      'O': 'ᴼ',
      'P': 'ᴾ',
      'Q': 'Q',
      'R': 'ᴿ',
      'S': 'ˢ',
      'T': 'ᵀ',
      'U': 'ᵁ',
      'V': 'ⱽ',
      'W': 'ᵂ',
      'X': 'ˣ',
      'Y': 'ʸ',
      'Z': 'ᶻ',
      '+': '⁺',
      '-': '⁻',
      '=': '⁼',
      '(': '⁽',
      ')': '⁾',
      '?': 'ˀ',
      '!': 'ᵎ',
    };
    let output = '';
    for (let i = 0; i < input.length; i++) {
      output += map[input[i]] || input[i];
    }
    return output;
  };
}

module.exports = new FunTranslators();