Detalhes do pacote

circular_buffer_js

StoneCypher6.5kMIT1.10.0

Fast TS/JS implementation of a circular buffer (aka ring queue, cyclic list, etc.) Extremely well tested.

circular, buffer, circular-buffer, ring

readme (leia-me)

circular_buffer_js

MIT License NPM Version Coveralls Issues GitHub contributors

npm install --save-dev circular_buffer_js

Typescript implementation of a circular buffer, and JS compiles to a es6 module minified, es6 commonjs minified, es6 iife minified, and es6 iife full.

  1. Well tested.
    • 100% coverage, 100% property coverage.
  2. Tiny.
    • The es6 minified module build is currently 1.4k.
  3. Dependency-free.
    • Only dev-time deps like typescript are involved.



API

You should consider viewing the real documentation, but:

// yields a buffer of fixed size `size`
const cb  = new circular_buffer(size),
      cb2 = circular_buffer.from([1,2,3]);

cb.push(item);       // inserts `item` at end of `cb`, then returns `item`
cb.shove(item);      // inserts `item` at end of `cb`; remove if needed, returns removed
cb.pop();            // removes and returns first element
cb.at(location);     // shows the element at 0-indexed offset `location`
cb.pos(location);    // shows the element at run-indexed offset `location`
cb.offset();         // shows the delta from 0-indexed to head
cb.indexOf(item);    // returns the index of the first `item`, or -1
cb.find(pred);       // return the the first match or undefined
cb.every(pred);      // tests if every queue element satisfies the predicate
cb.some(pred);       // tests if at least one element satisfies the predicate
cb.fill(item);       // maxes `length` and sets every element to `item`
cb.clear();          // empties the container
cb.reverse();        // reverses the container
cb.resize(size,pE);  // change to new size, truncating if required; pE to prefer end
cb.toArray();        // return an array of the current contents of the queue

cb.first;            // returns the first value in the queue; throws when empty
cb.last;             // returns the last value in the queue; throws when empty
cb.isFull;           // returns `true` if no space left; `false` otherwise
cb.isEmpty;          // returns `true` if no space remains; `false` otherwise
cb.available;        // returns the number of spaces remaining currently
cb.capacity;         // returns the total `size` allocated
cb.length;           // returns the amount of space currently used



What is this?

This is a circular buffer (or cycle buffer, ring queue, etc.) It was written because a library I wanted to use had a native buggy implementation, so I provided something more trustworthy.

A circular buffer is a fixed size buffer that allows you to push and pop forever, as a first in first out queue-like structure. Circular buffers are more efficient than queues, but can overflow.


Basic usage

import { circular_buffer } from 'circular_buffer_js';

const cb = new circular_buffer(3);  // [ , , ]

cb.push(1); // ok: [1, , ]
cb.push(2); // ok: [1,2, ]
cb.push(3); // ok: [1,2,3]

cb.at(0); // 1
cb.first; // 1
cb.last;  // 3

cb.push(4); // throws - full! ; [1,2,3]

cb.pop(); // 1: [2,3, ]
cb.at(0); // 2: [2,3, ]

cb.push(4); // ok: [2,3,4]
cb.push(5); // throws - full! ; [2,3,4]

cb.pop(); // 2: [3,4, ]
cb.pop(); // 3: [4, , ]
cb.pop(); // 4: [ , , ]

cb.pop(); // throws - empty! ; [ , , ]


Typescript

It's typescript, so you can also

import { circular_buffer } from 'circular_buffer_js';
const cb = new circular_buffer<number>(3);


Node CommonJS

And there's a CommonJS build, so you can

const cbuf            = require('circular_buffer_js'),
      circular_buffer = new cbuf.circular_buffer;


Browser <script>

There're also two iife builds - both regular and minified - so that you can use this in older browsers, or from CDN.

<script defer type="text/javascript" src="circular_buffer_js.min.js"></script>
<script defer type="text/javascript">

  window.onload = () => {

    console.log(
      `Using circular buffer version ${circular_buffer.version}`
    );

                      // package      // class
    const mybuf = new circular_buffer.circular_buffer(5);

  };

</script>



Alternatives

If this doesn't meet your needs, please try:

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Unreleased

  • 1.9.0 implement shove/1, fixes #55 #55
  • Add release process. Re-achieve 100% double-coverage bb822c2
  • Minor improvements to the release process c789600

v1.8.2

26 February 2022

v1.8.1

26 February 2022

v1.8.0

26 February 2022

  • Add offset and pos #54
  • stoch test in for .offset, fixes #52 .offset implemented, fixes #53 .offset tested #52 #53
  • stoch test in for .pos, fixes #50 .pos implemented, fixes #51 .pos tested #50 #51
  • Add tests for set .capacity, fixes #49 #49
  • draft implementations 0dd5d0f
  • unit tests 3d2637f
  • SetLength test was wrong: it asserted that length matched what was set, which isn't true when setting a higher size 60c7819
  • SetLength is either a broken test or has found a bug 172d926

v1.7.1

29 July 2021

v1.7.0

26 July 2021

  • resize/1, fixes #3 #3

v1.6.0

26 July 2021

v1.5.2

26 July 2021

v1.5.1

26 July 2021

  • readme mistake about find 5acae2b

v1.5.0

26 July 2021

  • find/1-2, fixes #15 #15
  • some/1, fixes #46 #46
  • Close a fairly gross line-left-behind bug causing unexpected reversals c18c0e2

v1.3.0

25 July 2021

  • every/1-2, fixes #45 #45

v1.2.0

25 July 2021

v1.1.0

25 July 2021

  • from/1-3, fixes #9 #9

v1.0.1

19 July 2021

v1.0.0

19 July 2021

v0.25.0

19 July 2021

  • tagging done 40ab035
  • prep for 1.0.0 underway, gonna go tag a lot of things c5aee62

v0.24.1

19 July 2021

  • we do need a few steenking badges 6f3cbe0

v0.24.0

19 July 2021

  • Create landing page, fixes #32 #32

v0.23.1

19 July 2021

v0.23.0

19 July 2021

  • eslint #44
  • Setting up coveralls 20d395a
  • Setting up coveralls with correct package number 52de726

v0.22.0

19 July 2021

  • Write the changelog to disk #43
  • Add changelog #42
  • eslint 1caa887

v0.21.3

19 July 2021

  • Write the changelog to disk 2d035c2
  • Okay changelogs are semi-automated now 1e54733
  • Okay, NPM standard doesn't work here 1ad912b

v0.21.2

19 July 2021

  • Automating the build system 52eb471
  • Automating the bump system 2923f64

v0.21.1

19 July 2021

  • Setting up a changelog tagging toolchain 456e34f

v0.20.1

19 July 2021

  • clear/0 now returns toArray/0, fixes #39 #39
  • toArray/0, simpler .pop/0, fixes #8 #8

v0.19.0

19 July 2021

  • There, wrote some basic documentation, fixes #41 #41

v0.18.1

19 July 2021

  • we're putting the docs in the repo now 85bf3f4
  • rebuild for putting the docs in the repo a35ae5b

v0.18.0

19 July 2021

  • implements .first and .last, fixes #40 #40
  • narrow the example code so comments fit on npm f0f2eb8
  • change the api in the readme 810babe
  • and the api 83e8edb

v0.17.0

19 July 2021

  • .npmignore to reduce the package, improve test labels 464bf40
  • changes methods to getters, camelcases booleans to disambiguate c44cdb4

v0.16.0

19 July 2021

  • implements clear/0 #38
  • readme improvements a6b608d

v0.15.15

19 July 2021

  • sets ref to docs in readme, fixes #33; .nojekyll fixes #34; small readme tips; adds fill/1 to api list in readme #33 #34
  • better github action only calls macs once, improves node range d428fbd
  • more .nojekyll in more directories because le sigh 5a573e4

v0.15.11

19 July 2021

  • Should close both issues - module path in package was missing dist, didn't copy subordinate .d.ts - fixes #31 #31
  • Indicate types in package.json, fixes #30; fill stoch coverage defect in fill/1 #30
  • lawdy. @drohen catching me screwing the pooch left and right. really need to automate the ci build to npm b9f6d25
  • bump and rebuild 38f1661
  • merge error in readme apparently fbb1a4f

v0.15.8

19 July 2021

  • Fill #26
  • build wasn't up to date (gross) fixes #28 #28
  • fixes #17 #17
  • fixes #17 - Adjusted comments, fixed off-by-one error, 'any' to 'T' patch #17
  • fixes #17 #17
  • merging with master f6e988f
  • test for fill/partial/3 bbf9dd6
  • testing fill d4711ec
  • catching up -nothing to see here 87eccd6
  • try, try again e184841
  • ... b1ad533
  • fix that ts-jest was a dep instead of a devdep; readme; disable failing docs path 53afc6a
  • better readme 769dfbe
  • Let's try auto-docs again 9c40f37
  • dix for length and fill/full test b4386ae
  • Let's take another stab at auto-docs feca402
  • local dd2b989
  • Update README.md 3dac9ec
  • putting fill where it belongs? 3b4051e
  • Update README.md 562eb0b
  • initial setup 09f426b
  • converted toStrictEqual to toEqual 1539e27
  • regen ec02720
  • second attempt withut the golfing 92d087e
  • sync with main e693d7f
  • resolving conflicts d0b995f
  • autodoc yaml fixes? 9b83b01
  • added rangeError a4d8448
  • conflicting files 2b71ab5
  • spacing 851e491
  • actually since we know it's ubuntu, we can do it directly there 6f6a361
  • add needs clause to enforce seriality; fix script location in yaml 09e1ca7
  • resolving the other two conflicting branches b804a28
  • Let's try auto-docs ... AGAIN. Trying to cope with rimraf install now f91de5d
  • spacing 7e140a9
  • stray rimraf ref b3e2720
  • adding a cache of package, installed rimraf in pages thread e5e832d
  • fill function attempt 1 4f345ac
  • trigger build without causing a fault 8c7bc7b
  • trigger build 67b789f
  • Update README.md c3c1974
  • fix for stray blank line d427252
  • Update README.md 1e4c0bc

v0.15.0

19 July 2021

  • main and module fields. re-add dist. disable underway doc stuff 2c15cd3
  • move archival script to appropriate branch; place warning dummies e9da39b
  • Attempting automatic doc archival 3ed2e08
  • maybe that error is actually about the following line 0561b59
  • trying a slightly more detailed approach c0e1aed
  • right, that package doesn't exist anymore, do it directly e1d3643
  • maybe ... maybe that's a string? 18eba1e

v0.13.2

19 July 2021

v0.13.1

19 July 2021

  • More serious stochastics. Separate stoch coverage fdrom spec coverage 84987d6

v0.13.0

19 July 2021

v0.12.0

19 July 2021

v0.11.2

19 July 2021

  • early 100% coverage, no stoch yet bcfd509
  • weird testing thing 0f9c917
  • Much improve the spec. Rename it to not be topic specific. 1fbaaa8

v0.11.0

19 July 2021

  • first steps towards basic unit tests 92f1d67

v0.10.0

19 July 2021

  • working fast-check in jest, and in build 4e3de2c

v0.9.0

19 July 2021

v0.8.0

19 July 2021

v0.7.0

19 July 2021

  • Move docs to a branch to reduce install size. Remove docs from main entirely. Remove versioning scheme from main. Expect to recreate in CI f61c05b
  • servicable at/1, push/1 a7ae11a
  • first steps at ci automation 99e265c
  • vestige of old docs directory 24c3fff
  • Add CI script and push to trigger 49ae90f

v0.6.0

19 July 2021

  • Minification. Moved verbose targets to non-tracked build. Minified into dist instead. Kept unminified iife only. edbadd8

v0.5.0

19 July 2021

  • Versioned documentation build now works :D 95b65de
  • Rollup bundling. Output is unminified esm, iife, cjs. 7f0c630

v0.4.0

19 July 2021

  • docs directory set up. typedoc extracting to current directory b320512

v0.3.0

19 July 2021

  • typescript now installed, building. version now part of build e9304d3

v0.2.0

19 July 2021

  • build and clean processes; version number file generated 57e4227

v0.1.0

19 July 2021