├── docs ├── .nojekyll ├── docs │ ├── .nojekyll │ ├── assets │ │ ├── .nojekyll │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ └── search.js │ ├── classes │ │ └── .nojekyll │ ├── modules │ │ ├── .nojekyll │ │ ├── generated_package_version.html │ │ └── circular_buffer.html │ ├── modules.html │ └── index.html └── index.html ├── src ├── html │ ├── .nojekyll │ └── index.html ├── ts │ ├── types.ts │ ├── generated │ │ └── package_version.ts │ ├── tests │ │ ├── rig.spec.ts │ │ ├── circular_buffer.spec.ts │ │ └── circular_buffer.stoch.ts │ └── circular_buffer.ts └── build_js │ ├── archive_docs_by_version.js │ ├── release.js │ └── write_version_file.js ├── archive_docs_by_version.js ├── dist ├── generated │ └── package_version.d.ts ├── circular_buffer.d.ts ├── circular_buffer.esm.min.js ├── circular_buffer.cjs.min.js ├── circular_buffer.iife.min.js └── circular_buffer.iife.js ├── .eslintignore ├── jest-stoch.config.js ├── .eslintrc.js ├── jest.config.js ├── rollup.config.js ├── LICENSE ├── .github └── workflows │ └── node.js.yml ├── .gitignore ├── .npmignore ├── tsconfig.json ├── package.json ├── README.md └── CHANGELOG.md /docs/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/html/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/ts/types.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/docs/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/docs/assets/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/docs/classes/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/docs/modules/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /archive_docs_by_version.js: -------------------------------------------------------------------------------- 1 | 2 | // The file you're looking for is in the root directory of the branch gh-pages 3 | -------------------------------------------------------------------------------- /src/build_js/archive_docs_by_version.js: -------------------------------------------------------------------------------- 1 | 2 | // The file you're looking for is in the root directory of the branch gh-pages 3 | -------------------------------------------------------------------------------- /docs/docs/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StoneCypher/circular_buffer_js/HEAD/docs/docs/assets/images/icons.png -------------------------------------------------------------------------------- /docs/docs/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StoneCypher/circular_buffer_js/HEAD/docs/docs/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/docs/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StoneCypher/circular_buffer_js/HEAD/docs/docs/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/docs/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StoneCypher/circular_buffer_js/HEAD/docs/docs/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /dist/generated/package_version.d.ts: -------------------------------------------------------------------------------- 1 | declare const version = "1.10.0", packagename = "circular_buffer_js"; 2 | export { version, packagename }; 3 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | 2 | node_modules 3 | dist 4 | build 5 | .github 6 | coverage 7 | docs 8 | src/html 9 | src/ts/generated 10 | rollup.config.js 11 | -------------------------------------------------------------------------------- /src/ts/generated/package_version.ts: -------------------------------------------------------------------------------- 1 | 2 | // Generated by %proj%/src/build_js/write_version_file.js 3 | 4 | const version = '1.10.0', 5 | packagename = 'circular_buffer_js'; 6 | 7 | export { version, packagename }; 8 | -------------------------------------------------------------------------------- /src/build_js/release.js: -------------------------------------------------------------------------------- 1 | 2 | const fs = require('fs'), 3 | { execSync } = require('child_process'), 4 | package = JSON.parse(fs.readFileSync('./package.json')), 5 | version = `v${package.version}`, 6 | command = `gh release create ${version} -F ./CHANGELOG.md`; 7 | 8 | console.log( `${execSync(command)}` ); 9 | -------------------------------------------------------------------------------- /src/ts/tests/rig.spec.ts: -------------------------------------------------------------------------------- 1 | 2 | import * as fc from 'fast-check'; 3 | 4 | 5 | 6 | 7 | 8 | test( 9 | 'Tests are running (arithmetic isn\'t wrong)', 10 | () => expect(1 + 2).toBe(3) 11 | ); 12 | 13 | 14 | 15 | 16 | 17 | test('fast-check is running (stoch arithmetic isn\'t wrong)', () => { 18 | 19 | fc.assert( 20 | 21 | fc.property( 22 | fc.nat(), fc.nat(), 23 | (a, b) => expect(a + b).toBe(b + a) 24 | ) 25 | 26 | ); 27 | 28 | }); 29 | -------------------------------------------------------------------------------- /jest-stoch.config.js: -------------------------------------------------------------------------------- 1 | 2 | const baseConfig = require('./jest.config.js'); // eslint-disable-line no-undef,@typescript-eslint/no-var-requires 3 | 4 | module.exports = { // eslint-disable-line no-undef 5 | 6 | ... baseConfig, 7 | 8 | testMatch : ['**/*.stoch.ts'], 9 | coverageDirectory : "coverage/stoch/", 10 | 11 | coverageThreshold : { 12 | global : { 13 | branches : 90, 14 | functions : 90, 15 | lines : 90, 16 | statements : 90, 17 | }, 18 | }, 19 | 20 | }; 21 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = { // eslint-disable-line no-undef 3 | 4 | root: true, 5 | 6 | parser: '@typescript-eslint/parser', 7 | 8 | plugins: [ 9 | '@typescript-eslint', 10 | ], 11 | 12 | extends: [ 13 | 'eslint:recommended', 14 | 'plugin:@typescript-eslint/recommended', 15 | ], 16 | 17 | rules: { 18 | "no-unused-vars" : [ "warn", { argsIgnorePattern : "^_", varsIgnorePattern : "^_", caughtErrors : "all", caughtErrorsIgnorePattern : "^_" } ], 19 | "@typescript-eslint/no-unused-vars" : [ "warn", { argsIgnorePattern : "^_", varsIgnorePattern : "^_", caughtErrors : "all", caughtErrorsIgnorePattern : "^_" } ], 20 | } 21 | 22 | }; 23 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = { // eslint-disable-line no-undef 3 | 4 | testEnvironment : 'node', 5 | 6 | moduleFileExtensions : ['js', 'ts'], 7 | coveragePathIgnorePatterns : ["/node_modules/", "/src/ts/tests/"], 8 | testMatch : ['**/*.spec.ts'], 9 | 10 | transform : { '^.+\\.ts$': 'ts-jest' }, 11 | 12 | verbose : false, 13 | collectCoverage : true, 14 | coverageDirectory : "coverage/spec/", 15 | 16 | coverageThreshold : { 17 | global : { 18 | branches : 90, 19 | functions : 90, 20 | lines : 90, 21 | statements : 90, 22 | }, 23 | }, 24 | 25 | collectCoverageFrom: ["src/ts/**/*.{js,ts}"] 26 | 27 | }; 28 | -------------------------------------------------------------------------------- /src/build_js/write_version_file.js: -------------------------------------------------------------------------------- 1 | 2 | /* eslint-disable no-undef */ 3 | /* eslint-disable @typescript-eslint/no-var-requires */ 4 | 5 | const fs = require('fs'), 6 | package = JSON.parse(`${fs.readFileSync('./package.json')}`); 7 | 8 | const filename = './src/ts/generated/package_version.ts'; 9 | 10 | /* eslint-enable @typescript-eslint/no-var-requires */ 11 | /* eslint-enable no-undef */ 12 | 13 | 14 | const data_template = ` 15 | // Generated by %proj%/src/build_js/write_version_file.js 16 | 17 | const version = '${package.version}', 18 | packagename = '${package.name}'; 19 | 20 | export { version, packagename }; 21 | `; 22 | 23 | fs.writeFileSync(filename, data_template, { flag: 'w', encoding: 'utf8'}); 24 | 25 | console.log(`# Wrote version file as ${package.name} ${package.version}; finished`); // eslint-disable-line no-undef 26 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | 2 | const es6_config = { 3 | 4 | input : 'build/es6/circular_buffer.js', 5 | 6 | output : { 7 | file : 'build/circular_buffer.esm.js', 8 | format : 'es', 9 | name : 'circular_buffer' 10 | } 11 | 12 | }; 13 | 14 | 15 | 16 | 17 | 18 | const cjs_config = { 19 | 20 | input : 'build/es6/circular_buffer.js', 21 | 22 | output : { 23 | file : 'build/circular_buffer.cjs.js', 24 | format : 'cjs', 25 | name : 'circular_buffer' 26 | } 27 | 28 | }; 29 | 30 | 31 | 32 | 33 | 34 | const iife_config = { 35 | 36 | input : 'build/es6/circular_buffer.js', 37 | 38 | output : { 39 | file : 'build/circular_buffer.iife.js', 40 | format : 'iife', 41 | name : 'circular_buffer' 42 | } 43 | 44 | }; 45 | 46 | 47 | 48 | 49 | 50 | export default [ es6_config, cjs_config, iife_config ]; 51 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 |
10 |Generated using TypeDoc
112 |Generated using TypeDoc
149 |Generated using TypeDoc
194 |npm install --save-dev circular_buffer_js
70 |
71 | Typescript implementation of a circular buffer, and JS compiles to a es6
79 | module minified, es6 commonjs minified, es6 iife minified, and es6 iife full.
es6 minified module build is currently 1.4k.typescript are involved.You should consider viewing the 99 | real documentation, 100 | but:
101 |// yields a buffer of fixed size `size`
102 | const cb = new circular_buffer(size),
103 | cb2 = circular_buffer.from([1,2,3]);
104 |
105 | cb.push(item); // inserts `item` at end of `cb`, then returns `item`
106 | cb.shove(item); // inserts `item` at end of `cb`; remove if needed, returns removed
107 | cb.pop(); // removes and returns first element
108 | cb.at(location); // shows the element at 0-indexed offset `location`
109 | cb.pos(location); // shows the element at run-indexed offset `location`
110 | cb.offset(); // shows the delta from 0-indexed to head
111 | cb.indexOf(item); // returns the index of the first `item`, or -1
112 | cb.find(pred); // return the the first match or undefined
113 | cb.every(pred); // tests if every queue element satisfies the predicate
114 | cb.some(pred); // tests if at least one element satisfies the predicate
115 | cb.fill(item); // maxes `length` and sets every element to `item`
116 | cb.clear(); // empties the container
117 | cb.reverse(); // reverses the container
118 | cb.resize(size,pE); // change to new size, truncating if required; pE to prefer end
119 | cb.toArray(); // return an array of the current contents of the queue
120 |
121 | cb.first; // returns the first value in the queue; throws when empty
122 | cb.last; // returns the last value in the queue; throws when empty
123 | cb.isFull; // returns `true` if no space left; `false` otherwise
124 | cb.isEmpty; // returns `true` if no space remains; `false` otherwise
125 | cb.available; // returns the number of spaces remaining currently
126 | cb.capacity; // returns the total `size` allocated
127 | cb.length; // returns the amount of space currently used
128 |
129 | This is a circular buffer (or cycle buffer, ring queue, etc.) It was written 134 | because a library I wanted to use had a native buggy implementation, so I 135 | provided something more trustworthy.
136 |A circular buffer is a fixed size buffer that allows you to push and pop 137 | forever, as a first in first out queue-like structure. Circular buffers are 138 | more efficient than queues, but can overflow.
139 |import { circular_buffer } from 'circular_buffer_js';
144 |
145 | const cb = new circular_buffer(3); // [ , , ]
146 |
147 | cb.push(1); // ok: [1, , ]
148 | cb.push(2); // ok: [1,2, ]
149 | cb.push(3); // ok: [1,2,3]
150 |
151 | cb.at(0); // 1
152 | cb.first; // 1
153 | cb.last; // 3
154 |
155 | cb.push(4); // throws - full! ; [1,2,3]
156 |
157 | cb.pop(); // 1: [2,3, ]
158 | cb.at(0); // 2: [2,3, ]
159 |
160 | cb.push(4); // ok: [2,3,4]
161 | cb.push(5); // throws - full! ; [2,3,4]
162 |
163 | cb.pop(); // 2: [3,4, ]
164 | cb.pop(); // 3: [4, , ]
165 | cb.pop(); // 4: [ , , ]
166 |
167 | cb.pop(); // throws - empty! ; [ , , ]
168 |
169 | It's typescript, so you can also
174 |import { circular_buffer } from 'circular_buffer_js';
175 | const cb = new circular_buffer<number>(3);
176 |
177 | And there's a CommonJS build, so you can
182 |const cbuf = require('circular_buffer_js'),
183 | circular_buffer = new cbuf.circular_buffer;
184 |
185 | There're also two iife builds - both regular and minified - so that you can
190 | use this in older browsers, or from CDN.
<script defer type="text/javascript" src="circular_buffer_js.min.js"></script>
192 | <script defer type="text/javascript">
193 |
194 | window.onload = () => {
195 |
196 | console.log(
197 | `Using circular buffer version ${circular_buffer.version}`
198 | );
199 |
200 | // package // class
201 | const mybuf = new circular_buffer.circular_buffer(5);
202 |
203 | };
204 |
205 | </script>
206 |
207 | If this doesn't meet your needs, please try:
212 |Generated using TypeDoc
264 |