Skip to content

Commit

Permalink
Add TypeScript definition (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 15, 2019
1 parent 9a3e16a commit 28c8e96
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
73 changes: 73 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
declare namespace filterConsole {
type Console = Record<
'log' | 'debug' | 'info' | 'warn' | 'error',
(message?: unknown, ...optionalParams: unknown[]) => void
>;

interface Options {
/**
Console methods to filter.
@default ['log', 'debug', 'info', 'warn', 'error']
*/
readonly methods?: ReadonlyArray<
'log' | 'debug' | 'info' | 'warn' | 'error'
>;

/**
Use a custom `console` object. Can be useful for testing or mocking.
@default console
*/
readonly console?: Console;
}
}

/**
Filter out unwanted `console.log()` output.
Can be useful when you don't control the output, for example, filtering out PropType warnings from a third-party React component.
@param excludePatterns - Console output that matches any of the given patterns are filtered from being logged.
Filter types:
- `string`: Checks if the string pattern is included in the console output.
- `RegExp`: Checks if the RegExp pattern matches the console output.
- `Function`: Receives the console output as a string and is expected to return a truthy/falsy value of whether to exclude it.
@returns A function, which when called, disables the filter.
@example
```
import filterConsole = require('filter-console');
const disableFilter = filterConsole(['🐼']);
const log = () => {
console.log('');
console.log('🦄');
console.log('🐼');
console.log('🐶');
};
log();
disableFilter();
log();
// $ node example.js
//
// 🦄
// 🐶
//
// 🦄
// 🐼
// 🐶
```
*/
declare function filterConsole(
excludePatterns: Array<string | RegExp | ((output: string) => boolean)>,
options?: filterConsole.Options
): () => void;

export = filterConsole;
17 changes: 17 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference types="node"/>
import {expectType} from 'tsd';
import filterConsole = require('.');

const disableFilter = filterConsole([
'🐼',
/foo/,
output => {
expectType<string>(output);
return true;
}
]);
filterConsole(['🐼'], {methods: ['log']});
filterConsole(['🐼'], {console: console});

expectType<() => void>(disableFilter);
disableFilter();
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"filter",
Expand All @@ -37,8 +38,10 @@
"proptypes"
],
"devDependencies": {
"ava": "^0.25.0",
"sinon": "^6.3.5",
"xo": "^0.23.0"
"@types/node": "^11.13.4",
"ava": "^1.4.1",
"sinon": "^7.3.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 28c8e96

Please sign in to comment.