@thi.ng/bidir-index 1.4.2 → 1.5.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 213 standalone projects, maintained as part
10
+ > This is one of 214 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,7 +67,7 @@ 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: 999 bytes
70
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.04 KB
71
71
 
72
72
  ## Dependencies
73
73
 
package/bidir-index.d.ts CHANGED
@@ -106,10 +106,17 @@ export declare class BidirIndex<T> implements IClear, ICopy<BidirIndex<T>>, IEmp
106
106
  * false), throws error if any of the given keys is unknown/unindexed (use
107
107
  * {@link BidirIndex.add} or {@link BidirIndex.addAll} first).
108
108
  *
109
+ * @remarks
110
+ * Only used if `fail=false` (default): If `all=true`, any unknown values
111
+ * will be mapped to `null` values in the result array. Otherwise, the
112
+ * default behavior is for such unknown values to be skipped.
113
+ *
109
114
  * @param keys
110
115
  * @param fail
116
+ * @param all
111
117
  */
112
118
  getAll(keys: Iterable<T>, fail?: boolean): number[];
119
+ getAll(keys: Iterable<T>, fail: boolean, all: true): (number | null)[];
113
120
  /**
114
121
  * Similar to {@link BidirIndex.getAll}, but returns result as ES6 Set,
115
122
  * thereby removing any duplicates in `keys`.
@@ -119,15 +126,31 @@ export declare class BidirIndex<T> implements IClear, ICopy<BidirIndex<T>>, IEmp
119
126
  */
120
127
  getAllUnique(keys: Iterable<T>, fail?: boolean): Set<number>;
121
128
  /**
122
- * Returns array of matching keys for all given IDs. If `fail` is true
123
- * (default: false), throws error if any of the given IDs is
124
- * unknown/unindexed (use {@link BidirIndex.add} or
129
+ * Reverse op of {@link BidirIndex.getAll}. Returns array of matching keys
130
+ * for all given IDs. If `fail` is true (default: false), throws error if
131
+ * any of the given IDs is unknown/unindexed (use {@link BidirIndex.add} or
125
132
  * {@link BidirIndex.addAll} first).
126
133
  *
134
+ * @remarks
135
+ * Only used if `fail=false` (default): If `all=true`, any unknown IDs will
136
+ * be mapped to `null` values in the result array. Otherwise, the default
137
+ * behavior is for such unknown values to be skipped.
138
+ *
127
139
  * @param ids
128
140
  * @param fail
141
+ * @param all
129
142
  */
130
143
  getAllIDs(ids: Iterable<number>, fail?: boolean): T[];
144
+ getAllIDs(ids: Iterable<number>, fail: boolean, all: true): (T | null)[];
145
+ /**
146
+ * Attempts to rename `currKey` into `newKey` without changing its ID
147
+ * mapping. Returns `ok` if successful, otherwise `missing` if `currKey` is
148
+ * unknown or `conflict` if `newKey` already exists.
149
+ *
150
+ * @param currKey
151
+ * @param newKey
152
+ */
153
+ renameKey(currKey: T, newKey: T): "missing" | "conflict" | "ok";
131
154
  /**
132
155
  * Returns a compact JSON serializable version of the index. Use
133
156
  * {@link bidirIndexFromJSON} to instantiate an index from such a JSON
package/bidir-index.js CHANGED
@@ -164,16 +164,8 @@ class BidirIndex {
164
164
  deleteAllIDs(ids) {
165
165
  for (const id of ids) this.deleteID(id);
166
166
  }
167
- /**
168
- * Returns array of IDs for all given keys. If `fail` is true (default:
169
- * false), throws error if any of the given keys is unknown/unindexed (use
170
- * {@link BidirIndex.add} or {@link BidirIndex.addAll} first).
171
- *
172
- * @param keys
173
- * @param fail
174
- */
175
- getAll(keys, fail = false) {
176
- return __iterate(this.fwd, keys, fail);
167
+ getAll(keys, fail = false, all) {
168
+ return __iterate(this.fwd, keys, fail, all);
177
169
  }
178
170
  /**
179
171
  * Similar to {@link BidirIndex.getAll}, but returns result as ES6 Set,
@@ -185,17 +177,26 @@ class BidirIndex {
185
177
  getAllUnique(keys, fail = false) {
186
178
  return new Set(__iterate(this.fwd, keys, fail));
187
179
  }
180
+ getAllIDs(ids, fail = false, all) {
181
+ return __iterate(this.rev, ids, fail, all);
182
+ }
188
183
  /**
189
- * Returns array of matching keys for all given IDs. If `fail` is true
190
- * (default: false), throws error if any of the given IDs is
191
- * unknown/unindexed (use {@link BidirIndex.add} or
192
- * {@link BidirIndex.addAll} first).
184
+ * Attempts to rename `currKey` into `newKey` without changing its ID
185
+ * mapping. Returns `ok` if successful, otherwise `missing` if `currKey` is
186
+ * unknown or `conflict` if `newKey` already exists.
193
187
  *
194
- * @param ids
195
- * @param fail
188
+ * @param currKey
189
+ * @param newKey
196
190
  */
197
- getAllIDs(ids, fail = false) {
198
- return __iterate(this.rev, ids, fail);
191
+ renameKey(currKey, newKey) {
192
+ const id1 = this.fwd.get(currKey);
193
+ if (id1 == null) return "missing";
194
+ const id2 = this.fwd.get(newKey);
195
+ if (id2 != null) return "conflict";
196
+ this.fwd.delete(currKey);
197
+ this.fwd.set(newKey, id1);
198
+ this.rev.set(id1, newKey);
199
+ return "ok";
199
200
  }
200
201
  /**
201
202
  * Returns a compact JSON serializable version of the index. Use
@@ -218,18 +219,19 @@ const __delete = (fwd, rev, key) => {
218
219
  }
219
220
  return false;
220
221
  };
221
- const __iterate = (index, keys, fail) => {
222
+ function __iterate(index, keys, fail, all) {
222
223
  const res = [];
223
224
  for (const k of keys) {
224
225
  const val = index.get(k);
225
226
  if (val === void 0) {
226
227
  if (fail) throw new Error(`unknwon key/ID: ${k}`);
228
+ if (all) res.push(null);
227
229
  } else {
228
230
  res.push(val);
229
231
  }
230
232
  }
231
233
  return res;
232
- };
234
+ }
233
235
  const defBidirIndex = (keys, opts) => new BidirIndex(keys, opts);
234
236
  const bidirIndexFromJSON = (src, map) => {
235
237
  const $src = typeof src === "string" ? JSON.parse(src) : src;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/bidir-index",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
4
4
  "description": "Bi-directional index mapping arbitrary keys to numeric IDs & vice versa",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -8,7 +8,8 @@
8
8
  "sideEffects": false,
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/thi-ng/umbrella.git"
11
+ "url": "git+https://github.com/thi-ng/umbrella.git",
12
+ "directory": "packages/bidir-index"
12
13
  },
13
14
  "homepage": "https://thi.ng/bidir-index",
14
15
  "funding": [
@@ -39,12 +40,12 @@
39
40
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
40
41
  },
41
42
  "dependencies": {
42
- "@thi.ng/api": "^8.12.12",
43
- "@thi.ng/checks": "^3.8.2"
43
+ "@thi.ng/api": "^8.12.14",
44
+ "@thi.ng/checks": "^3.8.4"
44
45
  },
45
46
  "devDependencies": {
46
- "esbuild": "^0.27.0",
47
- "typedoc": "^0.28.14",
47
+ "esbuild": "^0.27.2",
48
+ "typedoc": "^0.28.16",
48
49
  "typescript": "^5.9.3"
49
50
  },
50
51
  "keywords": [
@@ -94,5 +95,5 @@
94
95
  ],
95
96
  "year": 2022
96
97
  },
97
- "gitHead": "deb511294f7a120091b654af0ff7e8a399a465b3\n"
98
+ "gitHead": "4bd1b9d8ae52ba32b90a1b9a55d329c708ca7865\n"
98
99
  }