# ksuid

> Node.js implementation of K-Sortable Globally Unique IDs

Latest version **3.0.0** (published 2021-10-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install ksuid
pnpm add ksuid
yarn add ksuid
bun add ksuid
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-10-30 |
| First published | 2017-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 261 |
| Author | Mark Wubben |
| Maintainers | novemberborn |
| Keywords | uuid, ksuid |

## Links

- npm: https://www.npmjs.com/package/ksuid
- Repository: https://github.com/novemberborn/ksuid
- Homepage: https://github.com/novemberborn/ksuid#readme
- Issues: https://github.com/novemberborn/ksuid/issues
- npm.io page: https://npm.io/package/ksuid

## Dependencies (1)

- [base-convert-int-array](https://npm.io/package/base-convert-int-array.md) ^1.0.1

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-10-30
- 2.0.0 — 2020-08-25
- 1.2.0 — 2019-03-24
- 1.1.3 — 2018-09-09
- 1.1.2 — 2018-07-12
- 1.1.1 — 2018-06-11
- 1.1.0 — 2018-06-02
- 1.0.0 — 2018-05-19
- 0.4.0 — 2017-08-17
- 0.3.0 — 2017-06-28
- 0.2.0 — 2017-06-11
- 0.1.0 — 2017-06-10

## README

# ksuid

A Node.js implementation of [Segment's KSUID library](https://github.com/segmentio/ksuid).

You may also be interested in [`ksuid-cli`](https://www.npmjs.com/package/ksuid-cli).

## Installation

```console
$ npm install ksuid
```

## Usage

Require the module:

```js
const KSUID = require('ksuid')
```

### Creation

You can create a new instance synchronously:

```js
const ksuidFromSync = KSUID.randomSync()
```

Or asynchronously:

```js
const ksuidFromAsync = await KSUID.random()
```

You can also specify a specific time, either in milliseconds or as a `Date` object:

```js
const ksuidFromDate = KSUID.randomSync(new Date("2014-05-25T16:53:20Z"))
const ksuidFromMillisecondsAsync = await KSUID.random(1401036800000)
```

Or you can compose it using a timestamp and a 16-byte payload:

```js
const crypto = require('crypto')
const yesterdayInMs = Date.now() - 86400 * 1000
const payload = crypto.randomBytes(16)
const yesterdayKSUID = KSUID.fromParts(yesterdayInMs, payload)
```

You can parse a valid string-encoded KSUID:

```js
const maxKsuid = KSUID.parse('aWgEPTl1tmebfsQzFP4bxwgy80V')
```

Finally, you can create a KSUID from a 20-byte buffer:

```js
const fromBuffer = new KSUID(buffer)
```

### Properties

Once the KSUID has been created, use it:

```js
ksuidFromSync.string // The KSUID encoded as a fixed-length string
ksuidFromSync.raw // The KSUID as a 20-byte buffer
ksuidFromSync.date // The timestamp portion of the KSUID, as a `Date` object
ksuidFromSync.timestamp // The raw timestamp portion of the KSUID, as a number
ksuidFromSync.payload // A Buffer containing the 16-byte payload of the KSUID (typically a random value)
```

### Comparisons

You can compare KSUIDs:

```js
todayKSUID.compare(yesterdayKSUID) // 1
todayKSUID.compare(todayKSUID) // 0
yesterdayKSUID.compare(todayKSUID) // -1
```

And check for equality:

```js
todayKSUID.equals(todayKSUID) // true
todayKSUID.equals(yesterdayKSUID) // false
```

### Validation

You can check whether a particular buffer is a valid KSUID:

```js
KSUID.isValid(buffer) // Boolean
```

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