npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

buffingjs

v1.1.1

Published

An easy yet complete to use iterable data buffer in javascript

Downloads

4

Readme

BufferJs

An easy yet complete to use iterable data buffer in javascript

Ideal for parsing text data or tokens.

Works with any array-like data.

Compatible with both JavaScript and TypeScript

Installation

Download from CDN or install from npm

npm install --save buffingjs

Usage

Basic Example

import { BufferJs } from 'buffingjs';
// Or for javascript
// const BufferJs = require('buffingjs');

const hugeText = `At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium. 
voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati? 
cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est 
laborum et dolorum fuga.`;
const textBuffer = new BufferJs(hugeText);

const list = [1, 2, 3, 4, 5, 6];
const listBuffer = new BufferJs(list);

Moving the cursor

import { BufferJs } from 'buffingjs';
// Or for javascript
// const BufferJs = require('buffingjs');

const data = ['A', 'B', 'C', 'D', 'E'];
const buffer = new BufferJs(code);

console.log(buffer.current);    // 'A'
console.log(buffer.next);       // 'B'
console.log(buffer.prev);       // undefined

buffer.consume(2);              // Same as calling buffer.toNext() twice

console.log(buffer.current);    // 'C'
console.log(buffer.next);       // 'D'
console.log(buffer.prev);       // 'B'

console.log(buffer.toPrev());   // 'B'
console.log(buffer.current);    // 'B'
console.log(buffer.next);       // 'A'
console.log(buffer.prev);       // 'C'

console.log(buffer.position);   // 1
buffer.seek(2);
console.log(buffer.position);   // 2

console.log(buffer.current);    // 'C'
console.log(buffer.next);       // 'D'
console.log(buffer.prev);       // 'B'

Parsing

import { BufferJs } from 'buffingjs';
// Or for javascript
// const BufferJs = require('buffingjs');

const code = `1+2 - 3 * 5 / 823 + 2.501`;
const buffer = new BufferJs(code);

const regexNumbers = /[\d\.]/;
function parseNumbers() {
    let readDot = false;
    const value = buffer.extract(c => {
        if (c === '.') {
            if (readDot) {
                return false;
            }
            readDot = true;
        }
        return regexNumbers.test(c);
    }).join('');

    return {
        type: 'number',
        value: parseFloat(value)
    };
}

const regexOperators = /^(?:\+|\-|\*|\/|>|<|(?:>>)|(?:<<))$/;
function parseOperators() {
    let value = '';
    buffer.iterate(c => {
        const _possibleMatch = value + c;
        if (!regexOperators.test(_possibleMatch)) {
            return false;
        }
        value = _possibleMatch;
        return true;
    });

    return {
        type: 'operator',
        value
    };
}

const tokens = [];
buffer.forEach(current => {
    if (regexNumbers.test(current)) {
        tokens.push(parseNumbers());
    } else if (regexOperators.test(current)) {
        tokens.push(parseOperators());
    }
});

console.log(tokens);

Buffer Api

export

class Buffer

template T

Constructors

constructor

+ new Buffer(data: ArrayLike‹T›): Buffer

Creates an instance of Buffer.

Parameters:

Name | Type | ------ | ------ | data | ArrayLike‹T› |

Returns: Buffer

Accessors

current

get current(): T | undefined

The value at the current position

readonly

type (T | undefined)

Returns: T | undefined


hasCurrent

get hasCurrent(): boolean

Verify if there is a value to read at the current position

readonly

Returns: boolean


hasNext

get hasNext(): boolean

Verify if there is a value to read at the next position

readonly

type boolean

Returns: boolean


hasPrev

get hasPrev(): boolean

Verify if there is a value to read at the previous position

readonly

type boolean

Returns: boolean


length

get length(): number

The length of the buffer

readonly

type number

Returns: number


next

get next(): T | undefined

The next value

readonly

type (T | undefined)

Returns: T | undefined


position

get position(): number

The current position number

readonly

type number

Returns: number


prev

get prev(): T | undefined

The previous read value

readonly

type (T | undefined)

Returns: T | undefined

Methods

at

at(pos: number, mode?: "absolute" | "relative"): T | undefined

Returns the value at position [pos] If mode is 'relative' it is considered from the current position instead of 0 Negative values are accepted in the 'relative' mode In the 'absolute' mode negative values will always yield undefined

Parameters:

Name | Type | ------ | ------ | pos | number | mode? | "absolute" | "relative" |

Returns: T | undefined


consume

consume(count?: undefined | number): void

Consume the value at the current position and move [count] times Negative values are accepted and move backwards instead

Parameters:

Name | Type | ------ | ------ | count? | undefined | number |

Returns: void


drop

drop(): void

Remove the latest saved position from the stack

Returns: void


extract

extract(filter: function): T[]

Iterate from the current position untill [filter] return false or there is no data to process anymore Return the iterated data

Parameters:

filter: function

▸ (next: T, pos: number): boolean

Parameters:

Name | Type | ------ | ------ | next | T | pos | number |

Returns: T[]


forEach

forEach(callback: function): T[]

If the buffer does not change it is position inside the callback

this.consume will be called to ensure an infinite loop is not created by mistake

Parameters:

callback: function

▸ (next: T, pos: number): void

Parameters:

Name | Type | ------ | ------ | next | T | pos | number |

Returns: this


iterate

iterate(filter: function): this

Iterate from the current position untill [filter] return false or there is no data to process anymore

Parameters:

filter: function

▸ (next: T, pos: number): boolean

Parameters:

Name | Type | ------ | ------ | next | T | pos | number |

Returns: this


join

join(sep: string): string

Join the data into a string using the separator [sep]

Parameters:

Name | Type | ------ | ------ | sep | string |

Returns: string


restore

restore(): void

Restore to the latest saved position The position is removed from the stack

Returns: void


rewind

rewind(): void

Return to the starting position of the buffer

Returns: void


save

save(): void

Save the current position for later Saved positions are stored in stack

Returns: void


seek

seek(pos?: undefined | number): void

Change the current position number to [pos] Values bigger than the buffer legth or less than 0 will be forced to stay in the range

Parameters:

Name | Type | ------ | ------ | pos? | undefined | number |

Returns: void


toArray

toArray(): T[]

Returns: T[]


toNext

toNext(): T | undefined

Move to the next position and then return the value

Returns: T | undefined


toPrev

toPrev(): T | undefined

Move to the previous position and then return the value

Returns: T | undefined


toString

toString(): string

Return the string representation of the data

Returns: string