From 3e397b3f3d982fcb539a7dd434724409b983b3e2 Mon Sep 17 00:00:00 2001
From: "Lucian I. Last"
Date: Tue, 12 Sep 2023 22:46:09 +0200
Subject: [PATCH] Create qPool.ts
A First attempt at moving to typescript
---
src/qPool.ts | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 96 insertions(+)
create mode 100644 src/qPool.ts
diff --git a/src/qPool.ts b/src/qPool.ts
new file mode 100644
index 0000000..752f02d
--- /dev/null
+++ b/src/qPool.ts
@@ -0,0 +1,96 @@
+/* eslint func-names: ["error", "never"] */
+
+const isF = (x) => typeof x === "function";
+
+const len = (x) => x.toString().length;
+
+export interface Options {
+ init?: string;
+ maxIn?: number;
+}
+
+export default class QPool {
+ opts: Options;
+ pool: string;
+ maxIn: number;
+ size: number[];
+ constructor(opt: Options) {
+ this.opts = opt || {};
+ this.pool = this.opts.init || "";
+ this.maxIn = this.opts.maxIn || 2;
+ this.size = [];
+ }
+
+ // returns pool
+ get() {
+ return this.pool;
+ }
+
+ // returns pool length
+ length() {
+ return len(this.pool);
+ }
+
+ // returns string sizes
+ elementsSize() {
+ return this.size;
+ }
+
+ // returns string sizes
+ elementsLength() {
+ return this.size.length;
+ }
+
+ flush() {
+ this.pool = "";
+ this.size = [];
+ }
+
+ // enqueue, to last.
+ push(chunk) {
+ // add chunk
+ this.pool += chunk;
+ // add chunk size
+ this.size.push(len(chunk));
+ }
+
+ // enqueue, to first.
+ unshift(chunk) {
+ // add chunk
+ this.pool = chunk + this.pool;
+ // add chunk size
+ this.size.unshift(len(chunk));
+ }
+
+ // dequeue, first
+ shift() {
+ // add chunk
+ this.pool = this.pool.slice(this.size[0], this.length());
+ // add chunk size
+ this.size.shift();
+ }
+
+ // dequeue, last
+ pop() {
+ // add chunk
+ this.pool = this.pool.slice(
+ 0,
+ this.length() - this.size[this.elementsLength() - 1]
+ );
+ // add chunk size
+ this.size.pop();
+ }
+
+ // clear over size elemets either shift or pop.
+ clr(func) {
+ while (this.elementsLength() >= this.maxIn) func.apply(this);
+ }
+
+ process(chunk, type, cb) {
+ if (type === "stack") this.clr(this.pop);
+ else this.clr(this.shift);
+ this.push(chunk);
+ if (isF(cb)) cb(this.get());
+ else if (isF(type)) type(this.get());
+ }
+}