/
diff.ts
107 lines (93 loc) · 2.82 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
97
98
99
100
101
102
103
104
105
106
107
import c from 'picocolors'
import * as diff from 'diff'
import cliTruncate from 'cli-truncate'
export function formatLine(line: string, outputTruncateLength?: number) {
return cliTruncate(line, (outputTruncateLength ?? (process.stdout?.columns || 80)) - 4)
}
export interface DiffOptions {
outputTruncateLength?: number
outputDiffLines?: number
showLegend?: boolean
}
/**
* Returns unified diff between two strings with coloured ANSI output.
*
* @private
* @param {String} actual
* @param {String} expected
* @return {string} The diff.
*/
export function unifiedDiff(actual: string, expected: string, options: DiffOptions = {}) {
if (actual === expected)
return ''
const { outputTruncateLength, 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 c.dim(`${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 c.green(line)
return c.green(`- ${formatLine(line, outputTruncateLength)}`)
}
if (line[0] === '+') {
line = formatLine(line.slice(1), outputTruncateLength)
if (isCompact)
return c.red(line)
return c.red(`+ ${formatLine(line, outputTruncateLength)}`)
}
if (line.match(/@@/))
return '--'
return ` ${line}`
})
if (showLegend) {
// Compact mode
if (isCompact) {
formatted = [
`${c.green('- Expected')} ${formatted[0]}`,
`${c.red('+ 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(
c.green(`- Expected - ${counts['-']}`),
c.red(`+ Received + ${counts['+']}`),
'',
)
}
}
return formatted.map(i => indent + i).join('\n')
}