# @tblabs/fluentparser

> Fluent bytes stream parser and builder

Latest version **1.0.5** (published 2018-10-21) · beerware license · 0 weekly downloads

## Install

```sh
npm install @tblabs/fluentparser
pnpm add @tblabs/fluentparser
yarn add @tblabs/fluentparser
bun add @tblabs/fluentparser
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2018-10-21 |
| First published | 2018-10-21 |
| Weekly downloads | 0 |
| License | beerware |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | 8.4.0 |
| Dependencies | 1 |
| Unpacked size | 75.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tB |
| Maintainers | tblabs |

## Links

- npm: https://www.npmjs.com/package/@tblabs/fluentparser
- npm.io page: https://npm.io/package/@tblabs/fluentparser

## Dependencies (1)

- [jasmine-core](https://npm.io/package/jasmine-core.md) ^2.6.1

## Recent versions

- 1.0.5 (latest) — 2018-10-21
- 1.0.3 — 2018-10-21
- 1.0.2 — 2018-10-21
- 1.0.1 — 2018-10-21
- 1.0.0 — 2018-10-21

## README

# Fluent bytes frame Parser & Builder

Bytes stream parser and builder with fluent API.

## Frame build

```
    // Arrange
    const frameBuilder = new FluentBuilder();
    const cmd: byte = 0x04;
    const value: number = 1042;

    // Prepare frame
    const frame = frameBuilder
        .Byte(0x01).Byte(cmd).Word4LE(value).Xor()
        .Build();

    // And send
    serialPort.send(frame);
```

## Frame parse

```
    // Define parser output structure
    interface FrameData
    {
        cmd: number;
        value: number;
    }

    // Build parser
    const parserBuilder = new FluentParserBuilder<FrameData>();

    const parser: FluentParser = parserBuilder
        .Is(0x01).Get('cmd').Get4LE('value').IsXor()
        .Build();

    // Hook up on parse complete 
    parser.OnComplete(({cmd, value}) =>
    {
        console.log(cmd, value);
    });

    // Pump data into parser
    serialPort.on('data', (data) =>
    {
        data.forEach(b => parser.Parse(b));
    }
```

## Important classes
- FluentBuilder - builds binary frames with fluent API
- FluentParserBuilder<T> - definition of parser with fluent API
- FluentParser<T> - result of Build() on FluentParserBuilder

## API

### Frame Builder

- Byte(8-bit value) - adds one byte to output frame
- Word2LE(16-bit value) - adds two bytes as little-endian
- Word4LE(16-bit value) - adds four bytes as little-endian
- Xor() - calculates XOR of all previous bytes in frame

### Parser Builder

- Is(8-bit value) - check value
- Any() - omit value
- Get(varName: string) - stores single byte under varName
- Get2LE(varName: string) - builds 16-bit little-endian value from 2 bytes
- Get2BE(varName: string) - builds 16-bit big-endian value from 2 bytes
- Get4LE(varName: string) - builds 32-bit little-endian value from 4 bytes
- Get4BE(varName: string) - builds 32-bit big-endian value from 4 bytes
- If(toCompare: byte, varName: string, operationsList: list of operations) - attach new stream if param is fulfilled
- IsXor() - calculates xor of previous bytes and compares it with current byte
- Build() - returns FluentParser object

### Parser 
- Parse(single byte) - parser input
- OnComplete(callback: (extractedParams: T) => void): void
- OnFault(callback: (reason: string, frame: byte[]) => void): void

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