[go: up one dir, main page]

@sigyn/pattern
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Pattern

Loki pattern in JavaScript/TypeScript

npm version size ossf scorecard license

🚧 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

Package Sidebar

Install

npm i @sigyn/pattern

Weekly Downloads

398

Version

1.1.0

License

MIT

Unpacked Size

10.7 kB

Total Files

6

Last publish

Collaborators

  • pierred
  • fraxken