-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} |