Skip to content

Commit

Permalink
feat(utils): map uuid to crypto.randomUUID native;
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Sep 4, 2021
1 parent 635f2fd commit 3fb1e21
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
declare global {
interface Crypto {
randomUUID(): string;
}
}

/**
* All 256 hexadecimal pairs
* @NOTE Maximum index is `255`
Expand Down
10 changes: 1 addition & 9 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { suite } from 'uvu';
import { randomBytes } from 'crypto';
import * as assert from 'uvu/assert';
import * as utils from './utils';

// @ts-ignore
// @ts-ignore - missing def
import isUUID from 'is-uuid';

globalThis.crypto = {
// @ts-ignore
getRandomValues(arr: Uint8Array) {
return randomBytes(arr.length);
}
};

// ---

const HEX = suite('HEX');
Expand Down
14 changes: 1 addition & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,7 @@ export function viaHEX(input: string): Uint8Array {
}

// @see https://github.com/lukeed/uuid
export function uuid(): string {
var str='', i=0, arr=randomize(16);

for (; i < 16; i++) {
if (i===6) str += HEX[arr[i] & 15 | 64];
else if (i===8) str += HEX[arr[i] & 63 | 128];
else str += HEX[arr[i]];

if (i & 1 && i > 1 && i < 11) str += '-';
}

return str;
}
export const uuid = () => crypto.randomUUID();

// Alphabet for `uid` generator
const ALPHANUM = /*#__PURE__*/ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_';
Expand Down

0 comments on commit 3fb1e21

Please sign in to comment.