@rhombus-toolkit/collections 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,22 @@
1
+ /**
2
+ * A stack whose entries leave when their scope does.
3
+ *
4
+ * @remarks
5
+ * Iterates outermost first.
6
+ */
7
+ declare class AutoStack<T> implements Iterable<T> {
8
+ #private;
9
+ /** How many entries the stack holds. */
10
+ get length(): number;
11
+ /** Adds `item`; disposing the return truncates back to the pre-push depth, so an outer scope's dispose also drops what an inner one left. */
12
+ push(item: T): Disposable;
13
+ /** Whether `item` sits anywhere in the stack, not only at the top. */
14
+ has(item: T): boolean;
15
+ /** The innermost entry, absent where the stack holds nothing. */
16
+ peek(): T | undefined;
17
+ [Symbol.iterator](): ArrayIterator<T>;
18
+ }
19
+
1
20
  /**
2
21
  * A persistent singly-linked list — extending shares everything already there.
3
22
  *
@@ -46,4 +65,4 @@ declare class KindaWeakMap<K, V extends WeakKey> {
46
65
  readonly [Symbol.toStringTag]: "KindaWeakMap";
47
66
  }
48
67
 
49
- export { ImmutableLinkedList, KindaWeakMap };
68
+ export { AutoStack, ImmutableLinkedList, KindaWeakMap };
@@ -1,3 +1,26 @@
1
+ // src/AutoStack.ts
2
+ class AutoStack {
3
+ #items = [];
4
+ get length() {
5
+ return this.#items.length;
6
+ }
7
+ push(item) {
8
+ const pos = this.#items.length;
9
+ this.#items.push(item);
10
+ return { [Symbol.dispose]: () => {
11
+ this.#items.length = pos;
12
+ } };
13
+ }
14
+ has(item) {
15
+ return this.#items.includes(item);
16
+ }
17
+ peek() {
18
+ return this.#items.at(-1);
19
+ }
20
+ [Symbol.iterator]() {
21
+ return this.#items.values();
22
+ }
23
+ }
1
24
  // src/ImmutableLinkedList.ts
2
25
  class ImmutableLinkedList {
3
26
  static #nothing = new ImmutableLinkedList(undefined, undefined, 0);
@@ -118,6 +141,7 @@ class KindaWeakMap {
118
141
  [Symbol.toStringTag] = "KindaWeakMap";
119
142
  }
120
143
  export {
144
+ AutoStack,
121
145
  ImmutableLinkedList,
122
146
  KindaWeakMap
123
147
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rhombus-toolkit/collections",
3
- "version": "2.0.2",
4
- "description": "Collection data structures: ImmutableLinkedList (persistent, both ends known), KindaWeakMap (strong keys, weak values).",
3
+ "version": "2.1.0",
4
+ "description": "Collection data structures: AutoStack (entries leave with their scope), ImmutableLinkedList (persistent, both ends known), KindaWeakMap (strong keys, weak values).",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/rhombus-toolkit/ts.git",