Use text-diff

This commit is contained in:
Dragon Fire
2021-02-09 16:57:22 -05:00
parent 4f3bd6464f
commit 47c234ae54
3 changed files with 11 additions and 16 deletions
+10 -2
View File
@@ -1,8 +1,9 @@
const Command = require('../../structures/Command');
const { createCanvas, registerFont } = require('canvas');
const { stripIndents } = require('common-tags');
const Diff = require('text-diff');
const path = require('path');
const { textDiff, fetchHSUserDisplay } = require('../../util/Util');
const { fetchHSUserDisplay } = require('../../util/Util');
const words = require('../../assets/json/word-list');
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Regular.ttf'), { family: 'Noto' });
@@ -39,9 +40,16 @@ module.exports = class TypingTestCommand extends Command {
}
if (!msgs.size) return msg.reply('Sorry! You lose!');
if (msgs.first().content.toLowerCase() !== sentence) {
const diff = new Diff();
const textDiff = diff.main(msgs.first().content.toLowerCase(), sentence);
const formatted = diff.cleanupSemantic(textDiff).map(change => {
if (change[0] === 1) return `**${change[1]}**`;
if (change[0] === 0) return change[1];
return '';
}).join('');
return msg.reply(stripIndents`
Sorry! You made a typo, so you lose!
${textDiff(msgs.first().content.toLowerCase(), sentence)}
${formatted}
`);
}
const wpm = (sentence.length / 5) / ((newScore / 1000) / 60);
+1
View File
@@ -70,6 +70,7 @@
"sherlockjs": "^1.4.0",
"stackblur-canvas": "^2.4.0",
"tesseract.js": "^2.1.4",
"text-diff": "^1.0.1",
"tictactoe-minimax-ai": "^1.2.1",
"valid-url": "^1.0.9",
"winston": "^3.3.3"
-14
View File
@@ -181,20 +181,6 @@ module.exports = class Util {
return ((r << 16) | (g << 8) | b).toString(16);
}
static textDiff(oldText, newText) {
let changed = [];
return oldText.split('').map((char, i) => {
if (char === newText.charAt(i)) {
const chars = changed.length ? `**${changed.join('')}**${char}` : char;
if (changed.length) changed = [];
const extraText = newText.slice(oldText.length);
return i === oldText.length - 1 ? `${chars}${extraText.length ? `**${extraText}**` : ''}` : chars;
}
changed.push(newText.charAt(i));
return i === oldText.length - 1 ? `**${changed.join('')}${newText.slice(oldText.length)}**` : '';
}).join('');
}
static magikToBuffer(magik) {
return new Promise((res, rej) => {
magik.toBuffer((err, buffer) => {