-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
60 lines (51 loc) · 1.17 KB
/
index.js
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
import process from 'node:process';
import {format} from 'node:util';
export default function filterConsole(excludePatterns, options) {
options = {
console,
methods: [
'log',
'debug',
'info',
'warn',
'error',
],
...options,
};
const {console: consoleObject, methods} = options;
const originalMethods = methods.map(method => consoleObject[method]);
const check = string => {
for (const pattern of excludePatterns) {
if (typeof pattern === 'string') {
if (string.includes(pattern)) {
return true;
}
} else if (typeof pattern === 'function') {
if (pattern(string)) {
return true;
}
} else if (pattern.test(string)) {
return true;
}
}
return false;
};
for (const method of methods) {
const originalMethod = consoleObject[method];
consoleObject[method] = (...args) => {
if (check(format(...args))) {
return;
}
originalMethod(...args);
};
// Exposed for testing
if (process.env.NODE_ENV === 'test') {
consoleObject[method].original = originalMethod;
}
}
return () => {
for (const [index, method] of methods.entries()) {
consoleObject[method] = originalMethods[index];
}
};
}