@telemok/ring-buffer-uint8array

0.2023.1-2.2 • Public • Published

ring-buffer-uint8array (RingBufferUint8Array)

https://npmjs.com/package/@telemok/ring-buffer-uint8array is the typescript circular ring buffer uint8array with .push .shift .read .write find operations

Features

  • Work in Uint8array - it faster and native.
  • UART/USART stream parsing compatible.
  • Argument checking and fast functions without checking.
  • Fast and easy to learn.
  • Ring overwrite on/off.
  • Any buffer size, not 2^n.
  • NodeJs and browser Javascript support

Installation:

  1. Create your NodeJs, Browser or Webview app.
  2. run: npm install @telemok/ring-buffer-uint8array
  3. import { RingBufferUint8Array } from "@telemok/RingBufferUint8Array.ts"
  4. Ready.

Examples:

  1. Parsing students database
import {RingBufferUint8Array} from "@telemok/RingBufferUint8Array"

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

/* Create instance of RingBuffer */
let rb = new RingBufferUint8Array(10);

/* Data to parse, like streaming from UART. */
const inputStream = "Hello\r\nworld! May be.";

/* Separators to split data. */
const binarySeparators = [textEncoder.encode('\r\n'), textEncoder.encode('!')];

/* Iterate stream data.*/
for(let c of inputStream)
{
    /* Push bytes from stream. */
    rb.pushByte(textEncoder.encode(c)[0]);
    
    /* Extract Uint8array from ringbuffer without separator if separator match or return null. */
    let arr = rb.ifEndsWithUint8ArraysThenFlushUint8Array(binarySeparators, false);
    if(arr !== null)
        console.log("Find: ", textDecoder.decode(arr));
}

It will return:
Find: Hello
Find: world

Package Sidebar

Install

npm i @telemok/ring-buffer-uint8array

Weekly Downloads

2

Version

0.2023.1-2.2

License

Apache-2.0

Unpacked Size

28.4 kB

Total Files

6

Last publish

Collaborators

  • telemok