kuvio
Version:
Create string patterns and derive things from them, such as regexes
59 lines (57 loc) • 2.2 kB
JavaScript
import "./chunk-QXAXOUZS.mjs";
// src/arbitrary.ts
import { match } from "@simspace/matchers";
import * as fc from "fast-check";
import * as RA from "fp-ts/ReadonlyArray";
import { pipe } from "fp-ts/function";
var matchK = match.on("kind").w;
var arbitraryFromAtom = matchK({
anything: () => fc.char(),
character: ({ char: char2 }) => fc.constant(char2),
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(subpattern)
});
var arbitraryFromQuantifiedAtom = matchK({
star: ({ atom }) => fc.array(arbitraryFromAtom(atom)).map((strs) => strs.join("")),
plus: ({ atom }) => fc.array(arbitraryFromAtom(atom), { minLength: 1 }).map((strs) => strs.join("")),
question: ({ atom }) => fc.array(arbitraryFromAtom(atom), {
minLength: 0,
maxLength: 1
}).map((strs) => strs.join("")),
exactly: ({ atom, count }) => fc.array(arbitraryFromAtom(atom), {
minLength: count,
maxLength: count
}).map((strs) => strs.join("")),
between: ({ atom, min, max }) => fc.array(arbitraryFromAtom(atom), {
minLength: min,
maxLength: max
}).map((strs) => strs.join("")),
minimum: ({ atom, min }) => fc.array(arbitraryFromAtom(atom), { minLength: min }).map((strs) => strs.join(""))
});
var arbitraryFromTerm = match({
atom: arbitraryFromAtom,
quantifiedAtom: arbitraryFromQuantifiedAtom
});
var chainConcatAll = RA.foldLeft(
() => fc.constant(""),
(head, tail) => head.chain(
(headStr) => chainConcatAll(tail).map((tailStr) => headStr + tailStr)
)
);
var arbitraryFromPattern = match({
atom: arbitraryFromAtom,
disjunction: ({ left, right }) => fc.oneof(arbitraryFromPattern(left), arbitraryFromPattern(right)),
quantifiedAtom: arbitraryFromQuantifiedAtom,
termSequence: ({ terms }) => pipe(terms.map(arbitraryFromTerm), chainConcatAll)
});
export {
arbitraryFromAtom,
arbitraryFromPattern,
arbitraryFromQuantifiedAtom
};