-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a3e16a
commit 28c8e96
Showing
3 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters