kuvio
Version:
Create string patterns and derive things from them, such as regexes
94 lines (92 loc) • 4.03 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/arbitrary.ts
var arbitrary_exports = {};
__export(arbitrary_exports, {
arbitraryFromAtom: () => arbitraryFromAtom,
arbitraryFromPattern: () => arbitraryFromPattern,
arbitraryFromQuantifiedAtom: () => arbitraryFromQuantifiedAtom
});
module.exports = __toCommonJS(arbitrary_exports);
var import_matchers = require("@simspace/matchers");
var fc = __toESM(require("fast-check"));
var RA = __toESM(require("fp-ts/ReadonlyArray"));
var import_function = require("fp-ts/function");
var matchK = import_matchers.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 = (0, import_matchers.match)({
atom: arbitraryFromAtom,
quantifiedAtom: arbitraryFromQuantifiedAtom
});
var chainConcatAll = RA.foldLeft(
() => fc.constant(""),
(head, tail) => head.chain(
(headStr) => chainConcatAll(tail).map((tailStr) => headStr + tailStr)
)
);
var arbitraryFromPattern = (0, import_matchers.match)({
atom: arbitraryFromAtom,
disjunction: ({ left, right }) => fc.oneof(arbitraryFromPattern(left), arbitraryFromPattern(right)),
quantifiedAtom: arbitraryFromQuantifiedAtom,
termSequence: ({ terms }) => (0, import_function.pipe)(terms.map(arbitraryFromTerm), chainConcatAll)
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
arbitraryFromAtom,
arbitraryFromPattern,
arbitraryFromQuantifiedAtom
});