# time-ordered-set

> Efficiently maintain a set of nodes ordered by the time they were added to the set

Latest version **2.0.1** (published 2024-10-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install time-ordered-set
pnpm add time-ordered-set
yarn add time-ordered-set
bun add time-ordered-set
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2024-10-08 |
| First published | 2017-01-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/time-ordered-set
- Repository: https://github.com/mafintosh/time-ordered-set
- Issues: https://github.com/mafintosh/time-ordered-set/issues
- npm.io page: https://npm.io/package/time-ordered-set

## Recent versions

- 2.0.1 (latest) — 2024-10-08
- 2.0.0 — 2024-09-23
- 1.0.2 — 2018-09-25
- 1.0.1 — 2018-09-15
- 1.0.0 — 2017-01-21

## README

# time-ordered-set

Efficiently maintain a set of nodes ordered by the time they were added to the set

```
npm install time-ordered-set
```

## Usage

``` js
const TOS = require('time-ordered-set')
const s = new TOS()

// add 3 nodes

s.add({
  hello: 'world'
})

const node = s.add({
  hello: 'welt'
})

s.add({
  hello: 'verden'
})

// re-add the 2nd one

s.add(node)
console.log(s.toArray().map(node => node.hello)) // ['world', 'verden', 'welt']
```

## API

#### `const s = new TOS()`

Create a new set

#### `node = s.add(node)`

Add a new node to the set. Will add the properties `node.next` and `node.prev` to the node.
Re-adding the same node will move to the latest node.

#### `node = s.remove(node)`

Remove a node. Will set `node.next` and `node.prev` to `null`.

#### `bool = s.has(node)`

Check if a node has been added.

#### `const array = s.toArray([options])`

Get an ordered array out of all the nodes, ordered from oldest to newest. Use `options.reverse: true` to get from newest to oldest. Set `options.limit: number` if you only want to get a subset.

#### `s.oldest`

Property containing the oldest node.

#### `s.latest`

Property containing the newest node.

#### `s.length`

Property containing how many nodes are in the set.

## License

MIT

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