0.2.1 • Published 3 years ago

ordered-uuid-v4 v0.2.1

Weekly downloads
18
License
MIT
Repository
github
Last release
3 years ago

Ordered UUID v4 - COMB (combined time-GUID)

ordered-uuid-v4 is a JavaScript package that can generate a COMB (combined time-GUID), basically an ordered UUID. The COMB/ordered UUID contains:

  • 48-bit timestamp: the amount of centimilliseconds (10-5) since 00:00:00 UTC on 1 January 1970
  • 72-bit of randomness

A COMB replaces the first first 48 bits of a version 4, random UUID with a timestamp. Therefore the UUIDs are monotonically increasing (each one coming after the previously-created one) and they can be conveniently ordered.

Example of a generated timestamp-first COMB: 9256dc86-ca15-4e2d-85e2-dfd4d103ae0b

Advantages

  • Enjoy the benefits of random UUID while also allow sorting in chronological order
  • Compatible with software/databases that accept UUID v4 (because a COMB is in the same format as UUID v4)
  • Still a low chance of collision

Please note: this package generates an indentifier that looks the same as a UUID v4. Even though the format is the same, it is non-standard and not RFC-compliant.

Quickstart

1. Install

Use the package manager npm to install the package:

npm install ordered-uuid-v4

2. Create a COMB (ES6 module syntax)

import { generate as generateComb } from 'ordered-uuid-v4';

const comb = generateComb();
console.log(comb);
// example of output: '9256cf92-2737-4a9c-8bb1-8f08c744b796'

... or using CommonJS syntax:

const { generate: generateComb } = require('uuid');

const comb = generateComb();
console.log(comb);

API summary

comb.generate()Generate a timestamp-first COMB
comb.convertTime()Convert COMB string to a timestamp (formatted as UNIX, Date object, etc.)
comb.convertNumber()Convert 48-bit timestamp & 15-bit random part of COMB string to a number
comb.validate()Test a string to see if it is a valid UUID v4; does not distinguish between COMB and UUID v4!
comb.timestamp()Retrieve an integer consisting of the UNIX time & amount of centimilliseconds
comb.stringify()Convert timestamp hex & array of bytes to UUID string

API

comb.generate(options)

Create a timestamp-first COMB

optionsObject with one or more of the following properties:
options.randomArray of 10 random bytes (0-255)
options.rngAlternative to options.random, a Function that returns an Array of 10 random bytes (0-255)
options.timestamp48-bit timestamp as a Number
options.timestampGeneratorA Function that returns a Number of a 48-bit timestamp
returnsCOMB String

Example:

import { generate as generateComb } from 'ordered-uuid-v4';

generateComb(); // ⇨ '9256cf92-2737-4a9c-8bb1-8f08c744b796'

Example using predefined random values:

import { generate as generateComb } from 'ordered-uuid-v4';

const options = {
  random: [
    0xc1,
    0xea,
    0x71,
    0xb4,
    0xef,
    0xe1,
    0x67,
    0x1c,
    0x58,
    0x36,
  ],
};
generateComb(options); // ⇨ '9257dfe0-e6a1-41ea-b1b4-efe1671c5836'

comb.convertTime(uuid, type)

Get time information of COMB

Note: Only convert ordered UUIDs - those of which you are certain that it was generated with a timestamp. There is no way for this package to know if your input is a COMB or not. If not, it will still return a value, but this value is random and therefore meaningless.

uuidString COMB to convert
typeString format to return date in (see list below)
returnsNumber (48-bit), or date/time in format as specified at type
throwsTypeError if the UUID input string is invalid

Options for type:

unixNumber Unix time without extra precisionMay be used for displaying time, not recommended for sorting
unix-floatNumber floating point - Same as unix, except it has extra precision (after the decimal)
date-objectDate object with millisecond precisionUseful for displaying time, as input for time lib (e.g. Moment.js)
Fallback (default - no type specified)Number amount of centimilliseconds since 00:00:00 UTC on 1 January 1970Best type to use for sorting between identifiers

Example Fallback:

import { convertTime as combConvertTime } from 'ordered-uuid-v4';

combConvertTime('9257e320-80ff-44b6-a5b3-d8efedc3725c'); // ⇨ 160906170368255

Example date-object:

import { convertTime as combConvertTime } from 'ordered-uuid-v4';

const date = combConvertTime('9257e320-80ff-44b6-a5b3-d8efedc3725c', 'date-object');
console.log(date); // expected output: 2020-12-27T09:35:03.682Z

comb.convertNumber(uuid)

Get time information of COMB combined with a random part. Useful for consistent sorting if the centimillisecond precision of the timestamp is not great enough (see situation)

uuidString COMB to convert
returnsBigInt consisting of the 48-bit timestamp and a 15-bit random part
throwsTypeError if the UUID input string is invalid

Example:

import { convertNumber as combConvertNumber } from 'ordered-uuid-v4';

combConvertNumber('9257e320-80ff-44b6-a5b3-d8efedc3725c'); // expected return: 16090617036825517590n

Situation: when generating COMBs in a loop, multiple COMBS might be generated at the same time, meaning they have the same timestamp. This means they cannot be ordered chronologically. The best you can do is to also convert a part of the random bits of the COMB to a number and append this to the timestamp. Why? Because then the input order does not matter and can therefore be different every time, though the sorting output will be consistent every time. The convertNumber function can help accomplish this, as can be seen in this example.

comb.validate(str)

Test a string to see if it is a valid UUID v4/COMB.

Note: This function cannot distinguish between a true RFC-compliant UUID v4 and a timestamp-first COMB as input; both inputs will return true.

strString to validate
returnstrue if string is a valid UUID, false otherwise

Example:

import { validate as validateComb } from 'ordered-uuid-v4';

validateComb('not valid'); // ⇨ false
validateComb('9257e320-80ff-44b6-a5b3-d8efedc3725c'); // ⇨ true

comb.timestamp()

Retrieve current time

returnsNumber Timestamp - amount of centimilliseconds since 00:00:00 UTC on 1 January 1970

Example:

import { timestamp as combTimestamp } from 'ordered-uuid-v4';

combTimestamp(); // ⇨ 160906771016926

comb.stringify(timestamp, arr)

Convert array of bytes to UUID string

timestampString 48-bit timestamp in hex format
arrArray-like collection of 10 values between 0-255.
returnsString
throwsTypeError if a valid string cannot be generated

Example:

import { stringify as combStringify } from 'ordered-uuid-v4';

const timestamp = '9258059fac7c';
const bytes = [
  0x43,
  0xda,
  0x97,
  0x5e,
  0x2a,
  0x8a,
  0xd9,
  0xeb,
  0xae,
  0x0b,
];

combStringify(timestamp, bytes); // ⇨ '9258059f-ac7c-43da-975e-2a8ad9ebae0b'

Sources & material

  • Blog post: The mysterious “Ordered UUID”
  • The ramsey/uuid implementation has a codec named TimestampFirstCombCodec. This has been the reference implementation for creating this package. Check out this great page for more information.
  • The uuidjs/uuid codebase has been the foundation for the code of this package; many functions are similar to those found in that package.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT