UNPKG

@synstack/query

Version:
140 lines (136 loc) 3.84 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/query.index.ts var query_index_exports = {}; __export(query_index_exports, { QueryEngine: () => QueryEngine }); module.exports = __toCommonJS(query_index_exports); // src/query.engine.ts var import_v42 = require("zod/v4"); // src/query.lib.ts var import_v4 = require("zod/v4"); function queryPredicate(name, params, handler) { return { name, schema: import_v4.z.object({ [name]: params }), handler // Todo: fix this }; } var alwaysSchema = import_v4.z.object({ always: import_v4.z.literal(true) }).meta({ id: "always" }); var neverSchema = import_v4.z.object({ never: import_v4.z.literal(true) }).meta({ id: "never" }); function querySchema(extras) { const schema = import_v4.z.union([ import_v4.z.object({ get and() { return import_v4.z.array(schema).min(2); } }), import_v4.z.object({ get or() { return import_v4.z.array(schema).min(2); } }), import_v4.z.object({ get not() { return schema; } }), alwaysSchema, neverSchema, ...extras // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents ]); return schema; } function queryApply(predicates, query, input, options) { if (query === void 0) return false; const schema = querySchema(predicates.map((c) => c.schema)); const parsedQuery = options?.skipQueryValidation ? query : schema.parse(query); function apply(query2, input2) { if ("always" in query2) { return true; } if ("never" in query2) { return false; } if ("and" in query2) { if (query2.and.length === 0) return false; return query2.and.every((q) => apply(q, input2)); } if ("or" in query2) { if (query2.or.length === 0) return false; return query2.or.some((q) => apply(q, input2)); } if ("not" in query2) { return !apply(query2.not, input2); } return predicates.some((c) => { if (c.name in query2) return c.handler(query2[c.name])(input2); return false; }); } return apply(parsedQuery, input); } // src/query.engine.ts var QueryEngine = class _QueryEngine { predicates; _schema; constructor(predicates) { this.predicates = predicates; this._schema = querySchema( this.predicates.map((p) => p.schema) ); } static default() { return new _QueryEngine([]); } static addPredicate(name, params, handler) { return new _QueryEngine([ queryPredicate(name, params, handler) ]); } addPredicate(name, params, handler) { return new _QueryEngine([ ...this.predicates, queryPredicate(name, params, handler) ]); } get schema() { return this._schema; } get jsonSchema() { return import_v42.z.toJSONSchema(this.schema); } match(query, input, options) { return queryApply(this.predicates, query, input, options); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { QueryEngine }); //# sourceMappingURL=query.index.cjs.map