node-unique

A unique ID generator for node

Usage no npm install needed!

<script type="module">
  import nodeUnique from 'https://cdn.skypack.dev/node-unique';
</script>

README

unique

Unique generates IDs, that is all. The IDs that are generated have the following properties.

  • Portable - Unique generates String IDs so they can be used anywhere.
  • Compact - Unique Base64 encodes IDs to minimize wasted space.
  • Timely - Unique includes a timestamp to the millisecond in its IDs.
  • Orderly - Unique IDs are lexicographically ordered by time.
  • Random - Unique includes 122 random bits in its IDs.

Definition: base64(bin(hex(now()) + uuidV4()))

Where:

  • now() returns an integer representing milliseconds since epoch
  • uuidV4() returns a 32 character hex string (no dashes)

API

unique([ts]) - Generates a new id.

ts: Date (Optional)

  • Description: A date representing the timestamp you want encoded into your ID.
  • Default: Now.

unique.getTimestamp(id) - Returns the timestamp encoded in the given id.

id: String

  • Description: An id to extract the timestamp from.