UNPKG

kuvio

Version:

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

116 lines (114 loc) 4.38 kB
"use strict"; 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 fc = __toESM(require("fast-check")); var arbitraryFromAtom = (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(atom.subpattern); } }; var arbitraryFromQuantifiedAtom = (quantifiedAtom) => { switch (quantifiedAtom.kind) { case "star": return fc.array(arbitraryFromAtom(quantifiedAtom.atom)).map((strs) => strs.join("")); case "plus": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: 1 }).map((strs) => strs.join("")); case "question": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: 0, maxLength: 1 }).map((strs) => strs.join("")); case "exactly": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: quantifiedAtom.count, maxLength: quantifiedAtom.count }).map((strs) => strs.join("")); case "between": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: quantifiedAtom.min, maxLength: quantifiedAtom.max }).map((strs) => strs.join("")); case "minimum": return fc.array(arbitraryFromAtom(quantifiedAtom.atom), { minLength: quantifiedAtom.min }).map((strs) => strs.join("")); } }; var arbitraryFromTerm = (term) => { switch (term.tag) { case "atom": return arbitraryFromAtom(term); case "quantifiedAtom": return arbitraryFromQuantifiedAtom(term); } }; var chainConcatAll = (fcs) => fcs.length === 0 ? fc.constant("") : fcs[0].chain( (headStr) => chainConcatAll(fcs.slice(1)).map((tailStr) => headStr + tailStr) ); var arbitraryFromPattern = (pattern) => { switch (pattern.tag) { case "atom": return arbitraryFromAtom(pattern); case "disjunction": return fc.oneof( arbitraryFromPattern(pattern.left), arbitraryFromPattern(pattern.right) ); case "quantifiedAtom": return arbitraryFromQuantifiedAtom(pattern); case "termSequence": return chainConcatAll(pattern.terms.map(arbitraryFromTerm)); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { arbitraryFromAtom, arbitraryFromPattern, arbitraryFromQuantifiedAtom });