Skip to content

Commit 28c8e96

Browse files
BendingBendersindresorhus
authored andcommitted
Add TypeScript definition (#2)
1 parent 9a3e16a commit 28c8e96

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

index.d.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
declare namespace filterConsole {
2+
type Console = Record<
3+
'log' | 'debug' | 'info' | 'warn' | 'error',
4+
(message?: unknown, ...optionalParams: unknown[]) => void
5+
>;
6+
7+
interface Options {
8+
/**
9+
Console methods to filter.
10+
11+
@default ['log', 'debug', 'info', 'warn', 'error']
12+
*/
13+
readonly methods?: ReadonlyArray<
14+
'log' | 'debug' | 'info' | 'warn' | 'error'
15+
>;
16+
17+
/**
18+
Use a custom `console` object. Can be useful for testing or mocking.
19+
20+
@default console
21+
*/
22+
readonly console?: Console;
23+
}
24+
}
25+
26+
/**
27+
Filter out unwanted `console.log()` output.
28+
29+
Can be useful when you don't control the output, for example, filtering out PropType warnings from a third-party React component.
30+
31+
@param excludePatterns - Console output that matches any of the given patterns are filtered from being logged.
32+
33+
Filter types:
34+
- `string`: Checks if the string pattern is included in the console output.
35+
- `RegExp`: Checks if the RegExp pattern matches the console output.
36+
- `Function`: Receives the console output as a string and is expected to return a truthy/falsy value of whether to exclude it.
37+
@returns A function, which when called, disables the filter.
38+
39+
@example
40+
```
41+
import filterConsole = require('filter-console');
42+
43+
const disableFilter = filterConsole(['🐼']);
44+
45+
const log = () => {
46+
console.log('');
47+
console.log('🦄');
48+
console.log('🐼');
49+
console.log('🐶');
50+
};
51+
52+
log();
53+
54+
disableFilter();
55+
56+
log();
57+
58+
// $ node example.js
59+
//
60+
// 🦄
61+
// 🐶
62+
//
63+
// 🦄
64+
// 🐼
65+
// 🐶
66+
```
67+
*/
68+
declare function filterConsole(
69+
excludePatterns: Array<string | RegExp | ((output: string) => boolean)>,
70+
options?: filterConsole.Options
71+
): () => void;
72+
73+
export = filterConsole;

index.test-d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference types="node"/>
2+
import {expectType} from 'tsd';
3+
import filterConsole = require('.');
4+
5+
const disableFilter = filterConsole([
6+
'🐼',
7+
/foo/,
8+
output => {
9+
expectType<string>(output);
10+
return true;
11+
}
12+
]);
13+
filterConsole(['🐼'], {methods: ['log']});
14+
filterConsole(['🐼'], {console: console});
15+
16+
expectType<() => void>(disableFilter);
17+
disableFilter();

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"filter",
@@ -37,8 +38,10 @@
3738
"proptypes"
3839
],
3940
"devDependencies": {
40-
"ava": "^0.25.0",
41-
"sinon": "^6.3.5",
42-
"xo": "^0.23.0"
41+
"@types/node": "^11.13.4",
42+
"ava": "^1.4.1",
43+
"sinon": "^7.3.1",
44+
"tsd": "^0.7.2",
45+
"xo": "^0.24.0"
4346
}
4447
}

0 commit comments

Comments
 (0)