Skip to content

Commit

Permalink
chore: modify isUuid method to maintain the exact same legacy pre-V8.…
Browse files Browse the repository at this point in the history
…3.0 behaviour (#274)

* update isUuid to maintain the same legacy behaviour

* add a test for v1 and v3 UUIDs

* make the linter happy

* nil is not a v4 or v5

Co-authored-by: Golo Roden <golo.roden@thenativeweb.io>
  • Loading branch information
Mearman and goloroden committed Apr 1, 2021
1 parent 95d5380 commit 030b642
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/uuidv4.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deprecate } from 'util';
import { NIL as nil, v4, v5, validate } from 'uuid';
import { NIL as nil, v4, v5, validate, version } from 'uuid';

const regex = {
v4: /(?:^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}$)|(?:^0{8}-0{4}-0{4}-0{4}-0{12}$)/u,
Expand All @@ -19,7 +19,7 @@ const uuidv4 = deprecate(
);

const isUuid = deprecate(
(value: string): boolean => validate(value),
(value: string): boolean => validate(value) && (version(value) === 4 || version(value) === 5),
'isUuid() is deprecated. Use validate() from the uuid module instead.'
);

Expand Down
12 changes: 10 additions & 2 deletions test/unit/uuidv4Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,21 @@ suite('uuid', (): void => {
assert.that(isUuid('cdb63720-9628-5ef6-bbca-2e5ce6094f3c')).is.true();
});

test('returns true if an empty UUID is given.', async (): Promise<void> => {
assert.that(isUuid('00000000-0000-0000-0000-000000000000')).is.true();
test('returns false if an empty UUID is given.', async (): Promise<void> => {
assert.that(isUuid('00000000-0000-0000-0000-000000000000')).is.false();
});

test('returns false if no UUID v4 or v5 is given.', async (): Promise<void> => {
assert.that(isUuid('definitely-not-a-uuid')).is.false();
});

test('returns false if a UUID v1 is given.', async (): Promise<void> => {
assert.that(isUuid('d9428888-122b-11e1-b85c-61cd3cbb3210')).is.false();
});

test('returns false if a UUID v3 is given.', async (): Promise<void> => {
assert.that(isUuid('a981a0c2-68b1-35dc-bcfc-296e52ab01ec')).is.false();
});
});

suite('fromString', (): void => {
Expand Down

0 comments on commit 030b642

Please sign in to comment.