Skip to content

Commit

Permalink
fix(pattern): add Generic to NoopPattern (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken authored Feb 15, 2024
1 parent c3873fb commit d4ad82e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/pattern/src/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export interface PatternShape<T extends LokiPatternType = string> {
executeOnLogs(logs: Array<string>): LokiLiteralPattern<T>[];
}

export class NoopPattern implements PatternShape {
compile(): (log: string) => [] | [log: string] {
return (log) => [log];
export class NoopPattern<T extends LokiPatternType = string> implements PatternShape<T> {
compile(): (log: string) => [] | [log: LokiLiteralPattern<T>] {
return (log) => [log as LokiLiteralPattern<T>];
}

executeOnLogs(logs: Array<string>): LokiLiteralPattern<string>[] {
return logs;
executeOnLogs(logs: Array<string>): LokiLiteralPattern<T>[] {
return logs as LokiLiteralPattern<T>[];
}
}

Expand Down Expand Up @@ -46,7 +46,7 @@ export class Pattern<T extends LokiPatternType> implements PatternShape<T> {
return (log) => {
const match = new RegExp(exprStr).exec(log);

return match === null ? [] : [match.groups as LokiLiteralPattern<T>];
return match === null ? [] : [match.groups as unknown as LokiLiteralPattern<T>];
};
}

Expand Down
10 changes: 3 additions & 7 deletions src/pattern/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type ExtractPattern<Pattern extends string> = Pattern extends `${infer _}<${infe
Name extends "_" ? never : Trim<Name> : never;

export type LokiPatternType = string | Array<string> | ReadonlyArray<string>;
export type LokiLiteralPattern<T extends LokiPatternType> = ConvertEmptyRecord<
Simplify<
TupleToObject<
Split<ArrayToString<T>, ">">
>
>
>;
export type LokiLiteralPattern<T extends LokiPatternType> = ConvertEmptyRecord<Simplify<
TupleToObject<Split<ArrayToString<T>, ">">>
>>;
8 changes: 7 additions & 1 deletion src/pattern/test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { expectType, expectAssignable } from "tsd";

// Import Internal Dependencies
import { Pattern, NoopPattern, PatternShape } from "../../src/pattern";
import {
Pattern,
PatternShape,
NoopPattern
} from "../../src/pattern";

expectType<Pattern<string>>(new Pattern("foobar"));

Expand All @@ -22,6 +26,8 @@ expectType<(log: string) => [] | [log: { foobar: string }]>(new Pattern("<_> <fo
expectType<string[]>(new Pattern("invalid pattern should return string").executeOnLogs([]));
expectType<string[]>(new NoopPattern().executeOnLogs([]));

expectAssignable<PatternShape<string>>(new NoopPattern() || new Pattern("<foobar>"));
expectAssignable<PatternShape<string>>(new NoopPattern());
expectAssignable<PatternShape<string>>(new Pattern("foobar"));
expectAssignable<PatternShape<string>>(new Pattern(["foobar", "yo"]));
expectAssignable<PatternShape<"<_> <foobar>">>(new Pattern("<_> <foobar>"));

0 comments on commit d4ad82e

Please sign in to comment.