fig-tree-evaluator 2.23.2 → 3.0.0-preview.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ interface Registry {
2
+ getOperators(): readonly {
3
+ name: string;
4
+ alias?: string;
5
+ positionalParams?: readonly string[];
6
+ restParam: string | null;
7
+ }[];
8
+ getFragments(): readonly {
9
+ name: string;
10
+ }[];
11
+ }
12
+ type Spelling = 'preserve' | 'canonical' | 'alias';
13
+ interface NameOptions {
14
+ referenceNames?: Spelling;
15
+ }
16
+ interface CanonicalOptions extends NameOptions {
17
+ operatorNames?: Spelling;
18
+ referencesAsGet?: boolean;
19
+ }
20
+ interface ShorthandOptions extends NameOptions {
21
+ operatorNames?: Spelling;
22
+ arguments?: 'positional' | 'named';
23
+ getAsReference?: boolean;
24
+ }
25
+
26
+ type ObjectClass = {
27
+ kind: 'operator';
28
+ } | {
29
+ kind: 'fragment';
30
+ } | {
31
+ kind: 'shorthand';
32
+ key: string;
33
+ } | {
34
+ kind: 'plain';
35
+ } | {
36
+ kind: 'malformed';
37
+ message: string;
38
+ };
39
+ declare const classifyObject: (raw: Record<string, unknown>, recognizes: (name: string) => boolean) => ObjectClass;
40
+ interface PositionalShape {
41
+ positionalParams?: readonly string[];
42
+ restParam: string | null;
43
+ }
44
+ interface PositionalLayout {
45
+ bound: number;
46
+ restAt: number | null;
47
+ }
48
+ declare const positionalLayout: (shape: PositionalShape, length: number) => PositionalLayout | null;
49
+ declare const singlePositionalTarget: (shape: PositionalShape) => string | null;
50
+
51
+ declare const WILDCARD: unique symbol;
52
+ type Wildcard = typeof WILDCARD;
53
+ type PathSegment = string | number | Wildcard;
54
+
55
+ declare const NAMESPACE_TOKENS: {
56
+ readonly data: "data";
57
+ readonly d: "data";
58
+ readonly vars: "vars";
59
+ readonly v: "vars";
60
+ readonly params: "params";
61
+ readonly p: "params";
62
+ readonly element: "element";
63
+ readonly e: "element";
64
+ readonly index: "index";
65
+ readonly i: "index";
66
+ readonly error: "error";
67
+ readonly err: "error";
68
+ };
69
+ type ReferenceNamespace = (typeof NAMESPACE_TOKENS)[keyof typeof NAMESPACE_TOKENS];
70
+ type ReferenceRecognition = {
71
+ kind: 'reference';
72
+ namespace: ReferenceNamespace;
73
+ segments: PathSegment[];
74
+ drill: string;
75
+ binding?: string;
76
+ } | {
77
+ kind: 'invalid';
78
+ namespace: ReferenceNamespace;
79
+ reason: string;
80
+ code?: string;
81
+ } | {
82
+ kind: 'unrecognized';
83
+ } | {
84
+ kind: 'plain';
85
+ };
86
+ interface ReferenceScope {
87
+ bindings?: readonly string[];
88
+ }
89
+ declare const recognizeReference: (value: string, scope?: ReferenceScope) => ReferenceRecognition;
90
+
91
+ type BasicType = 'any' | 'string' | 'number' | 'boolean' | 'array' | 'object' | 'null' | 'integer';
92
+ type LiteralType = {
93
+ literal: readonly (string | number | boolean)[];
94
+ };
95
+ type ExpectedType = BasicType | LiteralType | readonly BasicType[];
96
+
97
+ declare const typesIntersect: (a: ExpectedType, b: ExpectedType) => boolean;
98
+
99
+ declare const toCanonical: (expression: unknown, fig: Registry, options?: CanonicalOptions) => unknown;
100
+ declare const toShorthand: (expression: unknown, fig: Registry, options?: ShorthandOptions) => unknown;
101
+ declare const toGet: (reference: unknown, options?: NameOptions) => Record<string, unknown> | null;
102
+ declare const toReference: (node: unknown, options?: NameOptions) => string | null;
103
+
104
+ export { classifyObject, positionalLayout, recognizeReference, singlePositionalTarget, toCanonical, toGet, toReference, toShorthand, typesIntersect };
@@ -0,0 +1 @@
1
+ import{d as e,E as r,p as n,S as a,e as s,f as o,R as i,s as l,g as u,h as f,G as p,j as c,k as m,W as d,l as h,N as k,F as g,D as v}from"../chunks/shared.js";export{t as typesIntersect}from"../chunks/shared.js";const y=e=>{const r=new Map;for(const{name:t,alias:n,positionalParams:a,restParam:s}of e.getOperators()){const e={name:t,alias:n,positionalParams:a,restParam:s};r.set(t,e),void 0!==n&&r.set(n,e)}const t=new Set(e.getFragments().map(({name:e})=>e));return{operators:r,fragments:t,recognizes:e=>"literal"===e||r.has(e)||t.has(e)}},$=(e,r)=>{const t=n(e,r.length);if(null===t)return null;const a=e.positionalParams,s=[];for(let e=0;e<t.bound;e++)s.push([a[e],r[e]]);return null!==t.restAt&&s.push([e.restParam,0===t.restAt?r:r.slice(t.restAt)]),s},w=/*#__PURE__*/new Set(["fallback","noCache","vars"]),P="'noCache' takes only the literal true — it is settled before the subtree runs",N=(e,t,n)=>{const a=e.operator;if("string"!=typeof a)return n(r.malformedNode,"the 'operator' value must be a literal string");if("literal"===a)return A(e,n);const s=t.operators.get(a);if(void 0===s)return n(r.unknownOperator,`'${a}' names no registered operator`);const o=[],i=[];for(const t in e){const a=e[t];if(void 0!==a)if("operator"===t)o.push({kind:"invocation"});else{if("noCache"===t&&!0!==a)return n(r.malformedNode,P);if("//"===t||w.has(t))o.push({kind:"keep",key:t,value:a});else{if("parameters"===t)return n(r.malformedNode,"'parameters' is reserved and unused on operator nodes");o.push({kind:"param",name:t,value:a}),i.push([t,a])}}}return{kind:"operator",operator:s,spelling:a,slots:o,params:i,fromPayload:!1}},A=(e,t)=>{if(!("value"in e))return t(r.malformedNode,"'literal' requires its content in 'value'");const n=[];for(const a in e){const s=e[a];if("operator"===a)n.push({kind:"invocation"});else if("value"===a)n.push({kind:"param",name:"value",value:s});else{if(void 0===s)continue;if("//"!==a&&!w.has(a))return t(r.unknownNodeKey,`'${a}' is not a key of 'literal' — content goes in 'value'`);n.push({kind:"keep",key:a,value:s})}}return{kind:"literal",content:e.value,slots:n,fromPayload:!1}},b=(e,t,n)=>{const a=e.fragment;if("string"!=typeof a)return n(r.malformedNode,"the 'fragment' value must be a literal string");if(!t.fragments.has(a))return n(r.unknownFragment,`'${a}' names no registered fragment`);const s=[];for(const a in e){const o=e[a];if(void 0!==o)if("fragment"===a)s.push({kind:"invocation"});else if("parameters"===a)j(o,t,n),s.push({kind:"param",name:a,value:o});else{if("noCache"===a&&!0!==o)return n(r.malformedNode,P);if("//"!==a&&!w.has(a))return n(r.unknownNodeKey,`'${a}' is not a key of a fragment call — arguments live only in 'parameters'`);s.push({kind:"keep",key:a,value:o})}}return{kind:"fragment",name:a,slots:s,parameters:e.parameters,fromPayload:!1}},j=(e,t,n)=>{s(e)||o(e,t.recognizes)||"string"==typeof e&&"reference"===u(e).kind||n(r.malformedNode,"a fragment's 'parameters' must be a named-arguments object or a node computing one")},z=(e,t,n,o)=>{const i=t.slice(1),l="literal"===i,u=!l&&n.fragments.has(i),f=[];for(const n in e){const s=e[n];if(n!==t){if(!a.has(n))return o(r.malformedNode,`'${n}' may not sit beside the shorthand key '${t}' — reserved modifiers only`);if("noCache"===n&&!0!==s&&!l)return o(r.malformedNode,P);void 0!==s&&f.push({kind:"keep",key:n,value:s})}else f.push({kind:"invocation"})}const p=e[t];if(l)return{kind:"literal",content:p,slots:f,fromPayload:!0};if(u)return s(p)?{kind:"fragment",name:i,slots:f,parameters:p,fromPayload:!0}:o(r.malformedNode,`fragments have no single-value or positional form — '${t}' takes a named-arguments object`);const c=n.operators.get(i);return{kind:"operator",operator:c,spelling:i,slots:f,params:C(c,p,n,o),fromPayload:!0}},C=(e,t,n,a)=>{const u=()=>a(r.positionalArity,`'${e.name}' takes no positional arguments — use the named form`);if(Array.isArray(t)){const n=$(e,t);if(null!==n)return n;if(void 0===e.positionalParams)return u();const s=e.positionalParams;return a(r.positionalArity,`'${e.name}' takes at most ${s.length} positional argument${1===s.length?"":"s"} (${s.join(", ")}), got ${t.length}`)}if(s(t)&&!o(t,n.recognizes)){const n=[];for(const s in t){const o=t[s];if(void 0!==o){if("//"!==s&&i.has(s))return a(r.unknownNodeKey,`'${s}' is not a parameter of '${e.name}'`);n.push([s,o])}}return n}const f=l(e);return null===f?u():[[f,t]]},W={positionalParams:p,restParam:null},x=/*#__PURE__*/new Set(["path","from","default"]),M=(e,r,t)=>"canonical"===t?r:"alias"===t?(e=>{for(const[r,t]of Object.entries(k))if(t===e&&r!==e)return r;return e})(r):e,O=(e,r)=>{if("preserve"===r)return e;const t=u(e);if("reference"!==t.kind)return e;const{token:n,rest:a}=f(e);return`$${M(n,t.namespace,r)}${a}`},S=e=>"index"!==e&&"error"!==e,F=(e,r)=>{if("string"!=typeof e)return null;const t=u(e);if("reference"!==t.kind)return null;const{namespace:n,segments:a}=t;if(!S(n))return null;const{token:s,rest:o}=f(e),i=(l=o).startsWith(".")?l.slice(1):l;var l;return"data"===n?{operator:"get",path:i}:"element"!==n&&a.length>0&&"string"!=typeof a[0]?null:{operator:"get",path:i,from:`$${M(s,n,r)}`}},K=(e,r)=>{if(e.has("default"))return null;const t=(e=>{if("string"==typeof e){if(e.startsWith("$"))return null;try{return h(e)}catch{return null}}if(!Array.isArray(e))return null;for(const r of e)if(!("string"==typeof r&&!r.startsWith("$")||"number"==typeof r&&Number.isInteger(r)&&r>=0))return null;return e})(e.get("path"));if(null===t)return null;const n=c(t);let a;const s=e.get("from");if(void 0===s)a="canonical"===r?"$data":"$d";else{if("string"!=typeof s)return null;const e=m(s);if("vars"===e||"params"===e){if(0===t.length?"vars"===e:"string"!=typeof t[0])return null;a=`$${M(f(s).token,e,r)}`}else{const e=u(s);if("reference"!==e.kind||!S(e.namespace))return null;if(""!==n&&e.segments.includes(d))return null;a=O(s,r).replace(/\.$/,"")}}return""===n?a:n.startsWith("[")?`${a}${n}`:`${a}.${n}`},E=e=>{const r=[];for(let t=e;null!==t;t=t.parent)r.push(t.key);return r.reverse()};class G{lookup;writer;path=null;depth=0;constructor(e,r){this.lookup=e,this.writer=r}fail=(e,r)=>{throw new g({code:e,message:r,path:E(this.path)})};child(e,r){return this.within(r,()=>this.value(e))}within(e,r){const{path:t,depth:n}=this;this.path={parent:t,key:e},this.depth=n+1;const a=r();return this.path=t,this.depth=n,a}value(t){if(this.depth>v&&this.fail(r.depthCeiling,`the expression nests deeper than the engine's ceiling of ${v} levels`),"string"==typeof t)return this.writer.string(t);if(Array.isArray(t))return t.map((e,r)=>this.child(e,r));if(!s(t))return t;const n=((t,n,a)=>{const s=e(t,n.recognizes);switch(s.kind){case"malformed":return a(r.malformedNode,s.message);case"plain":return s;case"operator":return N(t,n,a);case"fragment":return b(t,n,a);case"shorthand":return z(t,s.key,n,a)}})(t,this.lookup,this.fail);switch(n.kind){case"plain":return this.entries(t,!0);case"operator":return this.writer.operator(n,this);case"fragment":return this.writer.fragment(n,this);case"literal":return this.writer.literal(n,this)}}keep({key:e,value:r}){return"fallback"===e?this.child(r,e):"vars"===e?this.vars(r):r}parameters(e){return!s(e)||o(e,this.lookup.recognizes)?this.child(e,"parameters"):this.within("parameters",()=>this.entries(e,!1))}entries(e,r){const t={};for(const n in e){const a=e[n];t[n]="//"===n?a:r&&"vars"===n?this.vars(a):this.child(a,n)}return t}vars(e){return s(e)?this.within("vars",()=>this.entries(e,!1)):e}}const I=(e,r)=>"canonical"===r?e.operator.name:"alias"===r?e.operator.alias??e.operator.name:e.spelling,R=e=>Array.isArray(e)?e:[e],q=(e,r)=>[...R(e),...R(r)],D=e=>e.params.find(([e])=>"//"===e)?.[1],B=e=>e.slots.some(e=>"keep"===e.kind&&"//"===e.key),H={kind:"named"},J=(e,r)=>!(Array.isArray(e)||s(e)&&!o(e,r.recognizes)),L=(e={})=>{const r=e.operatorNames??"preserve",t=e.referenceNames??"preserve",n="named"===e.arguments,a=e.getAsReference??!0;return{operator:(e,s)=>{const o=e.params.map(([e,r])=>[e,"//"===e?r:s.child(r,e)]),i=o.filter(([e])=>"//"!==e);if(a&&"get"===e.operator.name){const r=e.slots.some(e=>"keep"===e.kind)||e.params.some(([e])=>"//"===e)?null:K(new Map(i),t);if(null!==r)return r}const l=n?H:((e,r,t)=>{const n=e.positionalParams;if(void 0===n||0===r.length)return H;const a=e.restParam,s=null===a?n:n.slice(0,-1),o=new Map(r);for(const e of o.keys())if(e!==a&&!s.includes(e))return H;let i=0;for(;i<s.length&&o.has(s[i]);)i++;for(let e=i;e<s.length;e++)if(o.has(s[e]))return H;const l=null!==a&&o.has(a);if(null!==a&&l!==(i===s.length))return H;const u=s.slice(0,i).map(e=>o.get(e));if(l){const e=o.get(a);return Array.isArray(e)?{kind:"array",value:[...u,...e]}:0===s.length&&J(e,t)?{kind:"single",value:e}:H}return 1===i&&J(u[0],t)?{kind:"single",value:u[0]}:{kind:"array",value:u}})(e.operator,i,s.lookup),u="named"===l.kind?void 0:D(e),f=void 0!==u&&B(e),p={};for(const t of e.slots)"invocation"===t.kind?(void 0===u||f||(p["//"]=u),p[`$${I(e,r)}`]="named"===l.kind?Object.fromEntries(o):l.value):"keep"===t.kind&&(p[t.key]="//"===t.key&&f?q(t.value,u):s.keep(t));return p},fragment:(e,r)=>{const t=void 0===e.parameters?{}:r.parameters(e.parameters),n="string"==typeof t,a={};for(const s of e.slots)"invocation"===s.kind?n?(a.fragment=e.name,e.fromPayload&&(a.parameters=t)):a[`$${e.name}`]=t:"param"===s.kind?n&&(a.parameters=t):a[s.key]=r.keep(s);return a},literal:(e,r)=>{const t={};for(const n of e.slots)"invocation"===n.kind?t.$literal=e.content:"keep"===n.kind&&(t[n.key]=r.keep(n));return t},string:e=>O(e,t)}},Q=(e,r,t)=>new G(y(r),((e={})=>{const r=e.operatorNames??"preserve",t=e.referenceNames??"preserve",n=e.referencesAsGet??!1;return{operator:(e,t)=>{const n={},a=e.fromPayload?D(e):void 0,s=void 0!==a&&B(e);for(const o of e.slots)if("invocation"===o.kind){if(void 0===a||s||(n["//"]=a),n.operator=I(e,r),e.fromPayload)for(const[r,a]of e.params)"//"!==r&&(n[r]=t.child(a,r))}else"param"===o.kind?n[o.name]=t.child(o.value,o.name):"//"===o.key&&s?n["//"]=q(o.value,a):n[o.key]=t.keep(o);return n},fragment:(e,r)=>{const t={};for(const n of e.slots)"invocation"===n.kind?(t.fragment=e.name,e.fromPayload&&(t.parameters=r.parameters(e.parameters))):"param"===n.kind?t.parameters=r.parameters(n.value):t[n.key]=r.keep(n);return t},literal:(e,r)=>{const t={};for(const n of e.slots)"invocation"===n.kind?(t.operator="literal",e.fromPayload&&(t.value=e.content)):"param"===n.kind?t.value=e.content:t[n.key]=r.keep(n);return t},string:e=>{if(n){const r=F(e,t);if(null!==r)return r}return O(e,t)}}})(t)).value(e),T=(e,r,t)=>new G(y(r),L(t)).value(e),U=(e,r)=>F(e,r?.referenceNames??"preserve"),V=(e,r)=>{const t=(e=>{if(!s(e))return null;const r=new Map;if("operator"in e){if("get"!==e.operator)return null;for(const t in e){const n=e[t];if("operator"!==t&&"//"!==t&&void 0!==n){if(!x.has(t))return null;r.set(t,n)}}return r}for(const r in e)if("$get"!==r&&"//"!==r)return null;if(!("$get"in e))return null;const t=e.$get;if(Array.isArray(t)){const e=$(W,t);if(null===e)return null;for(const[t,n]of e)r.set(t,n);return r}if(s(t)){for(const e in t){const n=t[e];if("//"!==e&&void 0!==n){if(!x.has(e))return null;r.set(e,n)}}return r}return r.set(l(W),t),r})(e);return null===t?null:K(t,r?.referenceNames??"preserve")};export{e as classifyObject,n as positionalLayout,u as recognizeReference,l as singlePositionalTarget,Q as toCanonical,U as toGet,V as toReference,T as toShorthand};