Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UUID module supporting v4 #479

Merged
merged 9 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
);
}