• Stars
    star
    248
  • Rank 163,560 (Top 4 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 7 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js implementation of K-Sortable Globally Unique IDs

ksuid

A Node.js implementation of Segment's KSUID library.

You may also be interested in ksuid-cli.

Installation

$ npm install ksuid

Usage

Require the module:

const KSUID = require('ksuid')

Creation

You can create a new instance synchronously:

const ksuidFromSync = KSUID.randomSync()

Or asynchronously:

const ksuidFromAsync = await KSUID.random()

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

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:

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:

const maxKsuid = KSUID.parse('aWgEPTl1tmebfsQzFP4bxwgy80V')

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

const fromBuffer = new KSUID(buffer)

Properties

Once the KSUID has been created, use it:

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:

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

And check for equality:

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

Validation

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

KSUID.isValid(buffer) // Boolean

More Repositories

1

babel-plugin-import-glob

Babel plugin to enable importing modules using a glob pattern
JavaScript
59
star
2

ignore-by-default

A list of directories you should ignore by default
JavaScript
23
star
3

copenhagen

Experimental infrastructure for creating Istanbul-compatible code coverage
JavaScript
21
star
4

package-hash

Generates a hash for an installed npm package, useful for salting caches
JavaScript
18
star
5

hullabaloo-config-manager

Manages complex Babel config chains, avoiding duplicated work and enabling effective caching
JavaScript
14
star
6

cached-module-loader

Highly experimental bundler and loader of Node.js modules
JavaScript
14
star
7

legendary

Dabbling with promises. It’ll be legendary
JavaScript
13
star
8

buoyant

Framework for implementing servers using the Raft Consensus Algorithm.
JavaScript
13
star
9

babel-plugin-es6-promise

Babel plugin that rewrites Promise references to es6-promise, but only if necessary
JavaScript
13
star
10

common-path-prefix

Computes the longest prefix string that is common to each path, excluding the base component
JavaScript
10
star
11

never

A function that throws when called, use with nullish coalescing to elide unexpected nullish values
JavaScript
8
star
12

identifierfy

Rewrites an identifier string so its valid according to ES2015
JavaScript
5
star
13

Browser-Extensions-for-Web-Hackers

Example code of my Browser Extensions for Web Hacker talk.
JavaScript
4
star
14

babel-plugin-files

Babel plugin to enable importing file metadata using a glob pattern
JavaScript
4
star
15

unicode-diff

Differences between Unicode versions
JavaScript
3
star
16

cloudflare-origin-pull

Verify that TLS peer certificates came from CloudFlare
JavaScript
2
star
17

chai-sentinels

Pass sentinel values through your code and assert the results with Chai.
JavaScript
2
star
18

wilee

Command line tool for interacting with ACME servers
JavaScript
2
star
19

linkedgov-hackcamp

JavaScript
2
star
20

aitch

Toolkit for constructing hypertext service clients.
JavaScript
2
star
21

legendary-node

Standard Node libraries reinterpreted with Legendary promises.
JavaScript
1
star
22

base-convert-int-array

Converts arrays of integers from one base to another. Uses an O(N²) algorithm.
JavaScript
1
star
23

sixtyfour

Base64 utilities for Node
JavaScript
1
star
24

release-zalgo

Helps you write code with promise-like chains that can run both synchronously and asynchronously.
JavaScript
1
star
25

thenstream

Construct a Readable stream from a thenable. Useful if you don't have the actual stream yet.
JavaScript
1
star
26

legendary-debug

Debugging utilities for Legendary promises
JavaScript
1
star
27

_ava-tap-crash

JavaScript
1
star
28

cityjsconf-value-comparisons

"Value Comparisons: How Do I Even?" @ City of London JavaScript Conference
CSS
1
star
29

eslint-plugin-as-i-preach

ESLint to @novemberborn’s taste
JavaScript
1
star
30

eh

Promise-based execution with halting
JavaScript
1
star
31

source-map-fixtures

Simple test fixtures backed by inline source maps and source map files
JavaScript
1
star
32

streamist

Enables functional, promise-based interactions with Readable streams.
JavaScript
1
star
33

x690

Low-level decoder for X.690 Distinguished Encoding Rules (DER)
JavaScript
1
star
34

WriteOnWall

WriteOnWall, as developed during the Mediamatic DevCamp 2010
JavaScript
1
star