short-uuid
Generate and translate standard UUIDs into shorter - or just different - formats and back.
v4.2.2
Major Changes in 4.0.0
- 🛑 short-uuid will now throw an error when provided an alphabet with duplicate characters. Duplicate characters will cause translation errors.
- ℹ️ 4.0.0 is written in modern ECMAScript. It uses features through ES9/ES2018.
- ℹ️ By default, short-uuid will pad shortened IDs to a consistent length.
- Padding does not affect translation, and the values are compatible with previous releases.
- Padding can be disabled with the option
consistentLength: false
when instantiating a translator. This is consistent with previous versions.
- ℹ️ 4.x does not include a pre-built version for browsers. If needed, 3.1.1 is compatible, but does not pad to a consistent length.
Quick Start
const short = require('short-uuid');
short.generate();
Details
short-uuid starts with RFC4122 v4-compliant UUIDs and translates them
into other, usually shorter formats. It also provides translators
to convert back and forth from RFC compliant UUIDs to the shorter formats.
As of 4.0.0, formats return consistent-length values unless specifically requested.
This is done by padding the start with the first ([0]
) character in the alphabet.
Previous versions can translate padded formats back to UUID.
const short = require('short-uuid');
const translator = short();
const decimalTranslator = short("0123456789");
const cookieTranslator = short(short.constants.cookieBase90);
translator.new();
translator.generate();
translator.toUUID(shortId);
translator.fromUUID(regularUUID);
short.uuid();
translator.uuid();
translator.alphabet;
translator.maxLength;
short.constants.flickrBase58;
short.constants.cookieBase90;
Options
short-uuid 4.0.0 adds support for options when creating a translator.
This will support additional configuration in the future.
const short = require('short-uuid');
const translator = short(short.constants.flickrBase58, {
consistentLength: false,
});
translator.new();
consistentLength
- Controls padding on shortened values. Default is true
.
Support
short-uuid 4.0.0 and later is confirmed to work on Node 8.x and later.
- Pre-compiled browser version is planned for future release.
short-uuid 3.1.1
and lower is confirmed to work on Node 0.10.x and later,
and browsers with a precompiled library proposed
by voronianski.
Notes
short-uuid provides RFC4122 v4-compliant UUIDs,
thanks to uuid
.
TypeScript definitions are included, thanks to
alexturek.
Recent Release Notes
4.1.0 adds a maxLength value to translators for reference
4.0.3 fixes default generate
4.0.1 adds consistent length translation and throws an error if provided an invalid alphabet.
3.1.1 updated dev dependencies which required dropping Node 4.x build test. Last Browserify distribution version temporarily.
2.3.4 corrects the behavior for UUIDs with uppercase letters. Last version to build on Node 0.x.
Please see Revisions for information on previous versions.