@benev/slate
Version:
frontend web stuff
32 lines • 1.14 kB
JavaScript
import { ob } from "../../ob.js";
import { Bytes } from "../bytes.js";
export class Grammar {
phraseFns;
templates;
constructor(phraseFns, templates) {
this.phraseFns = phraseFns;
this.templates = templates;
}
static phrases = (phrases) => ({
templates: (templates) => (new this(phrases, templates)),
});
generate([firstByte, ...bytes]) {
const template = this.templates[firstByte % this.templates.length];
const phraseCount = Object.keys(this.phraseFns).length;
const bytesPerPhrase = Math.floor(bytes.length / phraseCount);
let indexCounter = 0;
const phrases = ob(this.phraseFns).map(fn => {
const index = indexCounter++;
const start = index * bytesPerPhrase;
const end = Math.min(start + bytesPerPhrase, bytes.length - 1);
const buffer = new Uint8Array(bytes.slice(start, end));
return fn(buffer);
});
return template(phrases);
}
random() {
const buffer = Bytes.random(32);
return this.generate(buffer);
}
}
//# sourceMappingURL=grammar.js.map