├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/filter-console 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | type ConsoleMethods = 'log' | 'debug' | 'info' | 'warn' | 'error'; 2 | 3 | export type Console = Record< 4 | ConsoleMethods, 5 | (message?: unknown, ...optionalParameters: unknown[]) => void 6 | >; 7 | 8 | export interface Options { 9 | /** 10 | Console methods to filter. 11 | 12 | @default ['log', 'debug', 'info', 'warn', 'error'] 13 | */ 14 | readonly methods?: readonly ConsoleMethods[]; 15 | 16 | /** 17 | Use a custom `console` object. Can be useful for testing or mocking. 18 | 19 | @default console 20 | */ 21 | readonly console?: Console; 22 | } 23 | 24 | /** 25 | Filter out unwanted `console.log()` output. 26 | 27 | Can be useful when you don't control the output, for example, filtering out PropType warnings from a third-party React component. 28 | 29 | @param excludePatterns - Console output that matches any of the given patterns are filtered from being logged. 30 | 31 | Filter types: 32 | - `string`: Checks if the string pattern is included in the console output. 33 | - `RegExp`: Checks if the RegExp pattern matches the console output. 34 | - `Function`: Receives the console output as a string and is expected to return a truthy/falsy value of whether to exclude it. 35 | @returns A function, which when called, disables the filter. 36 | 37 | @example 38 | ``` 39 | import filterConsole from 'filter-console'; 40 | 41 | const disableFilter = filterConsole(['🐼']); 42 | 43 | const log = () => { 44 | console.log(''); 45 | console.log('🦄'); 46 | console.log('🐼'); 47 | console.log('🐶'); 48 | }; 49 | 50 | log(); 51 | 52 | disableFilter(); 53 | 54 | log(); 55 | 56 | // $ node example.js 57 | // 58 | // 🦄 59 | // 🐶 60 | // 61 | // 🦄 62 | // 🐼 63 | // 🐶 64 | ``` 65 | */ 66 | export default function filterConsole( 67 | excludePatterns: Array boolean)>, 68 | options?: Options 69 | ): () => void; 70 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import {format} from 'node:util'; 3 | 4 | export default function filterConsole(excludePatterns, options) { 5 | options = { 6 | console, 7 | methods: [ 8 | 'log', 9 | 'debug', 10 | 'info', 11 | 'warn', 12 | 'error', 13 | ], 14 | ...options, 15 | }; 16 | 17 | const {console: consoleObject, methods} = options; 18 | const originalMethods = methods.map(method => consoleObject[method]); 19 | 20 | const check = string => { 21 | for (const pattern of excludePatterns) { 22 | if (typeof pattern === 'string') { 23 | if (string.includes(pattern)) { 24 | return true; 25 | } 26 | } else if (typeof pattern === 'function') { 27 | if (pattern(string)) { 28 | return true; 29 | } 30 | } else if (pattern.test(string)) { 31 | return true; 32 | } 33 | } 34 | 35 | return false; 36 | }; 37 | 38 | for (const method of methods) { 39 | const originalMethod = consoleObject[method]; 40 | 41 | consoleObject[method] = (...args) => { 42 | if (check(format(...args))) { 43 | return; 44 | } 45 | 46 | originalMethod(...args); 47 | }; 48 | 49 | // Exposed for testing 50 | if (process.env.NODE_ENV === 'test') { 51 | consoleObject[method].original = originalMethod; 52 | } 53 | } 54 | 55 | return () => { 56 | for (const [index, method] of methods.entries()) { 57 | consoleObject[method] = originalMethods[index]; 58 | } 59 | }; 60 | } 61 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import filterConsole from './index.js'; 3 | 4 | const disableFilter = filterConsole([ 5 | '🐼', 6 | /foo/, 7 | output => { 8 | expectType(output); 9 | return true; 10 | }, 11 | ]); 12 | filterConsole(['🐼'], {methods: ['log']}); 13 | filterConsole(['🐼'], {console}); 14 | 15 | expectType<() => void>(disableFilter); 16 | disableFilter(); 17 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "filter-console", 3 | "version": "1.0.0", 4 | "description": "Filter out unwanted `console.log()` output", 5 | "license": "MIT", 6 | "repository": "sindresorhus/filter-console", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "filter", 27 | "console", 28 | "console-log", 29 | "log", 30 | "logging", 31 | "exclude", 32 | "exclusion", 33 | "devtools", 34 | "pattern", 35 | "patterns", 36 | "test", 37 | "testing", 38 | "mock", 39 | "electron", 40 | "react", 41 | "proptypes" 42 | ], 43 | "devDependencies": { 44 | "@types/node": "^16.7.2", 45 | "ava": "^3.15.0", 46 | "sinon": "^11.1.2", 47 | "tsd": "^0.17.0", 48 | "xo": "^0.44.0" 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # filter-console 2 | 3 | > Filter out unwanted `console.log()` output 4 | 5 | Can be useful when you don't control the output, for example, filtering out PropType warnings from a third-party React component. 6 | 7 | ## Install 8 | 9 | ``` 10 | $ npm install filter-console 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import filterConsole from 'filter-console'; 17 | 18 | const disableFilter = filterConsole(['🐼']); 19 | 20 | const log = () => { 21 | console.log(''); 22 | console.log('🦄'); 23 | console.log('🐼'); 24 | console.log('🐶'); 25 | }; 26 | 27 | log(); 28 | 29 | disableFilter(); 30 | 31 | log(); 32 | ``` 33 | 34 | ``` 35 | $ node example.js 36 | 37 | 🦄 38 | 🐶 39 | 40 | 🦄 41 | 🐼 42 | 🐶 43 | ``` 44 | 45 | ## API 46 | 47 | ### filterConsole(excludePatterns, options?) 48 | 49 | Returns a function, which when called, disables the filter. 50 | 51 | #### excludePatterns 52 | 53 | Type: `Array` 54 | 55 | Console output that matches any of the given patterns are filtered from being logged. The patterns are matched against what would be logged and not the `console` method input arguments directly. Meaning an exclude pattern of `'foo bar'` will match `console.log('foo %s', 'bar')`. 56 | 57 | Filter types: 58 | - `string`: Checks if the string pattern is included in the console output. 59 | - `RegExp`: Checks if the RegExp pattern matches the console output. 60 | - `Function`: Receives the console output as a string and is expected to return a truthy/falsy value of whether to exclude it. 61 | 62 | #### options 63 | 64 | Type: `object` 65 | 66 | ##### methods 67 | 68 | Type: `string[]`\ 69 | Default: `['log', 'debug', 'info', 'warn', 'error']` 70 | 71 | Console methods to filter. 72 | 73 | ##### console 74 | 75 | Type: `object`\ 76 | Default: `console` 77 | 78 | Use a custom `console` object. Can be useful for testing or mocking. 79 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import sinon from 'sinon'; 3 | import filterConsole from './index.js'; 4 | 5 | const methods = [ 6 | 'log', 7 | 'debug', 8 | 'info', 9 | 'warn', 10 | 'error', 11 | ]; 12 | 13 | const getConsole = () => { 14 | const customConsole = {}; 15 | 16 | for (const method of methods) { 17 | customConsole[method] = sinon.fake(); 18 | } 19 | 20 | return customConsole; 21 | }; 22 | 23 | test('main', t => { 24 | const customConsole = getConsole(); 25 | 26 | const fixture1 = '-1-'; 27 | const fixture2 = '-2-'; 28 | const fixture3 = '-3-'; 29 | const fixture4 = 'Foo Bar'; 30 | const fixture5 = 'unicorn'; 31 | 32 | const disableFilter = filterConsole([ 33 | '2', 34 | /^foo/i, 35 | output => output.endsWith('corn'), 36 | ], {console: customConsole}); 37 | 38 | customConsole.log(fixture1); 39 | customConsole.log(fixture2); 40 | customConsole.log(fixture3); 41 | customConsole.log(fixture4); 42 | customConsole.log(fixture5); 43 | 44 | t.true(customConsole.log.original.calledWith(fixture1)); 45 | t.false(customConsole.log.original.calledWith(fixture2)); 46 | t.true(customConsole.log.original.calledWith(fixture3)); 47 | t.false(customConsole.log.original.calledWith(fixture4)); 48 | t.false(customConsole.log.original.calledWith(fixture5)); 49 | 50 | disableFilter(); 51 | customConsole.log(fixture2); 52 | t.true(customConsole.log.calledWith(fixture2)); 53 | }); 54 | 55 | test('`methods` option`', t => { 56 | const customConsole = getConsole(); 57 | 58 | const fixture1 = '1'; 59 | 60 | filterConsole([fixture1], { 61 | console: customConsole, 62 | methods: ['warn'], 63 | }); 64 | 65 | customConsole.log(fixture1); 66 | t.true(customConsole.log.calledWith(fixture1)); 67 | 68 | customConsole.warn(fixture1); 69 | t.false(customConsole.warn.original.calledWith(fixture1)); 70 | }); 71 | --------------------------------------------------------------------------------