diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..7276fc7 --- /dev/null +++ b/index.d.ts @@ -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 boolean)>, + options?: filterConsole.Options +): () => void; + +export = filterConsole; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..86140d3 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,17 @@ +/// +import {expectType} from 'tsd'; +import filterConsole = require('.'); + +const disableFilter = filterConsole([ + '🐼', + /foo/, + output => { + expectType(output); + return true; + } +]); +filterConsole(['🐼'], {methods: ['log']}); +filterConsole(['🐼'], {console: console}); + +expectType<() => void>(disableFilter); +disableFilter(); diff --git a/package.json b/package.json index e153cf7..7aa0dbd 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "filter", @@ -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" } }