Skip to content

Commit d329b49

Browse files
committed
refactor: InvertedweakMap -> WeakValueMap
1 parent 05d6408 commit d329b49

File tree

5 files changed

+31
-30
lines changed

5 files changed

+31
-30
lines changed

README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</p>
1515

1616
This library provides three iterable weak data structures for JavaScript,
17-
IterableWeakSet, IterableWeakMap, and InvertedWeakMap. These data structures are
17+
IterableWeakSet, IterableWeakMap, and WeakValueMap. These data structures are
1818
designed to work with objects as keys or values, and are useful when you need to
1919
store a collection of objects that may be garbage collected.
2020

@@ -24,14 +24,15 @@ store a collection of objects that may be garbage collected.
2424

2525
```ts
2626
import {
27-
InvertedWeakMap,
2827
IterableWeakMap,
2928
IterableWeakSet,
29+
WeakValueMap,
3030
} from "https://deno.land/x/weakref/mod.ts";
3131

3232
const set = new IterableWeakSet();
3333
const map = new IterableWeakMap();
34-
const invertedMap = new InvertedWeakMap();
34+
35+
const weakValueMap = new WeakValueMap();
3536
```
3637

3738
### with Node.js & Browser
@@ -123,17 +124,17 @@ if (global.gc) {
123124
console.log(map.size); // output: 0
124125
```
125126

126-
### InvertedWeakMap
127+
### WeakValueMap
127128

128-
InvertedWeakMap is a class that allows you to create a map of non-object keys
129-
with weak references to object values. This is useful when you have a collection
130-
of non-object keys that you want to use to look up objects, and those objects
131-
may be garbage collected.
129+
WeakValueMap is a class that allows you to create a map of non-object keys with
130+
weak references to object values. This is useful when you have a collection of
131+
non-object keys that you want to use to look up objects, and those objects may
132+
be garbage collected.
132133

133134
**Interface**
134135

135136
```ts
136-
class InvertedWeakMap<K, V extends object> implements Map<K, V> {
137+
class WeakValueMap<K, V extends object> implements Map<K, V> {
137138
constructor(entries?: readonly (readonly [K, V])[] | null);
138139
constructor(iterable: Iterable<readonly [K, V]>);
139140
}
@@ -142,7 +143,7 @@ class InvertedWeakMap<K, V extends object> implements Map<K, V> {
142143
**Example**
143144

144145
```ts
145-
const map = new InvertedWeakMap();
146+
const map = new WeakValueMap();
146147

147148
// create an object with a weak reference
148149
{

mod.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { IterableWeakSet } from "./iterable_weak_set.ts";
22
export { IterableWeakMap } from "./iterable_weak_map.ts";
33

4-
export { InvertedWeakMap } from "./inverted_weak_map.ts";
4+
export { WeakValueMap } from "./weak_value_map.ts";

scripts/build_npm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ await build({
2828
name: "weakref",
2929
version,
3030
description:
31-
"Extend built-in collections with weak references for efficient garbage collection and optimal performance in memory-intensive applications with IterableWeakSet, IterableWeakMap, and InvertedWeakMap.",
31+
"Extend built-in collections with weak references for efficient garbage collection and optimal performance in memory-intensive applications with IterableWeakSet, IterableWeakMap, and WeakValueMap.",
3232
keywords: [
3333
"weakref",
3434
"weakset",

inverted_weak_map.test.ts weak_value_map.test.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { assert, assertEquals, assertFalse } from "testing/asserts.ts";
2-
import { InvertedWeakMap } from "./inverted_weak_map.ts";
2+
import { WeakValueMap } from "./weak_value_map.ts";
33

44
function* iterate<T>(items: T[]): IterableIterator<T> {
55
for (const item of items) {
66
yield item;
77
}
88
}
99

10-
Deno.test("InvertedWeakMap, constructor", () => {
11-
assertEquals(new InvertedWeakMap().size, 0);
12-
assertEquals(new InvertedWeakMap(undefined).size, 0);
13-
assertEquals(new InvertedWeakMap(null).size, 0);
10+
Deno.test("WeakValueMap, constructor", () => {
11+
assertEquals(new WeakValueMap().size, 0);
12+
assertEquals(new WeakValueMap(undefined).size, 0);
13+
assertEquals(new WeakValueMap(null).size, 0);
1414

15-
assertEquals(new InvertedWeakMap([]).size, 0);
16-
assertEquals(new InvertedWeakMap(iterate([])).size, 0);
15+
assertEquals(new WeakValueMap([]).size, 0);
16+
assertEquals(new WeakValueMap(iterate([])).size, 0);
1717

18-
assertEquals(new InvertedWeakMap([[1, {}]]).size, 1);
19-
assertEquals(new InvertedWeakMap(iterate([[1, {}] as const])).size, 1);
18+
assertEquals(new WeakValueMap([[1, {}]]).size, 1);
19+
assertEquals(new WeakValueMap(iterate([[1, {}] as const])).size, 1);
2020
});
2121

22-
Deno.test("InvertedWeakMap, comparison Map, WeakMap", () => {
22+
Deno.test("WeakValueMap, comparison Map, WeakMap", () => {
2323
const map = new Map();
24-
const iwmap = new InvertedWeakMap();
24+
const iwmap = new WeakValueMap();
2525

2626
assertEquals(map.size, 0);
2727
assertEquals(iwmap.size, 0);
@@ -86,10 +86,10 @@ Deno.test("InvertedWeakMap, comparison Map, WeakMap", () => {
8686
assertEquals(iwmap.size, 0);
8787

8888
assertEquals(map.toString(), "[object Map]");
89-
assertEquals(iwmap.toString(), "[object InvertedWeakMap]");
89+
assertEquals(iwmap.toString(), "[object WeakValueMap]");
9090
});
9191

92-
Deno.test("InvertedWeakMap, iterable", () => {
92+
Deno.test("WeakValueMap, iterable", () => {
9393
const tuples: [number | string | boolean, Record<string, unknown>][] = [
9494
[1, {}],
9595
["2", {}],
@@ -101,7 +101,7 @@ Deno.test("InvertedWeakMap, iterable", () => {
101101

102102
const maps = [
103103
new Map(tuples),
104-
new InvertedWeakMap(tuples),
104+
new WeakValueMap(tuples),
105105
];
106106

107107
for (const map of maps) {
@@ -162,14 +162,14 @@ Deno.test("InvertedWeakMap, iterable", () => {
162162
}
163163
});
164164

165-
Deno.test("InvertedWeakMap, garbage collect", async () => {
165+
Deno.test("WeakValueMap, garbage collect", async () => {
166166
let removedCount = 0;
167167
let insertedCount = 0;
168168
const register = new FinalizationRegistry(() => {
169169
removedCount++;
170170
});
171171

172-
const map = new InvertedWeakMap();
172+
const map = new WeakValueMap();
173173
for (let i = 0; removedCount < 100; i++) {
174174
await new Promise((resolve) => setTimeout(resolve, 16));
175175
const data = {};

inverted_weak_map.ts weak_value_map.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// deno-lint-ignore-file ban-types
22

3-
export class InvertedWeakMap<K, V extends object> implements Map<K, V> {
3+
export class WeakValueMap<K, V extends object> implements Map<K, V> {
44
#map = new Map<K, WeakRef<V>>();
55
#registry = new FinalizationRegistry<K>(this.#map.delete.bind(this.#map));
66

@@ -48,7 +48,7 @@ export class InvertedWeakMap<K, V extends object> implements Map<K, V> {
4848
}
4949

5050
get [Symbol.toStringTag]() {
51-
return "InvertedWeakMap";
51+
return "WeakValueMap";
5252
}
5353

5454
forEach(

0 commit comments

Comments
 (0)