-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
diff.ts
96 lines (83 loc) · 2.48 KB
/
diff.ts
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
import * as diff from 'diff'
export interface DiffOptions {
outputTruncateLength?: number
outputDiffLines?: number
showLegend?: boolean
}
function formatLine(line: string, maxWidth: number) {
return line.slice(0, maxWidth) + (line.length > maxWidth ? '…' : '')
}
export function unifiedDiff(actual: string, expected: string, options: DiffOptions = {}) {
if (actual === expected)
return ''
const { outputTruncateLength = 80, outputDiffLines, showLegend = true } = options
const indent = ' '
const diffLimit = outputDiffLines || 15
const counts = {
'+': 0,
'-': 0,
}
let previousState: '-' | '+' | null = null
let previousCount = 0
function preprocess(line: string) {
if (!line || line.match(/\\ No newline/))
return
const char = line[0] as '+' | '-'
if ('-+'.includes(char)) {
if (previousState !== char) {
previousState = char
previousCount = 0
}
previousCount++
counts[char]++
if (previousCount === diffLimit)
return `${char} ...`
else if (previousCount > diffLimit)
return
}
return line
}
const msg = diff.createPatch('string', expected, actual)
const lines = msg.split('\n').slice(5).map(preprocess).filter(Boolean) as string[]
const isCompact = counts['+'] === 1 && counts['-'] === 1 && lines.length === 2
let formatted = lines.map((line: string) => {
line = line.replace(/\\"/g, '"')
if (line[0] === '-') {
line = formatLine(line.slice(1), outputTruncateLength)
if (isCompact)
return line
return `- ${formatLine(line, outputTruncateLength)}`
}
if (line[0] === '+') {
line = formatLine(line.slice(1), outputTruncateLength)
if (isCompact)
return line
return `+ ${formatLine(line, outputTruncateLength)}`
}
if (line.match(/@@/))
return '--'
return ` ${line}`
})
if (showLegend) {
// Compact mode
if (isCompact) {
formatted = [
`- Expected ${formatted[0]}`,
`+ Received ${formatted[1]}`,
]
}
else {
if (formatted[0].includes('"'))
formatted[0] = formatted[0].replace('"', '')
const last = formatted.length - 1
if (formatted[last].endsWith('"'))
formatted[last] = formatted[last].slice(0, formatted[last].length - 1)
formatted.unshift(
`- Expected - ${counts['-']}`,
`+ Received + ${counts['+']}`,
'',
)
}
}
return formatted.map(i => indent + i).join('\n')
}