@sigyn/pattern
v1.1.0
Published
Loki pattern in JavaScript
Downloads
2,397
Readme
🚧 Requirements
- Node.js version 18 or higher
🚀 Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn
$ npm i @sigyn/pattern
# or
$ yarn add @sigyn/pattern
📚 Usage
import { Pattern } from "@sigyn/pattern";
const parser = new Pattern("HTTP <verb> <_> <endpoint>");
const parsedLogs = parser.executeOnLogs([
"HTTP POST 200 /api/v1/dowork",
"HTTP GET 200 /api/v1/dowork"
]);
// Automatically infer types
for (const { verb, endpoint } of parsedLogs) {
console.log(verb, endpoint);
}
You can also provide an Array to Pattern constructor (quite helpful with long or multipart patterns).
new Pattern([
"[schema: <id>|type: <user_type>]",
"HTTP <verb> <_> <endpoint>"
] as const)
[!TIP] Use
as const
to benefit from type inference
🌐 API
Shape
Pattern and NoopPattern both implement the same Shape using PatternShape
interface.
type LokiPatternType = string | Array<string> | ReadonlyArray<string>;
interface PatternShape<T extends LokiPatternType = string> {
compile(): (log: string) => [] | [log: LokiLiteralPattern<T>];
executeOnLogs(logs: Array<string>): LokiLiteralPattern<T>[];
}
License
MIT