oculus42 / short-uuid

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

Can we generate 19 chars shortID using this package #59

Closed gobinath2389 closed 3 years ago

gobinath2389 commented 3 years ago

My use case is to generate 19 character shortID, can we provide any options to the length of shorter ID

oculus42 commented 3 years ago

The maximum length of the shortID is based on the size of the alphabet provided. If you generate an alphabet of 107-138 characters, you will get a maximum length of 19.

Something like this:

const Short = require('short-uuid');
const largeAlphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_+-=~[]{}\|;:,.<>/¿?¡!@#$¢¥£€∞%≠åæœÆŒç∑ß∆Ω®†øπ«»'
const nineteen = Short(largeAlphabet);

nineteen.maxLength; // 19

nineteen.generate(); // '{{€V∑Œø]FMmk7JlÆ∆PR'
nineteen.generate(); // '4|«£qπWCß,∆EO]!¡VrB'
nineteen.new();      // 'z[2£73Kh+væFzgR∑4XÆ' -- works the same as generate()

The connection between the alphabet and the maximum length is a requirement for keeping the same uniqueness as a regular UUID. The mapping is one-to-one.

nineteen.toUUID('{{€V∑Œø]FMmk7JlÆ∆PR') === 'f7235c8d-5517-4574-ad1c-45f8e7dac45a';
nineteen.toUUID('4|«£qπWCß,∆EO]!¡VrB') === '1089f234-7b84-46a8-9539-45631f035beb';
nineteen.toUUID('z[2£73Kh+væFzgR∑4XÆ') === '7e6397ce-05e8-4dee-b8d9-0fd6f6cb3b0c';
oculus42 commented 3 years ago

Closing as this is answered. Please continue this thread if you have more questions.