fsdb/tests/helpers.ts
2025-07-01 19:14:18 -07:00

53 lines
2 KiB
TypeScript

import lurid from '@andyburke/lurid';
import { convert_to_words } from '@andyburke/lurid/word_bytes';
const TLDs: string[] = [
'com',
'org',
'net',
'edu',
'gov',
'nexus',
'shop',
'unreasonablylongtldname'
];
const random_byte_buffer: Uint8Array = new Uint8Array(3);
export function random_email_address(): string {
for (let i: number = 0; i < random_byte_buffer.length; ++i) random_byte_buffer[i] = Math.floor(Math.random() * 256);
const name = convert_to_words(random_byte_buffer).join('-');
for (let i: number = 0; i < random_byte_buffer.length; ++i) random_byte_buffer[i] = Math.floor(Math.random() * 256);
const domain = convert_to_words(random_byte_buffer).join('-');
const tld = TLDs[Math.floor(Math.random() * TLDs.length)];
return `${name}@${domain}.${tld}`;
}
export function random_username(): string {
for (let i: number = 0; i < random_byte_buffer.length; ++i) random_byte_buffer[i] = Math.floor(Math.random() * 256);
return convert_to_words(random_byte_buffer).join('-');
}
function get_a_random_array_element(values: any[]): any {
return values[Math.floor(Math.random() * values.length)];
}
const country_codes: string[] = ['', '+1', '1', '01', '219', '40', '506', '999'];
const digits: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const joinings: string[] = [' ', '-', '.'];
export function random_phone_number(): string {
const country_code = get_a_random_array_element(country_codes);
const area_code = [0, 0, 0].map((_) => get_a_random_array_element(digits)).join('');
const central_office_code = [0, 0, 0].map((_) => get_a_random_array_element(digits)).join('');
const subscriber_code = [0, 0, 0, 0].map((_) => get_a_random_array_element(digits)).join('');
return `${country_code}${country_code ? get_a_random_array_element(joinings) : ''}${area_code}${
get_a_random_array_element(joinings)
}${central_office_code}${get_a_random_array_element(joinings)}${subscriber_code}`;
}
const DATA_DIR = lurid();
export function get_data_dir(): string {
return Deno.env.get('FSDB_TEST_DATA_STORAGE_ROOT') ?? DATA_DIR;
}