1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

docs(std/uuid): Added JSdocs for the std/uuid module (#7735)

This commit is contained in:
aakhtar3 2020-10-01 05:40:40 -04:00 committed by GitHub
parent 326ccb1095
commit da29ccece5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 1 deletions

View file

@ -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);
@ -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[] = [];
@ -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);
@ -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);

View file

@ -1,4 +1,5 @@
// 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";
@ -6,6 +7,10 @@ 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;
}

View file

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

View file

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

View file

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