@holz/pattern-filter 0.7.0 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jesse Gibson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=t=>t.split(/[\s,]+/).filter(Boolean).reduce((e,n)=>(n[0]==="-"?e.exclude.push(c(n.slice(1))):e.include.push(c(n)),e),{include:[],exclude:[]}),i=(t,r)=>t.exclude.every(e=>!e.test(r))&&t.include.some(e=>e.test(r)),c=t=>{const r=t.replace(/\*/g,".*?");return new RegExp(`^${r}$`)};function l({processor:t,pattern:r}){const e=s(r);return n=>{i(e,n.origin.join(":"))&&t(n)}}exports.createPatternFilter=l;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=t=>t.split(/[\s,]+/).filter(Boolean).reduce((e,n)=>(n[0]==="-"?e.exclude.push(c(n.slice(1))):e.include.push(c(n)),e),{include:[],exclude:[]}),l=(t,r)=>t.exclude.every(e=>!e.test(r))&&t.include.some(e=>e.test(r)),c=t=>{const r=t.replace(/\*/g,".*?");return new RegExp(`^${r}$`)},i=({processor:t,pattern:r})=>{const e=s(r);return n=>{l(e,n.origin.join(":"))&&t(n)}};exports.createPatternFilter=i;
@@ -22,6 +22,6 @@ declare interface Config {
22
22
  * processor: backend,
23
23
  * })
24
24
  */
25
- export declare function createPatternFilter({ processor, pattern, }: Config): LogProcessor;
25
+ export declare const createPatternFilter: ({ processor, pattern, }: Config) => LogProcessor;
26
26
 
27
27
  export { }
@@ -1,19 +1,18 @@
1
1
  const s = (t) => t.split(/[\s,]+/).filter(Boolean).reduce(
2
2
  (e, r) => (r[0] === "-" ? e.exclude.push(c(r.slice(1))) : e.include.push(c(r)), e),
3
3
  { include: [], exclude: [] }
4
- ), i = (t, n) => t.exclude.every((e) => !e.test(n)) && t.include.some((e) => e.test(n)), c = (t) => {
4
+ ), l = (t, n) => t.exclude.every((e) => !e.test(n)) && t.include.some((e) => e.test(n)), c = (t) => {
5
5
  const n = t.replace(/\*/g, ".*?");
6
6
  return new RegExp(`^${n}$`);
7
- };
8
- function l({
7
+ }, i = ({
9
8
  processor: t,
10
9
  pattern: n
11
- }) {
10
+ }) => {
12
11
  const e = s(n);
13
12
  return (r) => {
14
- i(e, r.origin.join(":")) && t(r);
13
+ l(e, r.origin.join(":")) && t(r);
15
14
  };
16
- }
15
+ };
17
16
  export {
18
- l as createPatternFilter
17
+ i as createPatternFilter
19
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holz/pattern-filter",
3
- "version": "0.7.0",
3
+ "version": "0.8.2",
4
4
  "description": "Only print log messages that match a pattern",
5
5
  "type": "module",
6
6
  "repository": {
@@ -22,8 +22,7 @@
22
22
  "license": "MIT",
23
23
  "sideEffects": false,
24
24
  "files": [
25
- "dist",
26
- "src"
25
+ "dist"
27
26
  ],
28
27
  "keywords": [
29
28
  "holz",
@@ -38,15 +37,16 @@
38
37
  "test:types": "tsc"
39
38
  },
40
39
  "peerDependencies": {
41
- "@holz/core": "0.7.0"
40
+ "@holz/core": "^0.8.0"
42
41
  },
43
42
  "devDependencies": {
44
- "@holz/core": "^0.7.0",
43
+ "@holz/core": "^0.8.2",
45
44
  "@vitest/coverage-v8": "^3.0.8",
46
- "typescript": "^5.0.0",
45
+ "typescript": "^5.8.2",
47
46
  "vite": "^6.0.0",
48
47
  "vite-plugin-dts": "^4.5.3",
49
48
  "vite-tsconfig-paths": "^5.1.4",
50
49
  "vitest": "^3.0.8"
51
- }
52
- }
50
+ },
51
+ "gitHead": "f099a09cd01834de9bc1714908183cbb7b828c85"
52
+ }
@@ -1,37 +0,0 @@
1
- import { createLogger } from '@holz/core';
2
- import { createPatternFilter } from '../pattern-filter';
3
-
4
- describe('PatternFilter', () => {
5
- it('only passes through logs that have the filter', () => {
6
- const backend = vi.fn();
7
- const filter = createPatternFilter({
8
- pattern: '-ignored, -*:wild, *',
9
- processor: backend,
10
- });
11
-
12
- const logger = createLogger(filter);
13
-
14
- logger.namespace('ignored').info('excluded by filter');
15
- expect(backend).not.toHaveBeenCalled();
16
-
17
- logger.namespace('something').namespace('wild').info('also excluded');
18
- expect(backend).not.toHaveBeenCalled();
19
-
20
- logger.namespace('app').info('not ignored');
21
- expect(backend).toHaveBeenCalledWith(
22
- expect.objectContaining({
23
- message: 'not ignored',
24
- }),
25
- );
26
- });
27
-
28
- it('does not send anything by default', () => {
29
- const backend = vi.fn();
30
- const filter = createPatternFilter({ pattern: '', processor: backend });
31
- const logger = createLogger(filter);
32
-
33
- logger.info('no include patterns mean this is ignored');
34
-
35
- expect(backend).not.toHaveBeenCalled();
36
- });
37
- });
@@ -1,56 +0,0 @@
1
- import { parse, matches } from '../string-match';
2
-
3
- describe('string-match', () => {
4
- describe('parse', () => {
5
- it('parses include patterns', () => {
6
- expect(parse(' foo,bar baz , qux ')).toEqual({
7
- include: [/^foo$/, /^bar$/, /^baz$/, /^qux$/],
8
- exclude: [],
9
- });
10
- });
11
-
12
- it('detects and groups exclusion patterns', () => {
13
- expect(parse('first,-second,-third,fourth')).toEqual({
14
- include: [/^first$/, /^fourth$/],
15
- exclude: [/^second$/, /^third$/],
16
- });
17
- });
18
-
19
- it('converts star patterns to regex wildcards', () => {
20
- expect(parse('first,second*,split*part')).toEqual({
21
- include: [/^first$/, /^second.*?$/, /^split.*?part$/],
22
- exclude: [],
23
- });
24
- });
25
- });
26
-
27
- describe('matches', () => {
28
- it('returns true if the pattern matches', () => {
29
- expect(matches(parse('hello'), 'hello')).toBe(true);
30
- });
31
-
32
- it('returns true if the pattern matches a wildcard', () => {
33
- expect(matches(parse('hello*'), 'hello world')).toBe(true);
34
- });
35
-
36
- it('returns false if the pattern does not match', () => {
37
- expect(matches(parse('hello'), 'bar')).toBe(false);
38
- });
39
-
40
- it('returns false if the pattern is excluded', () => {
41
- expect(matches(parse('-hello'), 'hello')).toBe(false);
42
- });
43
-
44
- it('returns true if the pattern is included', () => {
45
- expect(matches(parse('hello'), 'hello')).toBe(true);
46
- });
47
-
48
- it('does not match if there is a contradiction', () => {
49
- expect(matches(parse('maybe,-maybe'), 'maybe')).toBe(false);
50
- });
51
-
52
- it('does not match if the filter is empty', () => {
53
- expect(matches(parse(''), 'content')).toBe(false);
54
- });
55
- });
56
- });
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export { createPatternFilter } from './pattern-filter';
@@ -1,38 +0,0 @@
1
- import type { Log, LogProcessor } from '@holz/core';
2
- import { parse, matches } from './string-match';
3
-
4
- /**
5
- * Only forwards logs who's origin matches the given pattern. It works the
6
- * same as the popular `debug` module.
7
- *
8
- * Patterns allow wildcards and negation. For example, the pattern `my-app*`
9
- * will match all logs with the origin `my-app` or `my-app:foo` or
10
- * `my-app:foo:bar`. The pattern `*, -my-app` will match all logs except those
11
- * with the origin `my-app`.
12
- *
13
- * @example
14
- * createPatternFilter({
15
- * pattern: 'my-app*, -spammy:library',
16
- * processor: backend,
17
- * })
18
- */
19
- export function createPatternFilter({
20
- processor,
21
- pattern,
22
- }: Config): LogProcessor {
23
- const filters = parse(pattern);
24
-
25
- return (log: Log) => {
26
- if (matches(filters, log.origin.join(':'))) {
27
- processor(log);
28
- }
29
- };
30
- }
31
-
32
- interface Config {
33
- /** A set of patterns to test against `log.origin`. */
34
- pattern: string;
35
-
36
- /** Where to send logs if they pass the filter. */
37
- processor: LogProcessor;
38
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * Parse a string of multiple patterns into a set of regular expressions. We
3
- * use a pattern string (instead of regexes) because it's easier to persist in
4
- * storage or specify through a CLI.
5
- *
6
- * Special thanks to `debug-js` for the inspiration. I read their code as
7
- * a guide for this implementation.
8
- */
9
- export const parse = (input: string): PatternFilters => {
10
- const patterns = input.split(/[\s,]+/).filter(Boolean);
11
-
12
- return patterns.reduce<PatternFilters>(
13
- (groups, pattern) => {
14
- if (pattern[0] === '-') {
15
- groups.exclude.push(toRegex(pattern.slice(1)));
16
- } else {
17
- groups.include.push(toRegex(pattern));
18
- }
19
-
20
- return groups;
21
- },
22
- { include: [], exclude: [] },
23
- );
24
- };
25
-
26
- /** Detect if a string matches a set of patterns. */
27
- export const matches = (filters: PatternFilters, input: string): boolean => {
28
- return (
29
- filters.exclude.every((pattern) => !pattern.test(input)) &&
30
- filters.include.some((pattern) => pattern.test(input))
31
- );
32
- };
33
-
34
- const toRegex = (pattern: string): RegExp => {
35
- const regex = pattern.replace(/\*/g, '.*?');
36
- return new RegExp(`^${regex}$`);
37
- };
38
-
39
- interface PatternFilters {
40
- include: Array<RegExp>;
41
- exclude: Array<RegExp>;
42
- }