@se-oss/circular-buffer
A lightweight TypeScript implementation of a fixed-capacity circular buffer, perfect for rolling windows, and efficient FIFO queues with automatic overwrite on full capacity.
📦 Installation
npm i @se-oss/circular-buffer
📖 Usage
import { CircularBuffer } from '@se-oss/circular-buffer';
// Create a buffer with capacity for 5 items
const buf = new CircularBuffer(5);
// Add some numbers
buf.put(10);
buf.put(20);
buf.put(30);
// Check status
console.log(buf.isEmpty()); // false
console.log(buf.isFull()); // false
console.log(buf.size()); // 3
// Random access
console.log(buf.at(0)); // 10
console.log(buf.at(-1)); // 30
// Iterate in FIFO order
for (const num of buf) {
console.log(num);
}
// → 10, 20, 30
// Overwrite when full
buf.put(40);
buf.put(50);
buf.put(60); // now full, this overwrites the oldest entry (10)
console.log(buf.toArray());
// → [20, 30, 40, 50, 60]
// Overwrite at gien index
buf.putAt(70, -2);
// → [20, 30, 40, 70, 60]
// Clear it
buf.clear();
console.log(buf.isEmpty()); // true
📚 Documentation
For all configuration options, please see the API docs.
🔗 References
🤝 Contributing
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
License
MIT © Shahrad Elahi and contributors.