Skip to content

Commit

Permalink
fix(pattern): enhance NoopPattern TS signature & add new d.ts test (#209
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fraxken authored Feb 12, 2024
1 parent dfada18 commit f96db61
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/pattern/src/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface PatternShape<T extends LokiPatternType = string> {
}

export class NoopPattern implements PatternShape {
compile(): (log: string) => [log: string] {
compile(): (log: string) => [] | [log: string] {
return (log) => [log];
}

executeOnLogs(logs: Array<string>): Array<string> {
executeOnLogs(logs: Array<string>): LokiLiteralPattern<string>[] {
return logs;
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/pattern/test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ expectType<{
verb: string;
code: string;
}[]>(new Pattern("<verb> <_> <code>").executeOnLogs([]));
expectType<string[]>(new Pattern("invalid pattern should return string").executeOnLogs([]));
expectType<{
verb: string;
code: string;
}[]>(new Pattern(["<verb>", " <_> ", "<code>"] as const).executeOnLogs([]));

expectAssignable<PatternShape>(new NoopPattern());
expectAssignable<PatternShape>(new Pattern("foobar"));
expectType<(log: string) => [] | [log: string]>(new NoopPattern().compile());
expectType<(log: string) => [] | [log: string]>(new Pattern("invalid pattern should return string").compile());
expectType<(log: string) => [] | [log: { foobar: string }]>(new Pattern("<_> <foobar>").compile());

expectType<string[]>(new Pattern("invalid pattern should return string").executeOnLogs([]));
expectType<string[]>(new NoopPattern().executeOnLogs([]));

expectAssignable<PatternShape<string>>(new NoopPattern());
expectAssignable<PatternShape<string>>(new Pattern("foobar"));
expectAssignable<PatternShape<"<_> <foobar>">>(new Pattern("<_> <foobar>"));

0 comments on commit f96db61

Please sign in to comment.