1.1.0 • Published 2 months ago

@sigyn/pattern v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

🚧 Requirements

🚀 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

1.1.0

2 months ago

1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

6 months ago