# weak-ref-collections

> Iterable WeakMaps and WeakSets. Provides WeakRefMap and WeakRefSet which store values using WeakRefs and clean themselves up when garbage collected.

Latest version **1.2.4** (published 2022-11-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install weak-ref-collections
pnpm add weak-ref-collections
yarn add weak-ref-collections
bun add weak-ref-collections
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2022-11-01 |
| First published | 2022-10-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Li Hongyi |
| Maintainers | fynyky |
| Keywords | weakref, weakmap, weakset, weak, iterable, enumerable |

## Links

- npm: https://www.npmjs.com/package/weak-ref-collections
- Repository: https://github.com/fynyky/weak-ref-collections
- Homepage: https://github.com/fynyky/weak-ref-collections#readme
- Issues: https://github.com/fynyky/weak-ref-collections/issues
- npm.io page: https://npm.io/package/weak-ref-collections

## Recent versions

- 1.2.4 (latest) — 2022-11-01
- 1.2.3 — 2022-11-01
- 1.2.2 — 2022-11-01
- 1.2.1 — 2022-10-31
- 1.2.0 — 2022-10-31
- 1.1.0 — 2022-10-31
- 1.0.0 — 2022-10-31

## README

# Weak Ref Collections
Iterable WeakMaps and WeakSets. Provides WeakRefMap and WeakRefSet which store object values using WeakRefs and clean themselves up when garbage collected. Supports both objects and primitives simultaneously. Behaves like normal Map and Set for primitives.

## Installation
```bash
npm install weak-ref-collections
```

## Usage
Unlike WeakMap which stores keys weakly, WeakRefMap stores keys strongly but stores values using WeakRefs. Is fully iterable. Works just like a normal Map object but holds its values weakly and cleans itself up when its values are garbage collected. Follows the [Map API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)

```javascript
const { WeakRefMap } = require("weak-ref-collections");

weakRefMap = new WeakRefMap();
weakRefMap.set("foo", {"bar": 1});
weakRefMap.get("foo");    // {"bar":1}
weakRefMap.has("foo");    // true
weakRefMap.delete("foo"); // true
weakRefMap.clear();       // undefined
weakRefMap.forEach((value, key, map) => {});
// Supports iteration
for (const [key, value] of weakRefMap) {}
for (const [key, value] of weakRefMap.entries()) {}
for (const key of weakRefMap.keys()) {}
for (const value of weakRefMap.values()) {}
```

Similarly for WeakRefSet is fully iterable unlike WeakSet. Works just like a normal Set object but holds its values weakly and cleans itself up when its values are garbage collected. Follows the [Set API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
```javascript
const { WeakRefSet } = require("weak-ref-set");

weakRefSet = new WeakRefSet();
const element = {"bar": 1};
weakRefSet.add(element);
weakRefSet.has(element);    // true
weakRefSet.delete(element); // true
weakRefSet.clear();         // undefined
weakRefSet.forEach((value, alsoValue, set) => {});
// Supports iteration
for (const [value, alsoValue] of weakRefSet) {}
for (const [value, alsoValue] of weakRefSet.entries()) {}
for (const value of weakRefSet.keys()) {}
for (const value of weakRefSet.values()) {}
```

---
_Source: https://npm.io/package/weak-ref-collections · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
