forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalignedAnsiStyleSerializer.ts
More file actions
51 lines (46 loc) · 1.38 KB
/
alignedAnsiStyleSerializer.ts
File metadata and controls
51 lines (46 loc) · 1.38 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import ansiRegex = require('ansi-regex');
import style = require('ansi-styles');
import type {NewPlugin} from 'pretty-format';
export const alignedAnsiStyleSerializer: NewPlugin = {
serialize(val: string): string {
// Return the string itself, not escaped nor enclosed in double quote marks.
return val.replace(ansiRegex(), match => {
switch (match) {
case style.inverse.open:
return '<i>';
case style.inverse.close:
return '</i>';
case style.bold.open:
return '<b>';
case style.dim.open:
return '<d>';
case style.green.open:
return '<g>';
case style.red.open:
return '<r>';
case style.yellow.open:
return '<y>';
case style.bgYellow.open:
return '<Y>';
case style.bold.close:
case style.dim.close:
case style.green.close:
case style.red.close:
case style.yellow.close:
case style.bgYellow.close:
return '</>';
default:
return match; // unexpected escape sequence
}
});
},
test(val: unknown): val is string {
return typeof val === 'string';
},
};