|
| 1 | +export type DiffItem = { |
| 2 | + // the value can be a character, a word, a string |
| 3 | + value: string; |
| 4 | + type: "correct" | "extra" | "missing" | "wrong" | "untouched" | "spacer"; |
| 5 | +}; |
| 6 | + |
| 7 | +export type DiffResult = { |
| 8 | + diff: DiffItem[]; |
| 9 | + end: boolean; |
| 10 | +}; |
| 11 | + |
| 12 | +export const diff = (originalText: string, typedText: string): DiffResult => { |
| 13 | + const correctWords = originalText.split(" "); |
| 14 | + const typedWords = typedText.split(" "); |
| 15 | + const currentWordIndex = typedWords.length - 1; |
| 16 | + const diff: DiffItem[] = []; |
| 17 | + |
| 18 | + // compare words BEFORE current word |
| 19 | + if (currentWordIndex > 0) { |
| 20 | + for (let i = 0; i < currentWordIndex; i++) { |
| 21 | + if (i >= correctWords.length) { |
| 22 | + break; |
| 23 | + } |
| 24 | + const correctWord = correctWords[i]; |
| 25 | + const typedWord = typedWords[i]; |
| 26 | + if (!correctWord || !typedWord) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + const wordDiff = diffWord(correctWord, typedWord); |
| 30 | + diff.push(...wordDiff); |
| 31 | + diff.push({ value: " ", type: "spacer" }); |
| 32 | + } |
| 33 | + } |
| 34 | + if (currentWordIndex >= correctWords.length) { |
| 35 | + return { diff, end: true }; |
| 36 | + } |
| 37 | + // compare current word |
| 38 | + const correctWord = correctWords[currentWordIndex]; |
| 39 | + const typedWord = typedWords[currentWordIndex]; |
| 40 | + if (!correctWord || !typedWord) { |
| 41 | + return { diff, end: false }; |
| 42 | + } |
| 43 | + const currentWordDiff = diffCurrentWord(correctWord, typedWord); |
| 44 | + diff.push(...currentWordDiff); |
| 45 | + |
| 46 | + // words AFTER current word are untouched |
| 47 | + if (currentWordIndex + 1 < correctWords.length) { |
| 48 | + for (let i = currentWordIndex + 1; i < correctWords.length; i++) { |
| 49 | + const correctWord = correctWords[i]; |
| 50 | + if (!correctWord) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + diff.push({ value: correctWord, type: "untouched" }); |
| 54 | + diff.push({ value: " ", type: "spacer" }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return { diff, end: false }; |
| 59 | +}; |
| 60 | + |
| 61 | +const diffWord = (originalWord: string, typedWord: string) => { |
| 62 | + const diff: DiffItem[] = []; |
| 63 | + |
| 64 | + // compare letters |
| 65 | + const correctLength = originalWord.length; |
| 66 | + const typedLength = typedWord.length; |
| 67 | + |
| 68 | + // if typed word is shorter than correct word: typed: "hel", correct: "hello" |
| 69 | + if (typedLength < correctLength) { |
| 70 | + // check until typedLength |
| 71 | + for (let i = 0; i < typedLength; i++) { |
| 72 | + let originalChar = originalWord[i]; |
| 73 | + let typedChar = typedWord[i]; |
| 74 | + if (!originalChar || !typedChar) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + diff.push(diffChar(originalChar, typedChar)); |
| 78 | + } |
| 79 | + // the rest are missing |
| 80 | + for (let i = typedLength; i < correctLength; i++) { |
| 81 | + let originalChar = originalWord[i]; |
| 82 | + if (!originalChar) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + diff.push({ value: originalChar, type: "missing" }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // if typed word is longer than correct word: typed: "helloo", correct: "hello" |
| 90 | + if (typedLength > correctLength) { |
| 91 | + // check until correctLength |
| 92 | + for (let i = 0; i < correctLength; i++) { |
| 93 | + let originalChar = originalWord[i]; |
| 94 | + let typedChar = typedWord[i]; |
| 95 | + if (!originalChar || !typedChar) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + diff.push(diffChar(originalChar, typedChar)); |
| 99 | + } |
| 100 | + // the rest are extra |
| 101 | + for (let i = correctLength; i < typedLength; i++) { |
| 102 | + let typedChar = typedWord[i]; |
| 103 | + if (!typedChar) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + diff.push({ value: typedChar, type: "extra" }); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // if typed word is equal to correct word: typed: "hello", correct: "alloh" |
| 111 | + if (typedLength === correctLength) { |
| 112 | + for (let i = 0; i < correctLength; i++) { |
| 113 | + let originalChar = originalWord[i]; |
| 114 | + let typedChar = typedWord[i]; |
| 115 | + if (!originalChar || !typedChar) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + diff.push(diffChar(originalChar, typedChar)); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return diff; |
| 123 | +}; |
| 124 | + |
| 125 | +const diffChar = (originalChar: string, typedChar: string): DiffItem => { |
| 126 | + if (originalChar === typedChar) { |
| 127 | + return { value: originalChar, type: "correct" }; |
| 128 | + } |
| 129 | + return { value: typedChar, type: "wrong" }; |
| 130 | +}; |
| 131 | + |
| 132 | +const diffCurrentWord = (correctWord: string, typedWord: string) => { |
| 133 | + const diff: DiffItem[] = []; |
| 134 | + const correctLength = correctWord.length; |
| 135 | + const typedLength = typedWord.length; |
| 136 | + |
| 137 | + // if typed word is shorter than correct word: typed: "hel", correct: "hello" |
| 138 | + if (typedLength < correctLength) { |
| 139 | + // check until typedLength |
| 140 | + for (let i = 0; i < typedLength; i++) { |
| 141 | + let originalChar = correctWord[i]; |
| 142 | + let typedChar = typedWord[i]; |
| 143 | + if (!originalChar || !typedChar) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + diff.push(diffChar(originalChar, typedChar)); |
| 147 | + } |
| 148 | + // the rest are untouched |
| 149 | + for (let i = typedLength; i < correctLength; i++) { |
| 150 | + let originalChar = correctWord[i]; |
| 151 | + if (!originalChar) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + diff.push({ value: originalChar, type: "untouched" }); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // if typed word is longer than correct word: typed: "helloo", correct: "hello" |
| 159 | + if (typedLength > correctLength) { |
| 160 | + // check until correctLength |
| 161 | + for (let i = 0; i < correctLength; i++) { |
| 162 | + let originalChar = correctWord[i]; |
| 163 | + let typedChar = typedWord[i]; |
| 164 | + if (!originalChar || !typedChar) { |
| 165 | + continue; |
| 166 | + } |
| 167 | + diff.push(diffChar(originalChar, typedChar)); |
| 168 | + } |
| 169 | + // the rest are extra |
| 170 | + for (let i = correctLength; i < typedLength; i++) { |
| 171 | + let typedChar = typedWord[i]; |
| 172 | + if (!typedChar) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + diff.push({ value: typedChar, type: "extra" }); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // if typed word is equal to correct word: typed: "hello", correct: "alloh" |
| 180 | + if (typedLength === correctLength) { |
| 181 | + for (let i = 0; i < correctLength; i++) { |
| 182 | + let originalChar = correctWord[i]; |
| 183 | + let typedChar = typedWord[i]; |
| 184 | + if (!originalChar || !typedChar) { |
| 185 | + continue; |
| 186 | + } |
| 187 | + diff.push(diffChar(originalChar, typedChar)); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + diff.push({ value: " ", type: "spacer" }); |
| 192 | + |
| 193 | + return diff; |
| 194 | +}; |
0 commit comments