diff options
author | underfin <2218301630@qq.com> | 2020-04-27 20:49:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-27 08:49:34 -0400 |
commit | df0000ff0a3ce20292fe73b909cd31bd352d5266 (patch) | |
tree | c97fdab1bd238e7b551a6997f589c32c7ced1305 /std/uuid/_common.ts | |
parent | 516d970fd30cae6df071f6f2c8a980558b264438 (diff) |
feat(std/uuid): Implement uuid v5 (#4916)
Diffstat (limited to 'std/uuid/_common.ts')
-rw-r--r-- | std/uuid/_common.ts | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/std/uuid/_common.ts b/std/uuid/_common.ts index b0cad2584..8f13ac7e3 100644 --- a/std/uuid/_common.ts +++ b/std/uuid/_common.ts @@ -13,6 +13,35 @@ export function bytesToUuid(bytes: number[] | Uint8Array): string { "-", ...bits.slice(8, 10), "-", - ...bits.slice(10), + ...bits.slice(10, 16), ].join(""); } + +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 stringToBytes(str: string): number[] { + str = unescape(encodeURIComponent(str)); + const bytes = new Array(str.length); + for (let i = 0; i < str.length; i++) { + bytes[i] = str.charCodeAt(i); + } + return bytes; +} + +export function createBuffer(content: number[]): ArrayBuffer { + const arrayBuffer = new ArrayBuffer(content.length); + const uint8Array = new Uint8Array(arrayBuffer); + for (let i = 0; i < content.length; i++) { + uint8Array[i] = content[i]; + } + return arrayBuffer; +} |