oculus42 / short-uuid

Translate standard UUIDs into shorter formats and back.
MIT License
450 stars 13 forks source link

Customise Length of UUID #55

Closed cakesoft-shivani closed 3 years ago

cakesoft-shivani commented 3 years ago

Can I put custom length of UUID ?

oculus42 commented 3 years ago

You can provide a custom alphabet which will produce a different length of translation, but a UUID is a specific format, and short-uuid will always return to the UUID format, even if the value is shorter.

const shortUUID = require('short-uuid');
const short = short(short.constants.flickrBase58, {consistentLength: false});

// Provide any length as long as it is hexadecimal
const s1 = short.fromUUID('12345'); // 'oaB'
const u1 = short.toUUID('oaB'); // '00000000-0000-0000-0000-000000012345'

If you want to translate another identifier format, I recommend using any-base directly, which handles most of the translation in short-uuid. Something like this:

const anyBase = require('any-base');

// Using short-uuid constant for Flickr, just for consistency in the examples
const baseHexToFlickr = anyBase(anyBase.HEX, short.constants.flickrBase58);
const baseFlickrToHex = anyBase(short.constants.flickrBase58, anyBase.HEX);

const s1 = baseHexToFlickr('12345'); // 'oaB'
const u1 = baseFlickrToHex('aoB'); // '12345'
oculus42 commented 3 years ago

I am closing this issue, as it is answered. If you have additional questions on this, please feel free to reply.