UNPKG

kuvio

Version:

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

58 lines (56 loc) 2.27 kB
import "./chunk-QXAXOUZS.mjs"; // src/arbitrary-deferred.ts import { match } from "@simspace/matchers"; import * as RA from "fp-ts/ReadonlyArray"; import { pipe } from "fp-ts/function"; var matchK = match.on("kind").w; var arbitraryFromAtom = (fc) => matchK({ anything: () => fc.char(), character: ({ char }) => fc.constant(char), characterClass: ({ exclude, ranges }) => (exclude ? fc.integer({ min: 1, max: 65535 }).filter( (i) => ranges.every(({ lower, upper }) => i > upper || i < lower) ) : fc.oneof( ...ranges.map( ({ lower, upper }) => fc.integer({ min: lower, max: upper }) ) )).map((charCode) => String.fromCharCode(charCode)), subgroup: ({ subpattern }) => arbitraryFromPattern(fc)(subpattern) }); var arbitraryFromQuantifiedAtom = (fc) => matchK({ star: ({ atom }) => fc.array(arbitraryFromAtom(fc)(atom)).map((strs) => strs.join("")), plus: ({ atom }) => fc.array(arbitraryFromAtom(fc)(atom), { minLength: 1 }).map((strs) => strs.join("")), question: ({ atom }) => fc.array(arbitraryFromAtom(fc)(atom), { minLength: 0, maxLength: 1 }).map((strs) => strs.join("")), exactly: ({ atom, count }) => fc.array(arbitraryFromAtom(fc)(atom), { minLength: count, maxLength: count }).map((strs) => strs.join("")), between: ({ atom, min, max }) => fc.array(arbitraryFromAtom(fc)(atom), { minLength: min, maxLength: max }).map((strs) => strs.join("")), minimum: ({ atom, min }) => fc.array(arbitraryFromAtom(fc)(atom), { minLength: min }).map((strs) => strs.join("")) }); var arbitraryFromTerm = (fc) => match({ atom: arbitraryFromAtom(fc), quantifiedAtom: arbitraryFromQuantifiedAtom(fc) }); var chainConcatAll = (fc) => RA.foldLeft( () => fc.constant(""), (head, tail) => head.chain( (headStr) => chainConcatAll(fc)(tail).map((tailStr) => headStr + tailStr) ) ); var arbitraryFromPattern = (fc) => match({ atom: arbitraryFromAtom(fc), disjunction: ({ left, right }) => fc.oneof(arbitraryFromPattern(fc)(left), arbitraryFromPattern(fc)(right)), quantifiedAtom: arbitraryFromQuantifiedAtom(fc), termSequence: ({ terms }) => pipe(terms.map(arbitraryFromTerm(fc)), chainConcatAll(fc)) }); export { arbitraryFromAtom, arbitraryFromPattern, arbitraryFromQuantifiedAtom };