Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Created July 9, 2023 16:51
Show Gist options
  • Save arleighdickerson/77724229f2a54fa8ab1294b3cccb054a to your computer and use it in GitHub Desktop.
Save arleighdickerson/77724229f2a54fa8ab1294b3cccb054a to your computer and use it in GitHub Desktop.
idUtil
export namespace idUtil {
export function create() {
let str = '';
for (let i = 0; i < 16; i += 1) {
const base = 'abcdefghijklmnopqrstuvwxyz234567'; //a-z, 2-7
const array = new Int32Array(1);
window.crypto.getRandomValues(array);
const j = array[0];
str +=
base[(j >>> 27) & 0x1f] +
base[(j >>> 22) & 0x1f] +
base[(j >>> 17) & 0x1f] +
base[(j >>> 12) & 0x1f] +
base[(j >>> 7) & 0x1f] +
base[(j >>> 2) & 0x1f]; // (length is always 6)
}
return str;
}
// case-insensitive
export function validate(id: any): id is string {
return typeof id === 'string' && id.length === 96 && new RegExp(/^[a-z2-7]+$/i).test(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment