# @sigyn/pattern

> Loki pattern in JavaScript

Latest version **1.1.0** (published 2024-03-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sigyn/pattern
pnpm add @sigyn/pattern
yarn add @sigyn/pattern
bun add @sigyn/pattern
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2024-03-15 |
| First published | 2023-11-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | GENTILHOMME Thomas |
| Maintainers | pierred, fraxken |
| Keywords | grafana, loki, pattern, log, logs |

## Links

- npm: https://www.npmjs.com/package/@sigyn/pattern
- Repository: https://github.com/MyUnisoft/sigyn
- Homepage: https://github.com/MyUnisoft/sigyn/blob/main/src/logql/README.md
- Issues: https://github.com/MyUnisoft/sigyn/issues
- npm.io page: https://npm.io/package/@sigyn/pattern

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2024-03-15
- 1.0.2 — 2024-02-12
- 1.0.1 — 2024-02-12
- 1.0.0 — 2023-11-23

## README

<p align="center"><h1 align="center">
  Pattern
</h1></p>

<p align="center">
  Loki pattern in JavaScript/TypeScript
</p>

<p align="center">
  <a href="https://github.com/MyUnisoft/sigyn/src/pattern">
    <img src="https://img.shields.io/github/package-json/v/MyUnisoft/sigyn/main/src/pattern?style=for-the-badge&label=version" alt="npm version">
  </a>
  <a href="https://github.com/MyUnisoft/sigyn/src/pattern">
    <img src="https://img.shields.io/bundlephobia/min/@sigyn/pattern?style=for-the-badge" alt="size">
  </a>
    <img src="https://api.securityscorecards.dev/projects/github.com/MyUnisoft/sigyn/badge?style=for-the-badge" alt="ossf scorecard">
  </a>
  <a href="https://github.com/MyUnisoft/sigyn/tree/main/src/pattern">
    <img src="https://img.shields.io/github/actions/workflow/status/MyUnisoft/sigyn/pattern.yml?style=for-the-badge">
  </a>
  <a href="https://github.com/MyUnisoft/sigyn/tree/main/src/LICENSE">
    <img src="https://img.shields.io/github/license/MyUnisoft/sigyn?style=for-the-badge" alt="license">
  </a>
</p>

## 🚧 Requirements

- [Node.js](https://nodejs.org/en/) version 18 or higher

## 🚀 Getting Started

This package is available in the Node Package Repository and can be easily installed with [npm](https://doc.npmjs.com/getting-started/what-is-npm) or [yarn](https://yarnpkg.com)

```bash
$ npm i @sigyn/pattern
# or
$ yarn add @sigyn/pattern
```

## 📚 Usage

```ts
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).

```ts
new Pattern([
  "[schema: <id>|type: <user_type>]",
  "HTTP <verb> <_> <endpoint>"
] as const)
```

> [!TIP]
> Use `as const` to benefit from type inference

## 🌐 API

- [Pattern](./docs/Pattern.md)
- [NoopPattern](./docs/NoopPattern.md)

### Shape

<em>Pattern</em> and <em>NoopPattern</em> both implement the same Shape using `PatternShape` interface.

```ts
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

---
_Source: https://npm.io/package/@sigyn/pattern · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
