fig-tree-evaluator 2.23.2 → 3.0.0-preview.2
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.
- package/README.md +214 -148
- package/build/chunks/shared.js +1 -0
- package/build/editor-hints/index.d.ts +35 -0
- package/build/editor-hints/index.js +1 -0
- package/build/format/index.d.ts +86 -0
- package/build/format/index.js +1 -0
- package/build/index.d.ts +783 -240
- package/build/index.js +1 -0
- package/build/migrate/index.d.ts +55 -0
- package/build/migrate/index.js +1 -0
- package/package.json +97 -50
- package/CHANGELOG.md +0 -134
- package/build/index.cjs.js +0 -1
- package/build/index.esm.js +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={unknownOperator:"unknown-operator",typeCheck:"type-check",operatorFailure:"operator-failure",timeout:"timeout",requestTimeout:"request-timeout",aborted:"aborted",unknownNodeKey:"unknown-node-key",unresolvedVar:"unresolved-var",unrecognizedIdentifier:"unrecognized-identifier",depthCeiling:"depth-ceiling",nonFiniteResult:"non-finite-result",escapedHandle:"escaped-handle",emptyAggregate:"empty-aggregate",malformedNode:"malformed-node",unknownFragment:"unknown-fragment",positionalArity:"positional-arity",invalidVars:"invalid-vars",invalidReference:"invalid-reference",uselessModifier:"useless-modifier",unreferencedVar:"unreferenced-var",missingRequired:"missing-required",unresolvedParam:"unresolved-param",unresolvedBinding:"unresolved-binding",deadBinding:"dead-binding",invalidAs:"invalid-as",maxDepthExceeded:"max-depth",maxNodesExceeded:"max-nodes",returnsMismatch:"returns-mismatch",operatorValidate:"operator-validate",missingDataPath:"missing-data-path",shadowedVar:"shadowed-var",varCycle:"var-cycle",bareVars:"bare-vars",fragmentCycle:"fragment-cycle",unboundToken:"unbound-token",unusedSubstitution:"unused-substitution",tokenRenumber:"token-renumber",inertReferenceToken:"inert-reference-token",invalidDefinition:"invalid-definition",invalidName:"invalid-name",reservedName:"reserved-name",invalidNullPolicy:"invalid-null-policy",duplicateOperator:"duplicate-operator",invalidOptions:"invalid-options"},t=Symbol("figtree.path.wildcard"),n=e=>"string"==typeof e||"number"==typeof e||e===t,r={found:!1,value:void 0},a=e=>{const t=[];let n="",r=!1,a=0;const i=()=>{r&&(t.push(n),n="",r=!1)};for(;a<e.length;){const o=e[a];if("."!==o){if("["===o){i();const{segment:n,next:r}=s(e,a);t.push(n),a=r;continue}n+=o,r=!0,a++}else i(),a++}return i(),t},s=(e,n)=>{let r=n+1;if(r>=e.length)throw i(e,'unterminated "["');const a=e[r];if("*"===a){if("]"!==e[r+1])throw i(e,'expected "]" after "[*"');return{segment:t,next:r+2}}if('"'===a||"'"===a){const t=a;r++;let n="";for(;r<e.length;){const a=e[r];if("\\"===a){const t=e[r+1];if(void 0===t)throw i(e,"trailing escape in quoted key");n+=t,r+=2;continue}if(a===t){if("]"!==e[r+1])throw i(e,'expected "]" after quoted key');return{segment:n,next:r+2}}n+=a,r++}throw i(e,"unterminated quoted key")}let s="";for(;r<e.length&&e[r]>="0"&&e[r]<="9";)s+=e[r],r++;if(s.length>0&&"]"===e[r])return{segment:Number(s),next:r+1};throw i(e,'expected a number, "*", or a quoted key inside "[…]"')},i=(e,t)=>new Error(`Invalid path "${e}": ${t}`),o=(e,t)=>l(e,(e=>Array.isArray(e)?e.slice():a(e))(t),0),l=(e,n,a)=>{if(a>=n.length)return{found:!0,value:e};const s=n[a];if(s===t){if(!Array.isArray(e))return r;const t=a+1;if(t>=n.length)return{found:!0,value:e};return{found:!0,value:e.map(e=>{const r=l(e,n,t);return r.found?r.value:null})}}const i=d(e,s);return i.found?l(i.value,n,a+1):r},d=(e,t)=>{if(null===e||"object"!=typeof e)return r;if(Array.isArray(e)){const n="number"==typeof t?t:u(t);return void 0===n||n<0||n>=e.length?r:{found:!0,value:e[n]}}const n=String(t);return Object.prototype.propertyIsEnumerable.call(e,n)?{found:!0,value:e[n]}:r},u=e=>/^\d+$/.test(e)?Number(e):void 0,c=e=>e.map(e=>"string"==typeof e?u(e)??e:e),m={data:"data",d:"data",vars:"vars",v:"vars",params:"params",p:"params",element:"element",e:"element",index:"index",i:"index"},f=e=>{if(!e.startsWith("$"))return null;const t=/^\$([^.[]*)/.exec(e)[1];return{token:t,rest:e.slice(1+t.length)}},h=e=>""===e?[]:a(e.startsWith(".")?e.slice(1):e),p=t=>{const n=f(t);if(null===n)return{kind:"plain"};const{token:r,rest:a}=n,s=m[r];if(void 0===s)return{kind:"unrecognized"};if(""===a)return"vars"===s?{kind:"invalid",namespace:s,code:e.bareVars,reason:"'$vars' must name a var — there is no whole-scope value"}:{kind:"reference",namespace:s,segments:[],drill:a};if("index"===s)return{kind:"invalid",namespace:s,reason:"'$index' is bare-only — it cannot be drilled"};try{return{kind:"reference",namespace:s,segments:h(a),drill:a}}catch(e){return{kind:"invalid",namespace:s,reason:e.message}}},g=e=>e.map((e,n)=>"number"==typeof e?`[${e}]`:e===t?"[*]":y(e,0===n)).join(""),v=/[.[\]"\\]/,y=(e,t)=>""===e||v.test(e)?`["${e.replace(/[\\"]/g,e=>`\\${e}`)}"]`:t?e:`.${e}`,b=/^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/,k=e=>b.test(e),$=(e,t)=>{const n=g(t);return""===n?`$${e}`:n.startsWith("[")?`$${e}${n}`:`$${e}.${n}`},w=e=>$("data",e),x=()=>{},A=e=>"object"==typeof e&&null!==e&&"function"==typeof e.then,P=e=>"object"==typeof e&&null!==e&&!Array.isArray(e),j=e=>{if("object"!=typeof e||null===e||Array.isArray(e))return!1;const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t},D=(e,t)=>{if(e===t)return 0;const n=e.length+1,r=t.length+1;let a=Array.from({length:r},(e,t)=>t);for(let s=1;s<n;s++){const n=[s];for(let i=1;i<r;i++){const r=a[i-1]+(e[s-1]===t[i-1]?0:1);n.push(Math.min(a[i]+1,n[i-1]+1,r))}a=n}return a[r-1]},N=(e,t)=>{let n,r=3;for(const a of t){const t=D(e,a);t<r&&(n=a,r=t)}return n},S=e=>{let t=2166136261;for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t=Math.imul(t,16777619)>>>0;return t.toString(16).padStart(8,"0")},q=e=>{let t;return()=>(void 0===t&&(t=(async()=>e())()),t)},O=/*#__PURE__*/new Set(["fallback","useCache","vars","//"]),W=500,C={kind:"operator"},E={kind:"fragment"},V={kind:"plain"},z=e=>({kind:"malformed",message:e}),R=(e,t)=>{const n="operator"in e,r="fragment"in e,a=[];for(const n in e)n.startsWith("$")&&t(n.slice(1))&&a.push(n);return n&&r?z("'operator' and 'fragment' may not share a node"):(n||r)&&a.length>0?z(`a canonical '${n?"operator":"fragment"}' key may not sit beside the shorthand key '${a[0]}'`):n?C:r?E:a.length>=2?z(`one node, one invocation: found ${a.map(e=>`'${e}'`).join(" and ")}`):1===a.length?{kind:"shorthand",key:a[0]}:V},F=(e,t)=>{if(!j(e))return!1;if("operator"in e||"fragment"in e)return!0;for(const n in e)if(n.startsWith("$")&&t(n.slice(1)))return!0;return!1},M=(e,t)=>{const n=e.positionalParams;if(void 0===n)return null;const r=e.restParam,a=null===r?n.length:n.length-1;return t>a&&null===r?null:{bound:Math.min(t,a),restAt:null!==r&&t>=a?a:null}},I=e=>{const t=e.positionalParams?.[0];return void 0===t?null:t.startsWith("...")?e.restParam:t},T=["path","default"],B=/*#__PURE__*/new Set(["operator","fragment","parameters","fallback","useCache","vars","//"]),Z=/*#__PURE__*/new Set([...B,"data","params","element","index","d","v","p","e","i","literal"]),_=e=>"string"!=typeof e?{ok:!1,reason:"a name must be a string"}:""===e?{ok:!1,reason:"a name must be non-empty"}:e.startsWith("$")?{ok:!1,reason:"a name may not start with '$'"}:/[.[\]]/.test(e)?{ok:!1,reason:"a name may not contain '.', '[' or ']'"}:{ok:!0};class G extends Error{code;path;holePath;operator;fragment;fragmentPath;errorData;related;cause;issues;trace;constructor(e){super(e.message),this.name="FigTreeError",this.code=e.code,this.path=e.path,void 0!==e.holePath&&(this.holePath=e.holePath),void 0!==e.operator&&(this.operator=e.operator),void 0!==e.fragment&&(this.fragment=e.fragment),void 0!==e.fragmentPath&&(this.fragmentPath=e.fragmentPath),void 0!==e.errorData&&(this.errorData=e.errorData),void 0!==e.related&&(this.related=e.related),void 0!==e.cause&&(this.cause=e.cause),void 0!==e.issues&&(this.issues=e.issues),void 0!==e.trace&&(this.trace=e.trace)}prettyPrint(){const e=this.operator?` (operator: ${this.operator})`:"",t=this.path.length>0?` at ${J(this.path)}`:"",n=`${this.code}${e}${t}`,r=this.errorData&&Object.keys(this.errorData).length>0?`\n${JSON.stringify(this.errorData,null,2)}`:"";return`${n}\n${this.message}${r}`}}const H=e=>e instanceof G,J=e=>e.map((e,t)=>"number"==typeof e?`[${e}]`:0===t?e:`.${e}`).join(""),K=e=>"object"==typeof e&&null!==e&&!Array.isArray(e)&&"literal"in e,L=(e,t)=>{const n=Q(e),r=Q(t);return!(!n.any&&!r.any)||(void 0!==n.literals&&void 0!==r.literals?n.literals.some(e=>r.literals.includes(e)):void 0!==n.literals?n.literals.some(e=>U(e,r.basics)):void 0!==r.literals?r.literals.some(e=>U(e,n.basics)):n.basics.some(e=>r.basics.includes(e)||"integer"===e&&r.basics.includes("number")||"number"===e&&r.basics.includes("integer")))},Q=e=>{if(K(e))return{any:!1,basics:[],literals:e.literal};const t="string"==typeof e?[e]:e;return{any:t.includes("any"),basics:t}},U=(e,t)=>"string"==typeof e?t.includes("string"):"boolean"==typeof e?t.includes("boolean"):t.includes("number")||Number.isInteger(e)&&t.includes("integer");export{$ as A,W as D,e as E,G as F,T as G,B as R,O as S,t as W,F as a,f as b,R as c,g as d,a as e,P as f,K as g,_ as h,j as i,k as j,c as k,h as l,n as m,N as n,o,M as p,Z as q,p as r,I as s,L as t,x as u,H as v,q as w,A as x,w as y,S as z};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
type BasicType = 'any' | 'string' | 'number' | 'boolean' | 'array' | 'object' | 'null' | 'integer';
|
|
2
|
+
|
|
3
|
+
declare const OPERATOR_CATEGORIES: readonly ["logic", "comparison", "math", "string", "array", "data", "io", "other"];
|
|
4
|
+
type OperatorCategory = (typeof OPERATOR_CATEGORIES)[number];
|
|
5
|
+
|
|
6
|
+
interface OperatorHints {
|
|
7
|
+
displayName: string;
|
|
8
|
+
docUrl: string;
|
|
9
|
+
backgroundColor: string;
|
|
10
|
+
textColor: string;
|
|
11
|
+
seeds?: {
|
|
12
|
+
[parameter: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
interface CategoryHints {
|
|
16
|
+
displayName: string;
|
|
17
|
+
order: number;
|
|
18
|
+
backgroundColor: string;
|
|
19
|
+
textColor: string;
|
|
20
|
+
}
|
|
21
|
+
type OperatorHintMap = {
|
|
22
|
+
[operator: string]: OperatorHints;
|
|
23
|
+
};
|
|
24
|
+
type CategoryHintMap = {
|
|
25
|
+
[category in OperatorCategory]: CategoryHints;
|
|
26
|
+
};
|
|
27
|
+
type TypeSeeds = {
|
|
28
|
+
[type in BasicType]: unknown;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare const categoryHints: CategoryHintMap;
|
|
32
|
+
declare const typeSeeds: TypeSeeds;
|
|
33
|
+
declare const operatorHints: OperatorHintMap;
|
|
34
|
+
|
|
35
|
+
export { categoryHints, operatorHints, typeSeeds };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={logic:{displayName:"Logic & control",order:0,backgroundColor:"#632fbc",textColor:"#ffffff"},comparison:{displayName:"Comparison",order:1,backgroundColor:"#247f7c",textColor:"#ffffff"},math:{displayName:"Arithmetic & math",order:2,backgroundColor:"#2b8237",textColor:"#ffffff"},string:{displayName:"Strings",order:3,backgroundColor:"#a1660c",textColor:"#ffffff"},array:{displayName:"Arrays & iteration",order:4,backgroundColor:"#236ac7",textColor:"#ffffff"},data:{displayName:"Data & objects",order:5,backgroundColor:"#b93171",textColor:"#ffffff"},io:{displayName:"I/O",order:6,backgroundColor:"#6e7a24",textColor:"#ffffff"},other:{displayName:"Other",order:7,backgroundColor:"#606e8a",textColor:"#ffffff"}},o={any:"Replace me",string:"Replace me",number:1,integer:1,boolean:!0,array:[],object:{},null:null},a="https://github.com/CarlosNZ/fig-tree-evaluator",l={and:{displayName:"Logical AND",docUrl:a,backgroundColor:"#d6d0f0",textColor:"#26183f",seeds:{values:[!0,!0]}},or:{displayName:"Logical OR",docUrl:a,backgroundColor:"#cabfeb",textColor:"#26183f",seeds:{values:[!0,!1]}},not:{displayName:"Logical NOT (!)",docUrl:a,backgroundColor:"#c0aee5",textColor:"#26183f",seeds:{value:!0}},if:{displayName:"Conditional (?)",docUrl:a,backgroundColor:"#ddd0f0",textColor:"#26183f",seeds:{condition:!0,then:"The condition is true",else:"The condition is false"}},match:{displayName:"Match",docUrl:a,backgroundColor:"#d4bfeb",textColor:"#26183f",seeds:{value:"matchMe",branches:{matchMe:"YES",nonMatch:"NO"},default:"No match"}},firstOf:{displayName:"First non-null",docUrl:a,backgroundColor:"#ccaee5",textColor:"#26183f",seeds:{values:[null,"The first non-null value"]}},equal:{displayName:"Equal (=)",docUrl:a,backgroundColor:"#d2efe9",textColor:"#183f3e",seeds:{values:["These are equal","These are equal"]}},notEqual:{displayName:"Not equal (!=)",docUrl:a,backgroundColor:"#c1e9e3",textColor:"#183f3e",seeds:{values:["These items","don't match"]}},greaterThan:{displayName:"Greater than (>)",docUrl:a,backgroundColor:"#b0e3e0",textColor:"#183f3e",seeds:{values:[10,9]}},greaterThanOrEqual:{displayName:"Greater than or equal (>=)",docUrl:a,backgroundColor:"#d2efef",textColor:"#183f3e",seeds:{values:[10,10]}},lessThan:{displayName:"Less than (<)",docUrl:a,backgroundColor:"#c1e6e9",textColor:"#183f3e",seeds:{values:[9,10]}},lessThanOrEqual:{displayName:"Less than or equal (<=)",docUrl:a,backgroundColor:"#b0dbe3",textColor:"#183f3e",seeds:{values:[9,9]}},plus:{displayName:"Plus (+)",docUrl:a,backgroundColor:"#d7edd4",textColor:"#193e1e",seeds:{values:[1,2,3]}},subtract:{displayName:"Subtract (-)",docUrl:a,backgroundColor:"#bfe4bc",textColor:"#193e1e",seeds:{value:10,minus:5}},multiply:{displayName:"Multiply (*)",docUrl:a,backgroundColor:"#b4e0b3",textColor:"#193e1e",seeds:{values:[5,5]}},divide:{displayName:"Divide (/)",docUrl:a,backgroundColor:"#cceacd",textColor:"#193e1e",seeds:{value:100,by:10}},modulo:{displayName:"Modulo",docUrl:a,backgroundColor:"#c3e7c6",textColor:"#193e1e",seeds:{value:10,mod:3}},power:{displayName:"Power (^)",docUrl:a,backgroundColor:"#acddb1",textColor:"#193e1e",seeds:{base:2,exponent:3}},round:{displayName:"Round",docUrl:a,backgroundColor:"#d4edd8",textColor:"#193e1e",seeds:{value:3.14159,decimals:2}},floor:{displayName:"Round down",docUrl:a,backgroundColor:"#bce4c4",textColor:"#193e1e",seeds:{value:2.7}},ceil:{displayName:"Round up",docUrl:a,backgroundColor:"#b3e0be",textColor:"#193e1e",seeds:{value:2.3}},abs:{displayName:"Absolute value",docUrl:a,backgroundColor:"#ccead5",textColor:"#193e1e",seeds:{value:-5}},min:{displayName:"Minimum",docUrl:a,backgroundColor:"#c3e7cf",textColor:"#193e1e",seeds:{values:[3,1,2]}},max:{displayName:"Maximum",docUrl:a,backgroundColor:"#acddbf",textColor:"#193e1e",seeds:{values:[3,1,2]}},buildString:{displayName:"String builder",docUrl:a,backgroundColor:"#f8d9c9",textColor:"#3f2f18",seeds:{template:"Hello {{$data.name}}"}},split:{displayName:"Split text",docUrl:a,backgroundColor:"#f5cbab",textColor:"#3f2f18",seeds:{value:"Alpha, Bravo, Charlie",delimiter:",",trim:!1}},join:{displayName:"Join text",docUrl:a,backgroundColor:"#f3cba0",textColor:"#3f2f18",seeds:{values:["Alpha","Bravo","Charlie"],delimiter:", "}},lower:{displayName:"Lower case",docUrl:a,backgroundColor:"#f7e1bf",textColor:"#3f2f18",seeds:{value:"Hello World"}},upper:{displayName:"Upper case",docUrl:a,backgroundColor:"#f6e1b4",textColor:"#3f2f18",seeds:{value:"Hello World"}},trim:{displayName:"Trim whitespace",docUrl:a,backgroundColor:"#f2dd97",textColor:"#3f2f18",seeds:{value:" Hello World "}},regex:{displayName:"Regular expression",docUrl:a,backgroundColor:"#f8f1c9",textColor:"#3f2f18",seeds:{value:"test-this",pattern:"^[a-z]{4}-[a-z]{4}$",flags:"i"}},length:{displayName:"Length",docUrl:a,backgroundColor:"#cde5f3",textColor:"#18293f",seeds:{value:[1,2,3,4,5]}},map:{displayName:"Map each",docUrl:a,backgroundColor:"#bbd7ef",textColor:"#18293f",seeds:{input:[1,2,3],each:"$element",as:"item"}},filter:{displayName:"Filter",docUrl:a,backgroundColor:"#a8c7eb",textColor:"#18293f",seeds:{input:[0,1,2,3],each:"$element",as:"item"}},find:{displayName:"Find first",docUrl:a,backgroundColor:"#cddcf3",textColor:"#18293f",seeds:{input:[0,1,2,3],each:"$element",as:"item"}},some:{displayName:"Any match",docUrl:a,backgroundColor:"#bbccef",textColor:"#18293f",seeds:{input:[0,1,2,3],each:"$element",as:"item"}},every:{displayName:"All match",docUrl:a,backgroundColor:"#a8b9eb",textColor:"#18293f",seeds:{input:[0,1,2,3],each:"$element",as:"item"}},get:{displayName:"Get data",docUrl:a,backgroundColor:"#f0d1e5",textColor:"#3f182a",seeds:{path:"path.to.value",from:{path:{to:{value:"Found it"}}},default:"Not found"}},buildObject:{displayName:"Build object",docUrl:a,backgroundColor:"#eac0cc",textColor:"#3f182a",seeds:{entries:[{key:"firstKey",value:"firstValue"},{key:"secondKey",value:"secondValue"}]}},convert:{displayName:"Convert type",docUrl:a,backgroundColor:"#dddfe3",textColor:"#272a30",seeds:{value:"42"}},http:{displayName:"HTTP request",docUrl:a,backgroundColor:"#eeedd2",textColor:"#3a3f18",seeds:{url:"https://restcountries.com/v3.1/name/zealand",returnPath:"[0].name.common",timeout:5e3}},graphQL:{displayName:"GraphQL query",docUrl:a,backgroundColor:"#e3e8c2",textColor:"#3a3f18",seeds:{query:'query getCountries {\n countries(filter: { continent: { eq: "OC" } }) {\n name\n }\n}',url:"https://countries.trevorblades.com/",returnPath:"countries[0].name",timeout:5e3}},sql:{displayName:"SQL query",docUrl:a,backgroundColor:"#d2e2b1",textColor:"#3a3f18",seeds:{query:"SELECT contact_name FROM customers LIMIT 5",timeout:5e3}},literal:{displayName:"Literal",docUrl:a,backgroundColor:"#d5dae4",textColor:"#272a30",seeds:{value:"No content inside a literal node is evaluated"}}};export{e as categoryHints,l as operatorHints,o as typeSeeds};
|
|
@@ -0,0 +1,86 @@
|
|
|
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
|
+
|
|
50
|
+
declare const WILDCARD: unique symbol;
|
|
51
|
+
type Wildcard = typeof WILDCARD;
|
|
52
|
+
type PathSegment = string | number | Wildcard;
|
|
53
|
+
|
|
54
|
+
type BasicType = 'any' | 'string' | 'number' | 'boolean' | 'array' | 'object' | 'null' | 'integer';
|
|
55
|
+
type LiteralType = {
|
|
56
|
+
literal: readonly (string | number | boolean)[];
|
|
57
|
+
};
|
|
58
|
+
type ExpectedType = BasicType | LiteralType | readonly BasicType[];
|
|
59
|
+
|
|
60
|
+
type ReferenceNamespace = 'data' | 'vars' | 'params' | 'element' | 'index';
|
|
61
|
+
|
|
62
|
+
type ReferenceRecognition = {
|
|
63
|
+
kind: 'reference';
|
|
64
|
+
namespace: ReferenceNamespace;
|
|
65
|
+
segments: PathSegment[];
|
|
66
|
+
drill: string;
|
|
67
|
+
} | {
|
|
68
|
+
kind: 'invalid';
|
|
69
|
+
namespace: ReferenceNamespace;
|
|
70
|
+
reason: string;
|
|
71
|
+
code?: string;
|
|
72
|
+
} | {
|
|
73
|
+
kind: 'unrecognized';
|
|
74
|
+
} | {
|
|
75
|
+
kind: 'plain';
|
|
76
|
+
};
|
|
77
|
+
declare const recognizeReference: (value: string) => ReferenceRecognition;
|
|
78
|
+
|
|
79
|
+
declare const typesIntersect: (a: ExpectedType, b: ExpectedType) => boolean;
|
|
80
|
+
|
|
81
|
+
declare const toCanonical: (expression: unknown, fig: Registry, options?: CanonicalOptions) => unknown;
|
|
82
|
+
declare const toShorthand: (expression: unknown, fig: Registry, options?: ShorthandOptions) => unknown;
|
|
83
|
+
declare const toGet: (reference: unknown, options?: NameOptions) => Record<string, unknown> | null;
|
|
84
|
+
declare const toReference: (node: unknown, options?: NameOptions) => string | null;
|
|
85
|
+
|
|
86
|
+
export { classifyObject, positionalLayout, recognizeReference, toCanonical, toGet, toReference, toShorthand, typesIntersect };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as e,E as r,p as n,S as a,i as s,a as o,R as i,s as l,r as u,b as f,d as c,W as p,G as m,e as d,F as h,D as k}from"../chunks/shared.js";export{t as typesIntersect}from"../chunks/shared.js";const g=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)}},v=(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},y=/*#__PURE__*/new Set(["fallback","useCache","vars"]),$=(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 w(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("//"===t||y.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}},w=(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&&!y.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}},P=(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)A(o,t,n),s.push({kind:"param",name:a,value:o});else{if("//"!==a&&"fallback"!==a&&"vars"!==a)return"useCache"===a?n(r.malformedNode,"'useCache' is not available on fragment calls — caching stays operator-level"):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}},A=(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")},N=(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)||u&&"useCache"===n)return o(r.malformedNode,`'${n}' may not sit beside the shorthand key '${t}' — reserved modifiers only`);void 0!==s&&f.push({kind:"keep",key:n,value:s})}else f.push({kind:"invocation"})}const c=e[t];if(l)return{kind:"literal",content:c,slots:f,fromPayload:!0};if(u)return s(c)?{kind:"fragment",name:i,slots:f,parameters:c,fromPayload:!0}:o(r.malformedNode,`fragments have no single-value or positional form — '${t}' takes a named-arguments object`);const p=n.operators.get(i);return{kind:"operator",operator:p,spelling:i,slots:f,params:b(p,c,n,o),fromPayload:!0}},b=(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=v(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]]},j={positionalParams:m,restParam:null},z=/*#__PURE__*/new Set(["path","from","default"]),W={data:"d",vars:"v",params:"p",element:"e",index:"i"},x=(e,r,t)=>"canonical"===t?r:"alias"===t?W[r]:e,C=(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`$${x(n,t.namespace,r)}${a}`},M=e=>e.startsWith(".")?e.slice(1):e,S=(e,r)=>{if("string"!=typeof e)return null;const t=u(e);if("reference"!==t.kind)return null;const{namespace:n}=t,{token:a,rest:s}=f(e),o=`$${x(a,n,r)}`;switch(n){case"data":return{operator:"get",path:M(s)};case"element":return{operator:"get",path:M(s),from:o};case"vars":case"params":{if(0===t.segments.length)return{operator:"get",path:"",from:o};const e=(e=>{if(e.startsWith(".")){let r=1;for(;r<e.length&&"."!==e[r]&&"["!==e[r];)r++;return r>1?r:null}const r=e[1];if("["!==e[0]||'"'!==r&&"'"!==r)return null;for(let t=2;t<e.length;t++)if("\\"===e[t])t++;else if(e[t]===r)return"]"===e[t+1]?t+2:null;return null})(s);return null===e?null:{operator:"get",path:M(s.slice(e)),from:`${o}${s.slice(0,e)}`}}default:return null}},F=(e,r)=>{if(e.has("default"))return null;const t=(e=>{if("string"==typeof e){if(e.startsWith("$"))return null;try{return d(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=u(s);if("reference"!==e.kind||"index"===e.namespace)return null;if(""!==n&&e.segments.includes(p))return null;a=C(s,r).replace(/\.$/,"")}return""===n?a:n.startsWith("[")?`${a}${n}`:`${a}.${n}`},K=e=>{const r=[];for(let t=e;null!==t;t=t.parent)r.push(t.key);return r.reverse()};class O{lookup;writer;path=null;depth=0;constructor(e,r){this.lookup=e,this.writer=r}fail=(e,r)=>{throw new h({code:e,message:r,path:K(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>k&&this.fail(r.depthCeiling,`the expression nests deeper than the engine's ceiling of ${k} 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 $(t,n,a);case"fragment":return P(t,n,a);case"shorthand":return N(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 E=(e,r)=>"canonical"===r?e.operator.name:"alias"===r?e.operator.alias??e.operator.name:e.spelling,G=e=>Array.isArray(e)?e:[e],I=(e,r)=>[...G(e),...G(r)],R=e=>e.params.find(([e])=>"//"===e)?.[1],q=e=>e.slots.some(e=>"keep"===e.kind&&"//"===e.key),D={kind:"named"},B=(e,r)=>!(Array.isArray(e)||s(e)&&!o(e,r.recognizes)),H=(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.key)?null:F(new Map(i),t);if(null!==r)return r}const l=n?D:((e,r,t)=>{const n=e.positionalParams;if(void 0===n||0===r.length)return D;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 D;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 D;const l=null!==a&&o.has(a);if(null!==a&&l!==(i===s.length))return D;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&&B(e,t)?{kind:"single",value:e}:D}return 1===i&&B(u[0],t)?{kind:"single",value:u[0]}:{kind:"array",value:u}})(e.operator,i,s.lookup),u="named"===l.kind?void 0:R(e),f=void 0!==u&&q(e),c={};for(const t of e.slots)"invocation"===t.kind?(void 0===u||f||(c["//"]=u),c[`$${E(e,r)}`]="named"===l.kind?Object.fromEntries(o):l.value):"keep"===t.kind&&(c[t.key]="//"===t.key&&f?I(t.value,u):s.keep(t));return c},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=>C(e,t)}},J=(e,r,t)=>new O(g(r),((e={})=>{const r=e.operatorNames??"preserve",t=e.referenceNames??"preserve",n=e.referencesAsGet??!1;return{operator:(e,t)=>{const n={},a=e.fromPayload?R(e):void 0,s=void 0!==a&&q(e);for(const o of e.slots)if("invocation"===o.kind){if(void 0===a||s||(n["//"]=a),n.operator=E(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["//"]=I(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=S(e,t);if(null!==r)return r}return C(e,t)}}})(t)).value(e),L=(e,r,t)=>new O(g(r),H(t)).value(e),Q=(e,r)=>S(e,r?.referenceNames??"preserve"),T=(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(!z.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=v(j,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(!z.has(e))return null;r.set(e,n)}}return r}return r.set(l(j),t),r})(e);return null===t?null:F(t,r?.referenceNames??"preserve")};export{e as classifyObject,n as positionalLayout,u as recognizeReference,J as toCanonical,Q as toGet,T as toReference,L as toShorthand};
|