UNPKG

kuvio

Version:

Create string patterns and derive things from them, such as regexes

83 lines (81 loc) 2.78 kB
import { pipe } from "./chunk-AG3DL6ZS.mjs"; import "./chunk-QXAXOUZS.mjs"; // src/arbitrary-deferred.ts var arbitraryFromAtom = (fc) => (atom) => { switch (atom.kind) { case "anything": return fc.char(); case "character": return fc.constant(atom.char); case "characterClass": return (atom.exclude ? fc.integer({ min: 1, max: 65535 }).filter( (i) => atom.ranges.every(({ lower, upper }) => i > upper || i < lower) ) : fc.oneof( ...atom.ranges.map( ({ lower, upper }) => fc.integer({ min: lower, max: upper }) ) )).map((charCode) => String.fromCharCode(charCode)); case "subgroup": return arbitraryFromPattern(fc)(atom.subpattern); } }; var arbitraryFromQuantifiedAtom = (fc) => (quantifiedAtom) => { switch (quantifiedAtom.kind) { case "star": return fc.array(arbitraryFromAtom(fc)(quantifiedAtom.atom)).map((strs) => strs.join("")); case "plus": return fc.array(arbitraryFromAtom(fc)(quantifiedAtom.atom), { minLength: 1 }).map((strs) => strs.join("")); case "question": return fc.array(arbitraryFromAtom(fc)(quantifiedAtom.atom), { minLength: 0, maxLength: 1 }).map((strs) => strs.join("")); case "exactly": return fc.array(arbitraryFromAtom(fc)(quantifiedAtom.atom), { minLength: quantifiedAtom.count, maxLength: quantifiedAtom.count }).map((strs) => strs.join("")); case "between": return fc.array(arbitraryFromAtom(fc)(quantifiedAtom.atom), { minLength: quantifiedAtom.min, maxLength: quantifiedAtom.max }).map((strs) => strs.join("")); case "minimum": return fc.array(arbitraryFromAtom(fc)(quantifiedAtom.atom), { minLength: quantifiedAtom.min }).map((strs) => strs.join("")); } }; var arbitraryFromTerm = (fc) => (term) => { switch (term.tag) { case "atom": return arbitraryFromAtom(fc)(term); case "quantifiedAtom": return arbitraryFromQuantifiedAtom(fc)(term); } }; var chainConcatAll = (fc) => (fcs) => fcs.length === 0 ? fc.constant("") : fcs[0].chain( (headStr) => chainConcatAll(fc)(fcs.slice(1)).map((tailStr) => headStr + tailStr) ); var arbitraryFromPattern = (fc) => (pattern) => { switch (pattern.tag) { case "atom": return arbitraryFromAtom(fc)(pattern); case "disjunction": return fc.oneof( arbitraryFromPattern(fc)(pattern.left), arbitraryFromPattern(fc)(pattern.right) ); case "quantifiedAtom": return arbitraryFromQuantifiedAtom(fc)(pattern); case "termSequence": return pipe(pattern.terms.map(arbitraryFromTerm(fc)), chainConcatAll(fc)); } }; export { arbitraryFromAtom, arbitraryFromPattern, arbitraryFromQuantifiedAtom };