import {Bytes} from "../bytes.js" import {Dictionary, Pattern} from "./types.js" export class Nomen { constructor(public dictionary: D, public patterns: Pattern[]) {} static dictionary = (dictionary: D) => ({ patterns: (patterns: Pattern[]) => ( new this(dictionary, patterns) ), }) generate([firstByte, ...bytes]: Uint8Array) { const pattern = this.patterns[firstByte % this.patterns.length] let index = 0 const obtainWordChunk = (key: keyof D) => { const dictionary = this.dictionary[key] const byte = bytes[(index++) % bytes.length] return dictionary[byte % dictionary.length] } return pattern .map(wordpattern => wordpattern.map(obtainWordChunk).join("")) .join(" ") } random() { const buffer = Bytes.random(32) return this.generate(buffer) } }