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 2 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
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 { runTests } from "https://deno.land/std/testing/mod.ts";
lucascaro marked this conversation as resolved.
Show resolved Hide resolved

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

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

runTests();
23 changes: 23 additions & 0 deletions uuid/tests/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 "https://deno.land/std/testing/asserts.ts";
import { test } from "https://deno.land/std/testing/mod.ts";
lucascaro marked this conversation as resolved.
Show resolved Hide resolved
import mod, { validate, v4 } from "../mod.ts";
import { validate as validate4 } from "../v4.ts";

test(function test_uuid_v4() {
const u = mod();
assertEquals(typeof u, "string", "returns a string");
assert(u !== "", "return string is not empty");
});

test(function test_uuid_v4_format() {
for (let i = 0; i < 10000; i++) {
const u = mod() as string;
assert(validate(u), `${u} is not a valid uuid v4`);
}
});

test(function test_default_is_v4() {
assertEquals(mod, v4, "default is v4");
assertEquals(validate, validate4, "validate is v4");
});
13 changes: 13 additions & 0 deletions uuid/tests/isNil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert } from "https://deno.land/std/testing/asserts.ts";
import { test } from "https://deno.land/std/testing/mod.ts";
// @ts-ignore
import { NIL_UUID, isNil } from "../mod.ts";

test(function test_isNil() {
const nil = NIL_UUID;
const u = "582cbcff-dad6-4f28-888a-e062ae36bafc";
assert(isNil(nil));
assert(!isNil(u));
console.log("");
});
17 changes: 17 additions & 0 deletions uuid/tests/v4/generate.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, assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { test } from "https://deno.land/std/testing/mod.ts";
import generate, { validate } from "../../v4.ts";

test(function test_uuid_v4() {
const u = generate();
assertEquals(typeof u, "string", "returns a string");
assert(u !== "", "return string is not empty");
});

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

test(function test_is_valid_uuid_v4() {
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`);
});
37 changes: 37 additions & 0 deletions uuid/v4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
/**
* Super-small uuid generation.
*
* Based on
* https://gist.github.com/jed/982883
lucascaro marked this conversation as resolved.
Show resolved Hide resolved
*/

const UUID_RE = /^[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 (
// return
(
"" + // a concatentated string:
1e7 + // 10000000 +
-1e3 + // -1000 +
-4e3 + // -4000 +
-8e3 + // -80000000 +
-1e11
) // -100000000000,
.replace(
// replacing
/[01]/g, // ones and zeroes with a
(): string =>
(
0 | // floored
(Math.random() * 16)
) // random integer from 0 to 15
.toString(16) // as a hex digit.
)
);
}