All files util.ts

100% Statements 29/29
75% Branches 3/4
100% Functions 3/3
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x   1x 2x 2x 2x 16x   2x     1x   2x 1x 1x       1x 1x 1x 1x 5x   1x     1x   2x 1x       1x 1x 1x 5x     1x      
export const AlphaNum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export const Hex = 'abcdef0123456789';
 
export function randomString(length = 12, characters = Hex) {
	let result = '';
	const charactersLength = characters.length;
	for (let i = 0; i < length; i++) {
		result += characters.charAt(Math.floor(Math.random() * charactersLength));
	}
	return result;
}
 
export function base64ToArrayBuffer(base64: string): ArrayBuffer {
	// For Node.js
	if (typeof Buffer !== 'undefined') {
		const buffer = Buffer.from(base64, 'base64');
		return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
	}
 
	// For browsers (as a fallback)
	const binaryString = atob(base64);
	const len = binaryString.length;
	const bytes = new Uint8Array(len);
	for (let i = 0; i < len; i++) {
		bytes[i] = binaryString.charCodeAt(i);
	}
	return bytes.buffer;
}
 
export function arrayBufferToBase64(buffer: ArrayBuffer): string {
	// For Node.js
	if (typeof Buffer !== 'undefined') {
		return Buffer.from(buffer).toString('base64');
	}
 
	// For browsers (as a fallback)
	const bytes = new Uint8Array(buffer);
	let binary = '';
	for (let i = 0; i < bytes.byteLength; i++) {
		binary += String.fromCharCode(bytes[i]);
	}
 
	return btoa(binary);
}