* Extract character code data from diff * @param expected The string that was searched for * @param actual The string that was found * @returns Character code statistics
(expected: string, actual: string)
| 55 | * @returns Character code statistics |
| 56 | */ |
| 57 | function getCharacterCodeData(expected: string, actual: string): { |
| 58 | report: string; |
| 59 | uniqueCount: number; |
| 60 | diffLength: number; |
| 61 | } { |
| 62 | // Find common prefix and suffix |
| 63 | let prefixLength = 0; |
| 64 | const minLength = Math.min(expected.length, actual.length); |
| 65 | |
| 66 | // Determine common prefix length |
| 67 | while (prefixLength < minLength && |
| 68 | expected[prefixLength] === actual[prefixLength]) { |
| 69 | prefixLength++; |
| 70 | } |
| 71 | |
| 72 | // Determine common suffix length |
| 73 | let suffixLength = 0; |
| 74 | while (suffixLength < minLength - prefixLength && |
| 75 | expected[expected.length - 1 - suffixLength] === actual[actual.length - 1 - suffixLength]) { |
| 76 | suffixLength++; |
| 77 | } |
| 78 | |
| 79 | // Extract the different parts |
| 80 | const expectedDiff = expected.substring(prefixLength, expected.length - suffixLength); |
| 81 | const actualDiff = actual.substring(prefixLength, actual.length - suffixLength); |
| 82 | |
| 83 | // Count unique character codes in the diff |
| 84 | const characterCodes = new Map<number, number>(); |
| 85 | const fullDiff = expectedDiff + actualDiff; |
| 86 | |
| 87 | for (let i = 0; i < fullDiff.length; i++) { |
| 88 | const charCode = fullDiff.charCodeAt(i); |
| 89 | characterCodes.set(charCode, (characterCodes.get(charCode) || 0) + 1); |
| 90 | } |
| 91 | |
| 92 | // Create character codes string report |
| 93 | const charCodeReport: string[] = []; |
| 94 | characterCodes.forEach((count, code) => { |
| 95 | // Include character representation for better readability |
| 96 | const char = String.fromCharCode(code); |
| 97 | // Make special characters more readable |
| 98 | const charDisplay = code < 32 || code > 126 ? `\\x${code.toString(16).padStart(2, '0')}` : char; |
| 99 | charCodeReport.push(`${code}:${count}[${charDisplay}]`); |
| 100 | }); |
| 101 | |
| 102 | // Sort by character code for consistency |
| 103 | charCodeReport.sort((a, b) => { |
| 104 | const codeA = parseInt(a.split(':')[0]); |
| 105 | const codeB = parseInt(b.split(':')[0]); |
| 106 | return codeA - codeB; |
| 107 | }); |
| 108 | |
| 109 | return { |
| 110 | report: charCodeReport.join(','), |
| 111 | uniqueCount: characterCodes.size, |
| 112 | diffLength: fullDiff.length |
| 113 | }; |
| 114 | } |
no test coverage detected