Skip to content

Commit a76b527

Browse files
committed
doc: update README.md
1 parent b6fed5f commit a76b527

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

Diff for: .vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"editor.formatOnSave": true,
66
"editor.defaultFormatter": "denoland.vscode-deno",
77
"cSpell.words": [
8+
"deno",
89
"iwset",
910
"weakref",
1011
"wmap",

Diff for: README.md

+123
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,126 @@
1212
<a href="https://www.npmjs.com/package/weakref"><img alt="Version" src="https://img.shields.io/npm/v/weakref.svg?style=flat-square&logo=npm" /></a>
1313
<a href="https://npmcharts.com/compare/weakref?minimal=true"><img alt="Downloads" src="https://img.shields.io/npm/dt/weakref.svg?style=flat-square" /></a>
1414
</p>
15+
16+
Weak Collection Library for Deno and Node.js.
17+
18+
## Usage
19+
20+
### with Deno
21+
22+
```ts
23+
import {
24+
IterableWeakMap,
25+
IterableWeakSet,
26+
} from "https://deno.land/x/weakref/mod.ts";
27+
28+
const set = new IterableWeakSet();
29+
const map = new IterableWeakMap();
30+
```
31+
32+
### with Node.js & Browser
33+
34+
**Install**
35+
36+
```bash
37+
npm install weakref
38+
```
39+
40+
```ts
41+
import { IterableWeakMap, IterableWeakSet } from "weakref";
42+
```
43+
44+
## Features
45+
46+
### IterableWeakSet
47+
48+
**Interface**
49+
50+
```ts
51+
class IterableWeakSet<T extends object> implements WeakSet<T>, Set<T> {
52+
constructor(values?: readonly T[] | null);
53+
constructor(iterable: Iterable<T>);
54+
}
55+
```
56+
57+
**Example**
58+
59+
```ts
60+
const set = new IterableWeakSet();
61+
62+
for (let i = 0; i < 100; i++) {
63+
set.add({});
64+
}
65+
66+
for (const item of set) {
67+
console.log(item); // will print 100 items
68+
}
69+
70+
// after garbage collection, {n} items will be collected
71+
72+
for (const item of set) {
73+
console.log(item); // will print (100 - {n}) items
74+
}
75+
```
76+
77+
### IterableWeakMap
78+
79+
**Interface**
80+
81+
```ts
82+
class IterableWeakMap<K extends object, V> implements WeakMap<K, V>, Map<K, V> {
83+
constructor(entries?: readonly (readonly [K, V])[] | null);
84+
constructor(iterable: Iterable<readonly [K, V]>);
85+
}
86+
```
87+
88+
**Example**
89+
90+
```ts
91+
const map = new IterableWeakMap();
92+
93+
for (let i = 0; i < 100; i++) {
94+
map.set({}, i);
95+
}
96+
97+
for (const [key, value] of map) {
98+
console.log(key, value); // will print 100 items
99+
}
100+
101+
// after garbage collection, {n} items will be collected
102+
103+
for (const [key, value] of map) {
104+
console.log(key, value); // will print (100 - {n}) items
105+
}
106+
```
107+
108+
### InvertedWeakMap
109+
110+
**Interface**
111+
112+
```ts
113+
class InvertedWeakMap<K, V extends object> implements Map<K, V> {
114+
constructor(entries?: readonly (readonly [K, V])[] | null);
115+
constructor(iterable: Iterable<readonly [K, V]>);
116+
}
117+
```
118+
119+
**Example**
120+
121+
```ts
122+
const map = new InvertedWeakMap();
123+
124+
for (let i = 0; i < 100; i++) {
125+
map.set(i, {});
126+
}
127+
128+
for (const [key, value] of map) {
129+
console.log(key, value); // will print 100 items
130+
}
131+
132+
// after garbage collection, {n} items will be collected
133+
134+
for (const [key, value] of map) {
135+
console.log(key, value); // will print (100 - {n}) items
136+
}
137+
```

Diff for: scripts/build_npm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ await build({
2525
name: "weakref",
2626
version,
2727
description:
28-
"weakref is a library for weak references. It provides IterableWeakSet, and IterableWeakMap",
28+
"Weak Collection Library for Deno and Node.js. It provides IterableWeakSet, IterableWeakMap, and InvertedWeakMap.",
2929
keywords: [
3030
"weakref",
3131
"weakset",

0 commit comments

Comments
 (0)