Skip to content

Commit

Permalink
feat: upgrade to new the MapIterator type
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Jan 12, 2025
1 parent fc0b2a2 commit 44ac988
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
7 changes: 2 additions & 5 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"name": "@vicary/multidict",
"description": "MultiDict is an extension of Map that supports bidirectioal, many to many relations among multiple keys and values.",
"version": "1.0.9",
"version": "1.0.10",
"exports": "./mod.ts",
"lock": false,
"nodeModulesDir": false,
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.1"
}
"nodeModulesDir": "none"
}
2 changes: 1 addition & 1 deletion dnt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Bundle src/mod.ts into both ESM and CJS format.
import { build } from "@deno/dnt";
import { build } from "jsr:@deno/dnt@^0.41.1";
import pkg from "./deno.json" with { type: "json" };

await Deno.remove("./dnt", { recursive: true }).catch(() => {});
Expand Down
23 changes: 12 additions & 11 deletions src/MultiDict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
*
* - Deleting a key deletes it from both directions.
*/
export class MultiDict<K, V> implements Omit<Map<K | V, Set<K | V>>, "set"> {
// This type doesn't work: `Map<K, Set<V>> | Map<V, Set<K>>`
export class MultiDict<K, V>
implements Omit<Map<K | V, ReadonlySet<K | V>>, "set"> {
// This type doesn't work: `Map<K, ReadonlySet<V>> | Map<V, ReadonlySet<K>>`
#map = new Map<K | V, Set<K | V>>();

/** Delete all entries */
Expand Down Expand Up @@ -55,7 +56,7 @@ export class MultiDict<K, V> implements Omit<Map<K | V, Set<K | V>>, "set"> {
* Keys and values are interchangeable, expect all existing keys and values
* being invoked as the "key" once.
*/
entries(): IterableIterator<[K | V, Set<K | V>]> {
entries(): MapIterator<[K | V, ReadonlySet<K | V>]> {
return this.#map.entries();
}

Expand All @@ -67,9 +68,9 @@ export class MultiDict<K, V> implements Omit<Map<K | V, Set<K | V>>, "set"> {
*/
forEach(
callbackfn: (
value: Set<K | V>,
value: ReadonlySet<K | V>,
key: K | V,
map: Map<K | V, Set<K | V>>,
map: Map<K | V, ReadonlySet<K | V>>,
) => void,
thisArg?: unknown,
): void {
Expand All @@ -79,10 +80,10 @@ export class MultiDict<K, V> implements Omit<Map<K | V, Set<K | V>>, "set"> {
/**
* Returns a Set of values for the specified key.
*/
get(key: K): Set<V> | undefined;
get(key: V): Set<K> | undefined;
get(key: K): ReadonlySet<V> | undefined;
get(key: V): ReadonlySet<K> | undefined;
get(key: K | V) {
return this.#map.get(key);
return this.#map.get(key) as ReadonlySet<K | V> | undefined;
}

/**
Expand All @@ -96,7 +97,7 @@ export class MultiDict<K, V> implements Omit<Map<K | V, Set<K | V>>, "set"> {
* Keys and values are interchangable, this is an iterator for all keys and
* values.
*/
keys(): IterableIterator<K | V> {
keys(): MapIterator<K | V> {
return this.#map.keys();
}

Expand Down Expand Up @@ -132,11 +133,11 @@ export class MultiDict<K, V> implements Omit<Map<K | V, Set<K | V>>, "set"> {
* Keys and values are interchangeable, therefore values set as keys will be
* returned as a single-value Set.
*/
values(): IterableIterator<Set<K | V>> {
values(): MapIterator<ReadonlySet<K | V>> {
return this.#map.values();
}

[Symbol.iterator](): IterableIterator<[K | V, Set<K | V>]> {
[Symbol.iterator](): MapIterator<[K | V, ReadonlySet<K | V>]> {
return this.#map[Symbol.iterator]();
}

Expand Down

0 comments on commit 44ac988

Please sign in to comment.