@holz/pattern-filter 0.2.0 → 0.5.0
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/dist/holz-pattern-filter.cjs +1 -1
- package/dist/holz-pattern-filter.js +14 -14
- package/package.json +4 -4
- package/src/__tests__/pattern-filter.test.ts +9 -14
- package/src/index.ts +1 -1
- package/src/pattern-filter.ts +10 -15
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.
|
|
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,19 +1,19 @@
|
|
|
1
|
-
const
|
|
2
|
-
(
|
|
1
|
+
const s = (t) => t.split(/[\s,]+/).filter(Boolean).reduce(
|
|
2
|
+
(e, r) => (r[0] === "-" ? e.exclude.push(c(r.slice(1))) : e.include.push(c(r)), e),
|
|
3
3
|
{ include: [], exclude: [] }
|
|
4
|
-
),
|
|
5
|
-
const
|
|
6
|
-
return new RegExp(`^${
|
|
4
|
+
), i = (t, n) => t.exclude.every((e) => !e.test(n)) && t.include.some((e) => e.test(n)), c = (t) => {
|
|
5
|
+
const n = t.replace(/\*/g, ".*?");
|
|
6
|
+
return new RegExp(`^${n}$`);
|
|
7
7
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
function l({
|
|
9
|
+
processor: t,
|
|
10
|
+
pattern: n
|
|
11
|
+
}) {
|
|
12
|
+
const e = s(n);
|
|
13
|
+
return (r) => {
|
|
14
|
+
i(e, r.origin.join(":")) && t(r);
|
|
15
|
+
};
|
|
15
16
|
}
|
|
16
17
|
export {
|
|
17
|
-
|
|
18
|
-
i as default
|
|
18
|
+
l as createPatternFilter
|
|
19
19
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/pattern-filter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Only print log messages that match a pattern",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/holz-pattern-filter.cjs",
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"test:types": "tsc"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@holz/core": "0.
|
|
44
|
-
"@vitest/coverage-c8": "0.28.5",
|
|
45
|
-
"typescript": "4.9.5",
|
|
43
|
+
"@holz/core": "^0.5.0",
|
|
44
|
+
"@vitest/coverage-c8": "^0.28.5",
|
|
45
|
+
"typescript": "^4.9.5",
|
|
46
46
|
"vite": "^4.0.0",
|
|
47
47
|
"vitest": "^0.28.5"
|
|
48
48
|
}
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import type { LogProcessor } from '@holz/core';
|
|
2
1
|
import { createLogger } from '@holz/core';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
class TestBackend implements LogProcessor {
|
|
6
|
-
processLog = vi.fn();
|
|
7
|
-
}
|
|
2
|
+
import { createPatternFilter } from '../pattern-filter';
|
|
8
3
|
|
|
9
4
|
describe('PatternFilter', () => {
|
|
10
5
|
it('only passes through logs that have the filter', () => {
|
|
11
|
-
const backend =
|
|
12
|
-
const filter =
|
|
6
|
+
const backend = vi.fn();
|
|
7
|
+
const filter = createPatternFilter({
|
|
13
8
|
pattern: '-ignored, -*:wild, *',
|
|
14
9
|
processor: backend,
|
|
15
10
|
});
|
|
@@ -17,13 +12,13 @@ describe('PatternFilter', () => {
|
|
|
17
12
|
const logger = createLogger(filter);
|
|
18
13
|
|
|
19
14
|
logger.namespace('ignored').info('excluded by filter');
|
|
20
|
-
expect(backend
|
|
15
|
+
expect(backend).not.toHaveBeenCalled();
|
|
21
16
|
|
|
22
17
|
logger.namespace('something').namespace('wild').info('also excluded');
|
|
23
|
-
expect(backend
|
|
18
|
+
expect(backend).not.toHaveBeenCalled();
|
|
24
19
|
|
|
25
20
|
logger.namespace('app').info('not ignored');
|
|
26
|
-
expect(backend
|
|
21
|
+
expect(backend).toHaveBeenCalledWith(
|
|
27
22
|
expect.objectContaining({
|
|
28
23
|
message: 'not ignored',
|
|
29
24
|
})
|
|
@@ -31,12 +26,12 @@ describe('PatternFilter', () => {
|
|
|
31
26
|
});
|
|
32
27
|
|
|
33
28
|
it('does not send anything by default', () => {
|
|
34
|
-
const backend =
|
|
35
|
-
const filter =
|
|
29
|
+
const backend = vi.fn();
|
|
30
|
+
const filter = createPatternFilter({ pattern: '', processor: backend });
|
|
36
31
|
const logger = createLogger(filter);
|
|
37
32
|
|
|
38
33
|
logger.info('no include patterns mean this is ignored');
|
|
39
34
|
|
|
40
|
-
expect(backend
|
|
35
|
+
expect(backend).not.toHaveBeenCalled();
|
|
41
36
|
});
|
|
42
37
|
});
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createPatternFilter } from './pattern-filter';
|
package/src/pattern-filter.ts
CHANGED
|
@@ -11,27 +11,22 @@ import { parse, matches } from './string-match';
|
|
|
11
11
|
* with the origin `my-app`.
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
|
-
*
|
|
14
|
+
* createPatternFilter({
|
|
15
15
|
* pattern: 'my-app*, -spammy:library',
|
|
16
16
|
* processor: backend,
|
|
17
17
|
* })
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
export function createPatternFilter({
|
|
20
|
+
processor,
|
|
21
|
+
pattern,
|
|
22
|
+
}: Config): LogProcessor {
|
|
23
|
+
const filters = parse(pattern);
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.filters = parse(config.pattern);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
processLog(log: Log) {
|
|
31
|
-
if (matches(this.filters, log.origin.join(':'))) {
|
|
32
|
-
this.processor.processLog(log);
|
|
25
|
+
return (log: Log) => {
|
|
26
|
+
if (matches(filters, log.origin.join(':'))) {
|
|
27
|
+
processor(log);
|
|
33
28
|
}
|
|
34
|
-
}
|
|
29
|
+
};
|
|
35
30
|
}
|
|
36
31
|
|
|
37
32
|
interface Config {
|