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

BREAKING(uuid): remove v1.generate() signature with buf and offset parameters and number[] return type #4877

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 6 additions & 9 deletions uuid/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ export function bytesToUuid(bytes: number[] | Uint8Array): string {
* 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[] = [];

uuid.replace(/[a-fA-F0-9]{2}/g, (hex: string): string => {
bytes.push(parseInt(hex, 16));
return "";
});

return bytes;
export function uuidToBytes(uuid: string): Uint8Array {
const bytes = uuid
.replaceAll("-", "")
.match(/.{1,2}/g)!
.map((byte) => parseInt(byte, 16));
return new Uint8Array(bytes);
}
2 changes: 1 addition & 1 deletion uuid/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { generate as generateV5, validate as validateV5 } from "./v5.ts";
* import { assert } from "@std/assert/assert";
*
* const uuid = v1.generate();
* assert(v1.validate(uuid as string));
* assert(v1.validate(uuid));
* ```
*/
export const v1 = {
Expand Down
48 changes: 5 additions & 43 deletions uuid/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,51 +100,13 @@ export interface GenerateOptions {
* nsecs: 5678,
* };
*
* const uuid = generate(options) as string;
* assert(validate(uuid));
* ```
*
* @deprecated This will be removed in 1.0.0. Use the other overload instead.
*/
export function generate(
options?: GenerateOptions,
buf?: number[],
offset?: number,
): string | number[];
/**
* Generates a
* {@link https://www.rfc-editor.org/rfc/rfc9562.html#section-5.1 | UUIDv1}.
*
* @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.
*
* @returns Returns a UUIDv1 string or an array of 16 bytes.
*
* @example Usage
* ```ts
* import { generate, validate } from "@std/uuid/v1";
* import { assert } from "@std/assert/assert";
*
* const options = {
* node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
* clockseq: 0x1234,
* msecs: new Date("2011-11-01").getTime(),
* nsecs: 5678,
* };
*
* const uuid = generate(options) as string;
* const uuid = generate(options);
* assert(validate(uuid));
* ```
*/
export function generate(options?: GenerateOptions): string;
export function generate(
options: GenerateOptions = {},
buf?: number[],
offset?: number,
): string | number[] {
let i = (buf && offset) || 0;
const b = buf ?? [];
export function generate(options: GenerateOptions = {}): string {
let i = 0;
const b = [];

let { node = _nodeId, clockseq = _clockseq } = options;

Expand Down Expand Up @@ -222,5 +184,5 @@ export function generate(
b[i + n] = node[n]!;
}

return buf ?? bytesToUuid(b);
return bytesToUuid(b);
}
16 changes: 0 additions & 16 deletions uuid/v1_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "@std/assert";
import { generate, validate } from "./v1.ts";
import { uuidToBytes } from "./_common.ts";

Deno.test("validate() checks if a string is a valid v1 UUID", () => {
const u = generate();
Expand Down Expand Up @@ -44,21 +43,6 @@ Deno.test("generate() can generate a static v1 UUID", () => {
assertEquals(u, "710b962e-041c-11e1-9234-0123456789ab");
});

Deno.test("generate() can fill the UUID into a buffer", () => {
const buf: number[] = [];
const v1options = {
node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
clockseq: 0x1234,
msecs: new Date("2011-11-01").getTime(),
nsecs: 5678,
};
const uuid = generate(v1options, buf, 0);
const expect = uuidToBytes("710b962e-041c-11e1-9234-0123456789ab");

assertEquals(buf, expect);
assertEquals(buf, uuid);
});

Deno.test("generate() throws when node is passed with less than 6 numbers", () => {
assertThrows(
() => {
Expand Down
4 changes: 2 additions & 2 deletions uuid/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export async function generate(
if (!validateCommon(namespace)) {
throw new TypeError("Invalid namespace UUID");
}
const space = uuidToBytes(namespace);
const toHash = concat([new Uint8Array(space), data]);
const namespaceBytes = uuidToBytes(namespace);
const toHash = concat([namespaceBytes, data]);
const buffer = await crypto.subtle.digest("MD5", toHash);
const bytes = new Uint8Array(buffer);

Expand Down
4 changes: 2 additions & 2 deletions uuid/v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export async function generate(
throw new TypeError("Invalid namespace UUID");
}

const space = uuidToBytes(namespace);
const toHash = concat([new Uint8Array(space), data]);
const namespaceBytes = uuidToBytes(namespace);
const toHash = concat([namespaceBytes, data]);
const buffer = await crypto.subtle.digest("sha-1", toHash);
const bytes = new Uint8Array(buffer);

Expand Down