@plurid/plurid-data-structures

Plurid Data Structures

Usage no npm install needed!

<script type="module">
  import pluridPluridDataStructures from 'https://cdn.skypack.dev/@plurid/plurid-data-structures';
</script>

README



Version License: DEL

plurid' data structures for typescript

Utility data structures to be used in TypesScript projects.


Contents

About

The package contains the following data structures

Batcher

Runs a batched action of size at certain time after pushing into the Batcher.

import {
    Batcher,
} from '@plurid/plurid-data-structures';


const batcher = new Batcher<string>(
    (batch) => {
        console.log(batch);
    },
    {
        size: 2,
    },
);

batcher.push('one');
batcher.push('two');
batcher.push('three');

Cacher

Caches by id a generic T data structure.

import {
    Cacher,
} from '@plurid/plurid-data-structures';


const cache = new Cacher<string>();

const a = cache.get('one') // undefined
cache.set('one', 'two'); // true
const b = cache.get('one') // 'two'

cache.unset('one') // true
cache.reset(); // true

CacherGetter

A Cacher which runs a cache call after a cache miss.

import {
    CacherGetter,
} from '@plurid/plurid-data-structures';


const cacherGetter = new CacherGetter<string>(
    // An array of cacher calls executed in order until one or none fills the cache request.
    [
        (index: string) => {
            // return based on index value
            if (Math.random() < 0.5) {
                return 'one';
            }

            return;
        },
        async (index: string) => {
            // return based on index value
            // runs asynchronously
            return 'two';
        },
    ],
    {}, // `Cacher` options
);


cacherGetter.get('one'); // 0.5 chance of getting `'one'` or `Promise<'two'>`.
cacherGetter.getAsynchronous('one'); // 0.5 chance of getting `'one'` or `'two'`.

Stepper

The Stepper debounces the incrementation and decrementation of numerical values.

const id = 'one';
const stepper = new Stepper();

stepper.define(
    id,
    async (value) => {
        // use value asynchronously
        // the value will be 2
    },
); // the value is 0

stepper.step(
    id,
    1,
); // the value is 0 + 1 = 1
stepper.step(
    id,
    -1,
); // the value is 1 - 1 = 0
stepper.step(
    id,
    2,
); // the value is 0 + 2 = 2

DeposedString

Computes differences between strings after each push and stores them as internal stages. At get the string is recomposed starting from the initial string following through all the steps of the stages.

The name, deposed string, stands for differentially composable string.

The DeposedString is intended for efficient persistent storage of historic changes to a text (a simple note, a comment, or even a long-form text).

import {
    DeposedString,
} from '@plurid/plurid-data-structures';


const deposedString = new DeposedString('');

deposedString.push('a1b2c3');
deposedString.push('a1c3');
deposedString.push('13d4');

const a = deposedString.get(0); // 'a1b2c3'
const b = deposedString.get(1); // 'a1c3'
const c = deposedString.get(2); // '13d4'
console.log('a, b, c', a, b, c);

const unload = deposedString.unload(); // extract the stages
console.log('unload', JSON.stringify(unload, null, 4));
// unload -> {
//     initial: '',
//     stages: [
//         [
//             [ '+', 0, 'a1b2c3' ],
//         ],
//         [
//             [ '-', 2, 2 ],
//         ],
//         [
//             [ '-', 0, 1 ],
//             [ '-', 1, 1 ],
//             [ '+', 2, 'd4' ],
//         ],
//     ],
// }

const freshDeposedString = new DeposedString('');
freshDeposedString.load(unload.stages); // load the stages into a new DeposedString

const freshC = freshDeposedString.get(2); // '13d4'
console.log('freshC', freshC);

LinkedList

LinkedList implementation.

import {
    LinkedList,
} from '@plurid/plurid-data-structures';


const linkedList = new LinkedList();
linkedList.add(10);
linkedList.add(20);

PieceTable

PieceTable implementation.

import {
    PieceTable,
} from '@plurid/plurid-data-structures';

const pieceTable = new PieceTable('original string');
pieceTable.insert(' text', 15); // insert at index 15, the end
pieceTable.delete(8, 1); // delete at index 8, the space between 'original' and 'string'
const text = pieceTable.stringAt(0, 19); // 'originalstring text'

Packages

Version

@plurid/plurid-data-structures

Codeophon