oculus42 / short-uuid

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

Add UUID type #54

Closed AlexBeauchemin closed 3 years ago

AlexBeauchemin commented 3 years ago

Adding a UUID type so we can expect a uuid instead of a string using typescript

import uuid, { UUID } from 'short-uuid';

const id = uuid.generate();

create(id); // ok
create('abcd') // error

function create(id: UUID) {
  ...
}

This implementation extends string to keep backward compatibility, so this will continue working

const id: string = uuid.generate()
abeauchemin-planned commented 3 years ago

@oculus42 Sorry for the delay, but yes you were right we should be able to differentiate a uuid from a short uuid based on the type. So I added another type for the short uuid SUUID So no we can either use either string or UUID as the type for a UUID and string or SUUID for a short uuid, which shouldn't break backward compatibility and would allow users to have stricter implementation in typescript instead of relying on the string type and allowing a to be considered as a UUID for example

ie:

import { generate, SUID } from 'short-uuid';

const newId = generate();

function create(id: SUID) { ... }

create(newId) // ok
create('abc') // error
oculus42 commented 3 years ago

Sorry for the delay, and thanks for the update. Merged!