@thi.ng/bidir-index 1.3.13 → 1.4.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.
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 210 standalone projects, maintained as part
10
+ > This is one of 211 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -67,11 +67,14 @@ For Node.js REPL:
67
67
  const bi = await import("@thi.ng/bidir-index");
68
68
  ```
69
69
 
70
- Package sizes (brotli'd, pre-treeshake): ESM: 884 bytes
70
+ Package sizes (brotli'd, pre-treeshake): ESM: 999 bytes
71
71
 
72
72
  ## Dependencies
73
73
 
74
- None
74
+ - [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
75
+ - [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
76
+
77
+ Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)
75
78
 
76
79
  ## API
77
80
 
package/bidir-index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import type { IClear, ICopy, IEmpty } from "@thi.ng/api";
1
2
  import type { BidirIndexOpts, SerializedBidirIndex } from "./api.js";
2
3
  /**
3
4
  * Bi-directional index to map arbitrary keys to numeric IDs and vice versa.
4
5
  */
5
- export declare class BidirIndex<T> {
6
+ export declare class BidirIndex<T> implements IClear, ICopy<BidirIndex<T>>, IEmpty<BidirIndex<T>> {
6
7
  fwd: Map<T, number>;
7
8
  rev: Map<number, T>;
8
9
  nextID: number;
@@ -12,6 +13,9 @@ export declare class BidirIndex<T> {
12
13
  * Yields same result as {@link BidirIndex.entries}.
13
14
  */
14
15
  [Symbol.iterator](): MapIterator<[T, number]>;
16
+ clear(): void;
17
+ copy(): BidirIndex<T>;
18
+ empty(): BidirIndex<T>;
15
19
  /**
16
20
  * Returns iterator of `[key,id]` pairs.
17
21
  */
package/bidir-index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { implementsFunction } from "@thi.ng/checks/implements-function";
1
2
  class BidirIndex {
2
3
  fwd;
3
4
  rev;
@@ -17,6 +18,25 @@ class BidirIndex {
17
18
  [Symbol.iterator]() {
18
19
  return this.entries();
19
20
  }
21
+ clear() {
22
+ this.fwd.clear();
23
+ this.rev.clear();
24
+ this.nextID = 0;
25
+ }
26
+ copy() {
27
+ const result = new BidirIndex();
28
+ result.fwd = implementsFunction(this.fwd, "copy") ? this.fwd.copy() : new Map(this.fwd);
29
+ result.rev = new Map(this.rev);
30
+ result.nextID = this.nextID;
31
+ return result;
32
+ }
33
+ empty() {
34
+ const result = new BidirIndex();
35
+ if (implementsFunction(this.fwd, "empty")) {
36
+ result.fwd = this.fwd.empty();
37
+ }
38
+ return result;
39
+ }
20
40
  /**
21
41
  * Returns iterator of `[key,id]` pairs.
22
42
  */
@@ -92,7 +112,7 @@ class BidirIndex {
92
112
  */
93
113
  addAll(keys) {
94
114
  const res = [];
95
- for (let k of keys) {
115
+ for (const k of keys) {
96
116
  res.push(this.add(k));
97
117
  }
98
118
  return res;
@@ -105,7 +125,7 @@ class BidirIndex {
105
125
  */
106
126
  addAllUnique(keys) {
107
127
  const res = /* @__PURE__ */ new Set();
108
- for (let k of keys) {
128
+ for (const k of keys) {
109
129
  res.add(this.add(k));
110
130
  }
111
131
  return res;
@@ -134,7 +154,7 @@ class BidirIndex {
134
154
  * @param keys
135
155
  */
136
156
  deleteAll(keys) {
137
- for (let k of keys) this.delete(k);
157
+ for (const k of keys) this.delete(k);
138
158
  }
139
159
  /**
140
160
  * Batch version of {@link BidirIndex.deleteID}.
@@ -142,7 +162,7 @@ class BidirIndex {
142
162
  * @param ids
143
163
  */
144
164
  deleteAllIDs(ids) {
145
- for (let id of ids) this.deleteID(id);
165
+ for (const id of ids) this.deleteID(id);
146
166
  }
147
167
  /**
148
168
  * Returns array of IDs for all given keys. If `fail` is true (default:
@@ -200,7 +220,7 @@ const __delete = (fwd, rev, key) => {
200
220
  };
201
221
  const __iterate = (index, keys, fail) => {
202
222
  const res = [];
203
- for (let k of keys) {
223
+ for (const k of keys) {
204
224
  const val = index.get(k);
205
225
  if (val === void 0) {
206
226
  if (fail) throw new Error(`unknwon key/ID: ${k}`);
package/encode.js CHANGED
@@ -2,7 +2,7 @@ const encodeObject = (index, obj, defaultValue, indexKeys = true) => {
2
2
  const keys = Object.keys(obj);
3
3
  const ids = indexKeys ? index.addAll(keys) : index.getAll(keys);
4
4
  const res = new Array(index.size).fill(defaultValue);
5
- for (let id of ids) {
5
+ for (const id of ids) {
6
6
  const val = obj[index.getID(id)];
7
7
  if (val != null) res[id] = val;
8
8
  }
@@ -10,15 +10,15 @@ const encodeObject = (index, obj, defaultValue, indexKeys = true) => {
10
10
  };
11
11
  const encodeObjectIterator = function* (index, objects, defaultValue, indexKeys = true) {
12
12
  if (indexKeys) {
13
- for (let o of objects) index.addAll(Object.keys(o));
13
+ for (const o of objects) index.addAll(Object.keys(o));
14
14
  }
15
- for (let o of objects) {
15
+ for (const o of objects) {
16
16
  yield* encodeObject(index, o, defaultValue, false);
17
17
  }
18
18
  };
19
19
  const decodeObject = (index, values, defaults) => {
20
20
  const res = {};
21
- for (let [k, id] of index) {
21
+ for (const [k, id] of index) {
22
22
  const val = values[id] ?? defaults?.[k];
23
23
  if (val != null) res[k] = val;
24
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/bidir-index",
3
- "version": "1.3.13",
3
+ "version": "1.4.0",
4
4
  "description": "Bi-directional index mapping arbitrary keys to numeric IDs & vice versa",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -38,6 +38,10 @@
38
38
  "test": "bun test",
39
39
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
40
40
  },
41
+ "dependencies": {
42
+ "@thi.ng/api": "^8.12.10",
43
+ "@thi.ng/checks": "^3.8.0"
44
+ },
41
45
  "devDependencies": {
42
46
  "esbuild": "^0.27.0",
43
47
  "typedoc": "^0.28.14",
@@ -53,6 +57,7 @@
53
57
  "iterator",
54
58
  "keys",
55
59
  "map",
60
+ "number",
56
61
  "typescript"
57
62
  ],
58
63
  "publishConfig": {
@@ -89,5 +94,5 @@
89
94
  ],
90
95
  "year": 2022
91
96
  },
92
- "gitHead": "be6e7657b1e5c54d7d648d1b52888a7297e95a17\n"
97
+ "gitHead": "824bf9047b5a10f777c5c5b4aeecf0c750a22c75\n"
93
98
  }