mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
BREAKING(uuid): remove v1.generate()
signature with buf
and offset
parameters and number[]
return type (#4877)
This commit is contained in:
parent
98c0c801e1
commit
b1684ec912
@ -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);
|
||||
}
|
||||
|
@ -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 = {
|
||||
|
48
uuid/v1.ts
48
uuid/v1.ts
@ -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;
|
||||
|
||||
@ -222,5 +184,5 @@ export function generate(
|
||||
b[i + n] = node[n]!;
|
||||
}
|
||||
|
||||
return buf ?? bytesToUuid(b);
|
||||
return bytesToUuid(b);
|
||||
}
|
||||
|
@ -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();
|
||||
@ -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(
|
||||
() => {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user