Skip to content

Commit

Permalink
add UUID module (denoland/std#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascaro authored and ry committed Jul 3, 2019
1 parent 78a9a27 commit 6a0858b
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "./strings/test.ts";
import "./testing/test.ts";
import "./textproto/test.ts";
import "./util/test.ts";
import "./uuid/test.ts";
import "./ws/test.ts";
import "./encoding/test.ts";
import "./os/test.ts";
Expand Down
15 changes: 15 additions & 0 deletions uuid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# UUID

Support for version 1, 3, 4, and 5 UUIDs.

## Usage

```ts
import uuid, { validate } from "https://deno.land/std/uuid/mod.ts";

// Generate a v4 uuid
const myUUID = uuid();

// Validate a v4 uuid
const isValid = validate(aString);
```
26 changes: 26 additions & 0 deletions uuid/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Based on https://github.com/kelektiv/node-uuid
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

export const NIL_UUID = "00000000-0000-0000-0000-000000000000";

export function isNil(val: string): boolean {
return val === NIL_UUID;
}

const NOT_IMPLEMENTED = (): void => {
throw new Error("Not implemented");
};

// TODO Implement
export const v1 = NOT_IMPLEMENTED;
// TODO Implement
export const v3 = NOT_IMPLEMENTED;

import _v4 from "./v4.ts";
export const v4 = _v4;

// TODO Implement
export const v5 = NOT_IMPLEMENTED;

export default v4;
export * from "./v4.ts";
13 changes: 13 additions & 0 deletions uuid/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env deno run
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { runIfMain } from "../testing/mod.ts";

// Generic Tests
import "./tests/isNil.ts";
import "./tests/generate.ts";

// V4 Tests
import "./tests/v4/validate.ts";
import "./tests/v4/generate.ts";

runIfMain(import.meta);
32 changes: 32 additions & 0 deletions uuid/tests/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../../testing/asserts.ts";
import { test } from "../../testing/mod.ts";
import mod, { validate, v4 } from "../mod.ts";
import { validate as validate4 } from "../v4.ts";

test({
name: "[UUID] uuid_v4",
fn(): void {
const u = mod();
assertEquals(typeof u, "string", "returns a string");
assert(u !== "", "return string is not empty");
}
});

test({
name: "[UUID] uuid_v4_format",
fn(): void {
for (let i = 0; i < 10000; i++) {
const u = mod() as string;
assert(validate(u), `${u} is not a valid uuid v4`);
}
}
});

test({
name: "[UUID] default_is_v4",
fn(): void {
assertEquals(mod, v4, "default is v4");
assertEquals(validate, validate4, "validate is v4");
}
});
16 changes: 16 additions & 0 deletions uuid/tests/isNil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert } from "../../testing/asserts.ts";
import { test } from "../../testing/mod.ts";
// @ts-ignore
import { NIL_UUID, isNil } from "../mod.ts";

test({
name: "[UUID] isNil",
fn(): void {
const nil = NIL_UUID;
const u = "582cbcff-dad6-4f28-888a-e062ae36bafc";
assert(isNil(nil));
assert(!isNil(u));
console.log("");
}
});
23 changes: 23 additions & 0 deletions uuid/tests/v4/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../../../testing/asserts.ts";
import { test } from "../../../testing/mod.ts";
import generate, { validate } from "../../v4.ts";

test({
name: "[UUID] test_uuid_v4",
fn(): void {
const u = generate();
assertEquals(typeof u, "string", "returns a string");
assert(u !== "", "return string is not empty");
}
});

test({
name: "[UUID] test_uuid_v4_format",
fn(): void {
for (let i = 0; i < 10000; i++) {
const u = generate() as string;
assert(validate(u), `${u} is not a valid uuid v4`);
}
}
});
17 changes: 17 additions & 0 deletions uuid/tests/v4/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert } from "../../../testing/asserts.ts";
import { test } from "../../../testing/mod.ts";
import generate, { validate } from "../../v4.ts";

test({
name: "[UUID] is_valid_uuid_v4",
fn(): void {
const u = generate();
const t = "84fb7824-b951-490e-8afd-0c13228a8282";
const n = "84fb7824-b951-490g-8afd-0c13228a8282";

assert(validate(u), `generated ${u} should be valid`);
assert(validate(t), `${t} should be valid`);
assert(!validate(n), `${n} should not be valid`);
}
});
19 changes: 19 additions & 0 deletions uuid/v4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

const UUID_RE = new RegExp(
"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",
"i"
);

export function validate(id: string): boolean {
return UUID_RE.test(id);
}

export default function generate(): string {
return "00000000-0000-4000-8000-000000000000".replace(
/[0]/g,
(): string =>
// random integer from 0 to 15 as a hex digit.
(crypto.getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
);
}

0 comments on commit 6a0858b

Please sign in to comment.