|
16 | 16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | 18 |
|
19 | | -// Add string methods |
20 | | -/** |
21 | | - * Replaces latin characters with their uppercase versions. |
22 | | - * @return {string} |
23 | | - */ |
24 | | -String.prototype.toLatinUppercase = function() { |
25 | | - return this.replace(/[a-z]/g, function(match) { |
26 | | - return match.toUpperCase() |
27 | | - }) |
28 | | -} |
29 | | - |
30 | | -/** |
31 | | - * Removes the first and last character of a string |
32 | | - * Used to remove enclosing characters like quotes, parentheses, brackets... |
33 | | - * @note Does NOT check for their existence ahead of time. |
34 | | - * @return {string} |
35 | | - */ |
36 | | -String.prototype.removeEnclosure = function() { |
37 | | - return this.substring(1, this.length - 1) |
38 | | -} |
39 | | - |
40 | | -/** |
41 | | - * Rounds to a certain number of decimal places. |
42 | | - * From https://stackoverflow.com/a/48764436 |
43 | | - * |
44 | | - * @param {number} decimalPlaces |
45 | | - * @return {number} |
46 | | - */ |
47 | | -Number.prototype.toDecimalPrecision = function(decimalPlaces = 0) { |
48 | | - const p = Math.pow(10, decimalPlaces) |
49 | | - const n = (this * p) * (1 + Number.EPSILON) |
50 | | - return Math.round(n) / p |
51 | | -} |
52 | | - |
53 | | -const CHARACTER_TO_POWER = new Map([ |
54 | | - ["-", "⁻"], |
55 | | - ["+", "⁺"], |
56 | | - ["=", "⁼"], |
57 | | - [" ", " "], |
58 | | - ["(", "⁽"], |
59 | | - [")", "⁾"], |
60 | | - ["0", "⁰"], |
61 | | - ["1", "¹"], |
62 | | - ["2", "²"], |
63 | | - ["3", "³"], |
64 | | - ["4", "⁴"], |
65 | | - ["5", "⁵"], |
66 | | - ["6", "⁶"], |
67 | | - ["7", "⁷"], |
68 | | - ["8", "⁸"], |
69 | | - ["9", "⁹"], |
70 | | - ["a", "ᵃ"], |
71 | | - ["b", "ᵇ"], |
72 | | - ["c", "ᶜ"], |
73 | | - ["d", "ᵈ"], |
74 | | - ["e", "ᵉ"], |
75 | | - ["f", "ᶠ"], |
76 | | - ["g", "ᵍ"], |
77 | | - ["h", "ʰ"], |
78 | | - ["i", "ⁱ"], |
79 | | - ["j", "ʲ"], |
80 | | - ["k", "ᵏ"], |
81 | | - ["l", "ˡ"], |
82 | | - ["m", "ᵐ"], |
83 | | - ["n", "ⁿ"], |
84 | | - ["o", "ᵒ"], |
85 | | - ["p", "ᵖ"], |
86 | | - ["r", "ʳ"], |
87 | | - ["s", "ˢ"], |
88 | | - ["t", "ᵗ"], |
89 | | - ["u", "ᵘ"], |
90 | | - ["v", "ᵛ"], |
91 | | - ["w", "ʷ"], |
92 | | - ["x", "ˣ"], |
93 | | - ["y", "ʸ"], |
94 | | - ["z", "ᶻ"] |
95 | | -]) |
96 | | - |
97 | | -const CHARACTER_TO_INDICE = new Map([ |
98 | | - ["-", "₋"], |
99 | | - ["+", "₊"], |
100 | | - ["=", "₌"], |
101 | | - ["(", "₍"], |
102 | | - [")", "₎"], |
103 | | - [" ", " "], |
104 | | - ["0", "₀"], |
105 | | - ["1", "₁"], |
106 | | - ["2", "₂"], |
107 | | - ["3", "₃"], |
108 | | - ["4", "₄"], |
109 | | - ["5", "₅"], |
110 | | - ["6", "₆"], |
111 | | - ["7", "₇"], |
112 | | - ["8", "₈"], |
113 | | - ["9", "₉"], |
114 | | - ["a", "ₐ"], |
115 | | - ["e", "ₑ"], |
116 | | - ["h", "ₕ"], |
117 | | - ["i", "ᵢ"], |
118 | | - ["j", "ⱼ"], |
119 | | - ["k", "ₖ"], |
120 | | - ["l", "ₗ"], |
121 | | - ["m", "ₘ"], |
122 | | - ["n", "ₙ"], |
123 | | - ["o", "ₒ"], |
124 | | - ["p", "ₚ"], |
125 | | - ["r", "ᵣ"], |
126 | | - ["s", "ₛ"], |
127 | | - ["t", "ₜ"], |
128 | | - ["u", "ᵤ"], |
129 | | - ["v", "ᵥ"], |
130 | | - ["x", "ₓ"] |
131 | | -]) |
132 | | - |
133 | | -const EXPONENTS = [ |
134 | | - "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹" |
135 | | -] |
136 | | - |
137 | | -const EXPONENTS_REG = new RegExp("([" + EXPONENTS.join("") + "]+)", "g") |
138 | | - |
139 | | -/** |
140 | | - * Put a text in sup position |
141 | | - * @param {string} text |
142 | | - * @return {string} |
143 | | - */ |
144 | | -export function textsup(text) { |
145 | | - let ret = "" |
146 | | - text = text.toString() |
147 | | - for(let letter of text) |
148 | | - ret += CHARACTER_TO_POWER.has(letter) ? CHARACTER_TO_POWER.get(letter) : letter |
149 | | - return ret |
150 | | -} |
151 | | - |
152 | | -/** |
153 | | - * Put a text in sub position |
154 | | - * @param {string} text |
155 | | - * @return {string} |
156 | | - */ |
157 | | -export function textsub(text) { |
158 | | - let ret = "" |
159 | | - text = text.toString() |
160 | | - for(let letter of text) |
161 | | - ret += CHARACTER_TO_INDICE.has(letter) ? CHARACTER_TO_INDICE.get(letter) : letter |
162 | | - return ret |
163 | | -} |
| 19 | +import { textsub, textsup } from "./subsup.mjs" |
164 | 20 |
|
165 | 21 | /** |
166 | 22 | * Simplifies (mathematically) a mathematical expression. |
@@ -400,35 +256,3 @@ export function parseName(str, removeUnallowed = true) { |
400 | 256 |
|
401 | 257 | return str |
402 | 258 | } |
403 | | - |
404 | | -/** |
405 | | - * Creates a randomized color string. |
406 | | - * @returns {string} |
407 | | - */ |
408 | | -export function getRandomColor() { |
409 | | - let clrs = "0123456789ABCDEF" |
410 | | - let color = "#" |
411 | | - for(let i = 0; i < 6; i++) { |
412 | | - color += clrs[Math.floor(Math.random() * (16 - 5 * (i % 2 === 0)))] |
413 | | - } |
414 | | - return color |
415 | | -} |
416 | | - |
417 | | -/** |
418 | | - * Escapes text to html entities. |
419 | | - * @param {string} str |
420 | | - * @returns {string} |
421 | | - */ |
422 | | -export function escapeHTML(str) { |
423 | | - return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") |
424 | | -} |
425 | | - |
426 | | - |
427 | | -/** |
428 | | - * Parses exponents and replaces them with expression values |
429 | | - * @param {string} expression - The expression to replace in. |
430 | | - * @return {string} The parsed expression |
431 | | - */ |
432 | | -export function exponentsToExpression(expression) { |
433 | | - return expression.replace(EXPONENTS_REG, (m, exp) => "^" + exp.split("").map((x) => EXPONENTS.indexOf(x)).join("")) |
434 | | -} |
0 commit comments