UNPKG

rword

Version:

A cryptographically secure random generator for real English words. Contains 370,000 words.

35 lines 982 B
import { Random } from './Random.js'; export class Rword { generations = 0; seedChars; words = []; constructor(words, seed) { this.seedChars = seed ? Array.from(seed).map((c) => c.charCodeAt(0)) : undefined; this.load(words.slice()); } generate(length = 1) { if (length <= 0) return []; if (this.seedChars) { return Array.from({ length }, () => { const index = Math.floor(Random.seededValue(this.seedChars, this.generations++) * this.words.length); return this.words[index]; }); } return Random.indexes(this.words.length, length).map((i) => this.words[i]); } getWords() { return this.words; } shuffle() { Random.shuffle(this.words, this.seedChars); } load(words) { this.words = words; this.shuffle(); } } //# sourceMappingURL=Rword.js.map