-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickmath.js
executable file
·357 lines (302 loc) · 11 KB
/
quickmath.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env node
/*
QuickMathsJS-WebCalc An intuitive web-based calculator using math.js. Features inline results and supports free-form calculations.
Copyright (C) 2023 Brian Khuu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const mathjs = require('mathjs');
const webcalc = require('./calculator.js')(mathjs);
const open = require('opn');
const express = require('express');
const zlib = require('zlib');
const path = require('path');
const { version } = require('./package.json');
require('colors');
const Diff = require('diff');
function calculateFileContent(content, useSections = false) {
if (useSections) {
return webcalc.calculateWithMathSections(content);
} else {
return webcalc.calculate(content);
}
}
function processFile(filePath, useSections, callback) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
const updatedContent = calculateFileContent(data, useSections);
callback(filePath, updatedContent);
});
}
/******************************************************************************
* TESTING
*/
function processTests(tests, testCaseFilePath) {
let failures = 0;
tests.forEach((test, index) => {
try {
const result = calculateFileContent(test.given);
const passfail = (result === test.expect) ? "PASS"['green'] : "FAIL"['red']
console.log(`Test ${index + 1} (${test.name}): ${passfail}`);
if (result !== test.expect) {
const diff = Diff.diffChars(test.expect, result);
let diff_list = '';
diff.forEach((part) => {
diff_list += part.added ? part.value.underline.green:
part.removed ? part.value.strikethrough.red:
part.value.grey;
});
console.log(`Given:\n${test.given}`);
console.log(`Diff:\n${diff_list}`);
failures++;
}
} catch (error) {
// Extract the undefined symbol name from the error message
console.log(`\nTest ${index + 1} (${test.name}): ${"FAIL - ERROR"['red']}`);
console.log("ERR:", error.message);
console.log(error.stack.toString());
console.log('\n');
failures++;
}
});
return failures;
}
// This has before and after cases (good for checking replacement test examples)
function runFullTestCase(testCaseFilePath) {
const fullPath = path.join(__dirname, testCaseFilePath);
const content = fs.readFileSync(fullPath, 'utf-8');
const regex = /^#+ (.*?)\n(?:[^#]*?)\*\*(?:For Example|Input|Given):\*\*\n```(?:.*?)\n([\s\S]+?)\n```\n\n\*\*(?:Result|Output|Expect):\*\*\n```(?:.*?)\n([\s\S]+?)\n```$/gm;
const tests = [];
console.log(`FULL TEST CASE FILE: ${testCaseFilePath}`);
let match;
while ((match = regex.exec(content)) !== null) {
tests.push({
name: match[1],
given: match[2],
expect: match[3]
});
tests.push({
name: match[1] + "- STATIC TESTING",
given: match[3],
expect: match[3]
});
}
return processTests(tests, testCaseFilePath);
}
// This has simple cases (e.g. results already present examples)
function runMathBlockTestCase(testCaseFilePath) {
const fullPath = path.join(__dirname, testCaseFilePath);
const content = fs.readFileSync(fullPath, 'utf-8');
const regex = /^#+ (.*?)\n(?:[^#]*?)^```calc(?:.*?)\n([\s\S]+?)\n^```$/gm;
const tests = [];
console.log(`BLOCK TEST CASE FILE: ${testCaseFilePath}`);
let match;
while ((match = regex.exec(content)) !== null) {
tests.push({
name: match[1],
given: match[2],
expect: match[2]
});
}
return processTests(tests, testCaseFilePath);
}
function runDelimTests() {
// Test for the math delimiter feature
let failures = 0;
/**
* Tagged template function to remove common indentation from template literals.
* This allows for cleaner code formatting without affecting the string's actual content.
*
* @param {Array<string>} strings - The static parts of the template literal.
* @param {...any} values - The interpolated values within the template literal.
* @return {string} - The processed string with common indentation removed.
*/
function stripIndent(strings, ...values) {
// Combine the strings and values back into the full string
const fullString = strings.reduce((acc, str, i) => `${acc}${str}${values[i] || ''}`, '');
// Find the common indentation at the beginning of each line
const match = fullString.match(/^[ \t]*(?=\S)/gm);
if (!match) return fullString;
// Calculate the amount of indentation to remove
const indent = Math.min(...match.map(el => el.length));
// Create a regular expression to remove the common indentation
const regexp = new RegExp(`^[ \\t]{${indent}}`, 'gm');
// Return the string with the common indentation removed
return indent > 0 ? fullString.replace(regexp, '') : fullString;
}
const mockContent = stripIndent`
This is a sample content.
\`\`\`calc
1 + 1 = ?
\`\`\`
\`\`\`calc
1 + 1 = ?
1 + 1 = ?
\`\`\`
\`\`\`calc {id = "testid"}
1 + 1 = ?
1 + 1 = ?
\`\`\`
\`\`\`calc
1 + 1 = ?
1 + 1 = ?
\`\`\`
This should remain unchanged.
\`\`\`calc
1 + 1 =
\`\`\`
This should also remain unchanged.`;
const expectedContent = stripIndent`
This is a sample content.
\`\`\`calc
1 + 1 = 2
\`\`\`
\`\`\`calc
1 + 1 = 2
1 + 1 = 2
\`\`\`
\`\`\`calc {id = "testid"}
1 + 1 = 2
1 + 1 = 2
\`\`\`
\`\`\`calc
1 + 1 = 2
1 + 1 = 2
\`\`\`
This should remain unchanged.
\`\`\`calc
1 + 1 =
\`\`\`
This should also remain unchanged.`;
const mathDelimiterResult = calculateFileContent(mockContent, true);
if (mathDelimiterResult.trim() === expectedContent.trim()) {
console.log(`Math delimiter test (Using LF): ${"PASS"['green']}`);
} else {
console.log(`Math delimiter test (Using LF): ${"FAIL"['red']}`);
console.log('Expected:\n', expectedContent);
console.log('Got:\n', mathDelimiterResult);
failures++;
}
// Windows Style Newline Handling
const mockContent_crlf = expectedContent.replace(/\n/g, "\r\n");
const expectedContent_crlf = expectedContent.replace(/\n/g, "\r\n");
const mathDelimiterResult_crlf = calculateFileContent(mockContent_crlf, true);
if (mathDelimiterResult_crlf.trim() === expectedContent_crlf.trim()) {
console.log(`Math delimiter test (Using CRLF): ${"PASS"['green']}`);
} else {
console.log(`Math delimiter test (Using CRLF): ${"FAIL"['red']}`);
console.log('Expected:\n', expectedContent_crlf);
console.log('Got:\n', mathDelimiterResult_crlf);
failures++;
}
return failures;
}
function runTests() {
let failures = runDelimTests();
failures += failures > 0 ? 0 : runMathBlockTestCase('readme.md');
failures += failures > 0 ? 0 : runFullTestCase('readme.md');
failures += failures > 0 ? 0 : runMathBlockTestCase('userexamples.md');
failures += failures > 0 ? 0 : runFullTestCase('userexamples.md');
failures += failures > 0 ? 0 : runFullTestCase('testcases_basics.md');
failures += failures > 0 ? 0 : runFullTestCase('testcases_advance.md');
failures += failures > 0 ? 0 : runFullTestCase('testcases_constants.md');
failures += failures > 0 ? 0 : runFullTestCase('testcases_errors_and_edgecases.md');
if (failures > 0) {
console.error(`Failed ${failures} test(s). Exiting.`);
process.exit(1);
} else {
console.error(`All test passed. Exiting.`);
}
}
/******************************************************************************
* CLI
*/
// Initialise webcalc internals
webcalc.initialise();
// Check for command line flags
const useSectionsFlag = '--sections';
const testFlag = '--test';
const helpFlag = '--help';
const webFlag = '--web';
const versionFlag = '--version';
function displayHelp() {
console.log(`
Usage: quickmath [OPTIONS] [FILE]
Options:
--help Show this help message and exit.
--version Display the current version of QuickMathsJS.
--sections Evaluate only sections surrounded by the \`\`\`calc delimiter.
--web Launch the web interface.
--test Run predefined test cases.
Examples:
quickmath --help Show this help message and exit.
quickmath --version Display the current version.
quickmath path/to/your/file.txt Evaluate entire file.
quickmath --sections path/to/your/file.txt Evaluate only math sections in file.
quickmath --web path/to/your/file.txt Launch the web interface and load entire file.
quickmath --test Run predefined test cases.
`);
}
function displayVersion() {
console.log(`QuickMathsJS version: ${version}`);
}
if (process.argv.includes(helpFlag)) {
displayHelp();
} else if (process.argv.includes(versionFlag)) {
displayVersion();
} else if (process.argv.includes(testFlag)) {
runTests();
} else if (process.argv.includes(webFlag)) {
const app = express();
const port = 3000;
// Serve static files from the root directory
app.use(express.static(__dirname));
const launchURL = async () => {
let url = `http://localhost:${port}`;
if (process.argv.length > 3) {
const filePath = process.argv[3];
try {
const fileContent = await fs.promises.readFile(filePath, 'utf8');
const compressed = zlib.gzipSync(fileContent).toString('base64');
url += `#${compressed}`;
} catch (err) {
console.error("Error reading the file:", err);
process.exit(1);
}
}
open(url);
};
app.listen(port, () => {
console.log(`QuickMathsJS-WebCalc is running at http://localhost:${port}`);
launchURL();
});
} else {
if (process.argv.length < 3) {
console.error("Please provide a path to the input file or use --help for more options.");
displayHelp();
process.exit(1);
}
const filePath = process.argv[2];
const useSections = process.argv.includes(useSectionsFlag);
processFile(filePath, useSections, (path, updatedContent) => {
fs.writeFile(path, updatedContent, (err) => {
if (err) {
console.error("Error writing to the file:", err);
return;
}
console.log("File updated successfully!");
});
});
}