Skip to content

Commit

Permalink
docs(uuid): Added JSdocs for the uuid module (denoland/deno#7735)
Browse files Browse the repository at this point in the history
  • Loading branch information
aakhtar3 authored and caspervonb committed Jan 24, 2021
1 parent 74473a8 commit 9c5c2af
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions uuid/_common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/**
* Converts the byte array to a UUID string
* @param bytes Used to convert Byte to Hex
*/
export function bytesToUuid(bytes: number[] | Uint8Array): string {
const bits: string[] = [...bytes].map((bit): string => {
const s: string = bit.toString(16);
Expand All @@ -17,6 +21,10 @@ export function bytesToUuid(bytes: number[] | Uint8Array): string {
].join("");
}

/**
* Converts a string to a byte array by converting the hex value to a number
* @param uuid Value that gets converted
*/
export function uuidToBytes(uuid: string): number[] {
const bytes: number[] = [];

Expand All @@ -28,6 +36,10 @@ export function uuidToBytes(uuid: string): number[] {
return bytes;
}

/**
* Converts a string to a byte array using the char code
* @param str Value that gets converted
*/
export function stringToBytes(str: string): number[] {
str = unescape(encodeURIComponent(str));
const bytes = new Array(str.length);
Expand All @@ -37,6 +49,10 @@ export function stringToBytes(str: string): number[] {
return bytes;
}

/**
* Creates a buffer for creating a SHA-1 hash
* @param content Buffer for SHA-1 hash
*/
export function createBuffer(content: number[]): ArrayBuffer {
const arrayBuffer = new ArrayBuffer(content.length);
const uint8Array = new Uint8Array(arrayBuffer);
Expand Down
7 changes: 6 additions & 1 deletion uuid/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Based on https://github.com/kelektiv/node-uuid
// Based on https://github.com/kelektiv/node-uuid -> https://www.ietf.org/rfc/rfc4122.txt
// Supporting Support for RFC4122 version 1, 4, and 5 UUIDs
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as v1 from "./v1.ts";
import * as v4 from "./v4.ts";
import * as v5 from "./v5.ts";

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

/**
* Checks if UUID is nil
* @param val UUID value
*/
export function isNil(val: string): boolean {
return val === NIL_UUID;
}
Expand Down
10 changes: 10 additions & 0 deletions uuid/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const UUID_RE = new RegExp(
"i",
);

/**
* Validates the UUID v1
* @param id UUID value
*/
export function validate(id: string): boolean {
return UUID_RE.test(id);
}
Expand All @@ -25,6 +29,12 @@ type V1Options = {
rng?: () => number[];
};

/**
* Generates a RFC4122 v1 UUID (time-based)
* @param options Can use RFC time sequence values as overwrites
* @param buf Can allow the UUID to be written in byte-form starting at the offset
* @param offset Index to start writing on the UUID bytes in buffer
*/
export function generate(
options?: V1Options | null,
buf?: number[],
Expand Down
5 changes: 5 additions & 0 deletions uuid/v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ const UUID_RE = new RegExp(
"i",
);

/**
* Validates the UUID v4
* @param id UUID value
*/
export function validate(id: string): boolean {
return UUID_RE.test(id);
}

/** Generates a RFC4122 v4 UUID (pseudo-randomly-based) */
export function generate(): string {
const rnds = crypto.getRandomValues(new Uint8Array(16));

Expand Down
10 changes: 10 additions & 0 deletions uuid/v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { assert } from "../_util/assert.ts";
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

/**
* Validates the UUID v5
* @param id UUID value
*/
export function validate(id: string): boolean {
return UUID_RE.test(id);
}
Expand All @@ -20,6 +24,12 @@ interface V5Options {
namespace: string | number[];
}

/**
* Generates a RFC4122 v5 UUID (SHA-1 namespace-based)
* @param options Can use a namespace and value to creat SHA-1 hash
* @param buf Can allow the UUID to be written in byte-form starting at the offset
* @param offset Index to start writing on the UUID bytes in buffer
*/
export function generate(
options: V5Options,
buf?: number[],
Expand Down

0 comments on commit 9c5c2af

Please sign in to comment.