UNPKG

kuvio

Version:

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

116 lines (112 loc) 4.08 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/arbitrary-deferred.ts var arbitrary_deferred_exports = {}; __export(arbitrary_deferred_exports, { arbitraryFromAtom: () => arbitraryFromAtom, arbitraryFromPattern: () => arbitraryFromPattern, arbitraryFromQuantifiedAtom: () => arbitraryFromQuantifiedAtom }); module.exports = __toCommonJS(arbitrary_deferred_exports); // src/util/pipe.ts function pipe() { let ret = arguments[0]; for (let i = 1; i < arguments.length; i++) { ret = arguments[i](ret); } return ret; } // 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)); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { arbitraryFromAtom, arbitraryFromPattern, arbitraryFromQuantifiedAtom });