1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 01:59:06 -05:00
Original: f52b3ec002
This commit is contained in:
Lucas Caro 2019-07-03 08:13:22 -07:00 committed by Ryan Dahl
parent 78a9a27e6f
commit 6a0858bd5d
9 changed files with 162 additions and 0 deletions

View file

@ -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";

15
uuid/README.md Normal file
View file

@ -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
uuid/mod.ts Normal file
View file

@ -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
uuid/test.ts Executable file
View file

@ -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
uuid/tests/generate.ts Normal file
View file

@ -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
uuid/tests/isNil.ts Normal file
View file

@ -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
uuid/tests/v4/generate.ts Normal file
View file

@ -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
uuid/tests/v4/validate.ts Normal file
View file

@ -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
uuid/v4.ts Normal file
View file

@ -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)
);
}