UNPKG

@sunney/flareutils

Version:

Small Utilities and little goodies that make developing with Cloudflare easier and faster.

45 lines 1.45 kB
function randOf(collection, rand) { return () => collection[Math.floor(rand() * collection.length)]; } /** * Generator that generates random phonetic strings. Cryptographic security is dependent on the random number generator that is passed in. */ export class Phonetic { /** * Random number generator used by Phonetic. Must generate numbers from 0 to 1. */ generator; /** * Returns a random vowel. */ vowel; /** * Returns a random consonant. */ consonant; /** * Creates a new Phonetic generator. * @param {RandNum} rand Random Number Generator. While it is recommended to use a cryptographically secure random number generator(a la. [Isaac](/classes/Isaac)), this is not required. * @constructor */ constructor(rand) { this.generator = rand; this.vowel = randOf("aeiou", rand); this.consonant = randOf("bcdfghjklmnpqrstvwxyz", rand); } /** * Generates a random phonetic string. * @param {number} length Length of the string to generate. * @returns {string} Random phonetic string. * @example * const phonetic = phonetic.rand(10); */ rand(len = 10) { let id = ""; const start = Math.round(this.generator()); for (let i = 0; i < len; i++) id += i % 2 === start ? this.consonant() : this.vowel(); return id; } } //# sourceMappingURL=Phonetic.js.map