oculus42 / short-uuid

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

short-uuid is not short :) #22

Closed ORESoftware closed 5 years ago

ORESoftware commented 5 years ago

I swear I have used this library before to good success, but I have this:

import * as short from 'short-uuid';
const id = short.uuid();

and it's generating a uuid that looks like:

"id": "297fbfb1-9006-4ca0-9e1d-5e86bc1cf2ec",

what am I doing wrong?

ORESoftware commented 5 years ago

ahh, in my other project I was using this:

import shortid = require("shortid");
const id = shortid.generate();

is the above not good enough? What do you think.

oculus42 commented 5 years ago

Hey,

short-uuid does involve an extra step, because it permits you to use any of a variety of translations, and the IDs can be consistently translated back to the full UUID. It actually starts with a valid v4 UUID, which is why calling short.uuid() gave you a full UUID; the uuid library is exposed for convenience.

import * as short from 'short-uuid';

const shortTranslator = short();

const id = shortTranslator.new(); // p7rqPaUAmeMyzQaJePjm5r

That defaults to the set Flickr uses to ensure that an ID can be read without confusion.

If you want shorter, there is a 90-character base that is still safe for sending in cookie headers:

const cookieTranslator = short(short.constants.cookieBase90);
const id2 = cookieTranslator.new(); // 73CDB4.^NLQl52id+5YI

These keys are longer than the shortid keys, but they have are complete UUIDs, so they have lower collision risk.

oculus42 commented 5 years ago

v3.1.0 is out, and includes a top-level short.generate() to make life easier for developers looking to quickly start generating shortened UUIDs.

Hope this helps!

ORESoftware commented 5 years ago

@oculus42 thanks!