28 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			28 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
// Taken from python's string module
 | 
						|
export const ascii_lowercase = "abcdefghijklmnopqrstuvwxyz";
 | 
						|
export const ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 | 
						|
export const ascii_letters = ascii_lowercase + ascii_uppercase;
 | 
						|
export const digits = "0123456789";
 | 
						|
export const hexdigits = digits + "abcdef" + "ABCDEF";
 | 
						|
export const octdigits = "01234567";
 | 
						|
export const punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
 | 
						|
 | 
						|
export function randomString(len: number, charset: string): string {
 | 
						|
    const chars = [];
 | 
						|
    const array = new Uint8Array(len);
 | 
						|
    globalThis.crypto.getRandomValues(array);
 | 
						|
    for (let index = 0; index < len; index++) {
 | 
						|
        chars.push(charset[Math.floor(charset.length * (array[index] / Math.pow(2, 8)))]);
 | 
						|
    }
 | 
						|
    return chars.join("");
 | 
						|
}
 | 
						|
 | 
						|
export function randomId() {
 | 
						|
    let dt = new Date().getTime();
 | 
						|
    return "xxxxxxxx".replace(/x/g, (c) => {
 | 
						|
        const r = (dt + Math.random() * 16) % 16 | 0;
 | 
						|
        dt = Math.floor(dt / 16);
 | 
						|
        return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
 | 
						|
    });
 | 
						|
}
 |