prisme-flow
Version:
prisme platform flow engine
2 lines • 32.4 kB
JavaScript
var jsonata=function(){"use strict";function a(a){for(var b=1,c=[],d={},e=d;b<a.length;){var f=a.charAt(b);if(":"===f)break;var g=function(){c.push(d),e=d,d={}},h=function(a,b,c,d){for(var e=1,g=b;g<a.length;)if(g++,f=a.charAt(g),f===d){if(e--,0===e)break}else f===c&&e++;return g};switch(f){case"s":case"n":case"b":case"l":case"o":d.regex="["+f+"m]",d.type=f,g();break;case"a":d.regex="[asnblfom]",d.type=f,d.array=!0,g();break;case"f":d.regex="f",d.type=f,g();break;case"j":d.regex="[asnblom]",d.type=f,g();break;case"x":d.regex="[asnblfom]",d.type=f,g();break;case"-":e.context=!0,e.contextRegex=new RegExp(e.regex),e.regex+="?";break;case"?":case"+":e.regex+=f;break;case"(":var i=h(a,b,"(",")"),j=a.substring(b+1,i);if(j.indexOf("<")!==-1)throw{code:"S0402",stack:(new Error).stack,value:j,offset:b};d.regex="["+j+"m]",d.type="("+j+")",b=i,g();break;case"<":if("a"!==e.type&&"f"!==e.type)throw{code:"S0401",stack:(new Error).stack,value:e.type,offset:b};var k=h(a,b,"<",">");e.subtype=a.substring(b+1,k),b=k}b++}var l="^"+c.map(function(a){return"("+a.regex+")"}).join("")+"$",m=new RegExp(l),n=function(a){var b;if(B(a))b="f";else{var c=typeof a;switch(c){case"string":b="s";break;case"number":b="n";break;case"boolean":b="b";break;case"object":b=null===a?"l":Array.isArray(a)?"a":"o";break;case"undefined":b="m"}}return b},o=function(a,b){for(var d="^",e=0,f=0;f<c.length;f++){d+=c[f].regex;var g=b.match(d);if(null===g)throw{code:"T0410",stack:(new Error).stack,value:a[e],index:e+1};e=g[0].length}throw{code:"T0410",stack:(new Error).stack,value:a[e],index:e+1}};return{definition:a,validate:function(a,b){var d="";a.forEach(function(a){d+=n(a)});var e=m.exec(d);if(e){var f=[],g=0;return c.forEach(function(c,d){var h=a[g],i=e[d+1];if(""===i)if(c.context){var j=n(b);if(!c.contextRegex.test(j))throw{code:"T0411",stack:(new Error).stack,value:b,index:g+1};f.push(b)}else f.push(h),g++;else i.split("").forEach(function(b){if("a"===c.type){if("m"===b)h=void 0;else{h=a[g];var d=!0;if("undefined"!=typeof c.subtype)if("a"!==b&&i!==c.subtype)d=!1;else if("a"===b&&h.length>0){var e=n(h[0]);if(e!==c.subtype.charAt(0))d=!1;else{var j=h.filter(function(a){return n(a)!==e});d=0===j.length}}if(!d)throw{code:"T0412",stack:(new Error).stack,value:h,index:g+1,type:c.subtype};"a"!==b&&(h=[h])}f.push(h),g++}else f.push(h),g++})}),f}o(a,d)}}}function b(a){var b=!1;if("number"==typeof a){var c=parseFloat(a);if(b=!isNaN(c),b&&!isFinite(c))throw{code:"D1001",value:a,stack:(new Error).stack}}return b}function c(a){var c=!1;return Array.isArray(a)&&(c=0===a.filter(function(a){return!b(a)}).length),c}function d(a,b,c){var d,l=c.lookup("__evaluate_entry");switch(l&&l(a,b,c),a.type){case"path":d=e(a,b,c);break;case"binary":d=g(a,b,c);break;case"unary":d=h(a,b,c);break;case"name":d=i(a,b,c);break;case"literal":d=j(a,b,c);break;case"wildcard":d=k(a,b,c);break;case"descendant":d=m(a,b,c);break;case"condition":d=w(a,b,c);break;case"block":d=x(a,b,c);break;case"regex":d=y(a,b,c);break;case"function":d=D(a,b,c);break;case"variable":d=z(a,b,c);break;case"lambda":d=G(a,b,c);break;case"partial":d=H(a,b,c)}a.hasOwnProperty("predicate")&&(d=f(a.predicate,d,c)),a.hasOwnProperty("group")&&(d=t(a.group,d,c));var n=c.lookup("__evaluate_exit");return n&&n(a,b,c,d),d}function e(a,b,c){var e,f,g=!1;"variable"===a[0].type?a[0].absolute=!0:"unary"===a[0].type&&"["===a[0].value&&(b=[null]);for(var h=0;h<a.length;h++){var i=a[h];i.keepArray===!0&&(g=!0);var j=[];if(e=void 0,f=i.absolute===!0?[b]:Array.isArray(b)?b:[b],a.length>1&&"literal"===i.type&&(i.type="name"),f.forEach(function(a){var b=d(i,a,c);"undefined"!=typeof b&&(Array.isArray(b)&&"["!==i.value?b.forEach(function(a){"undefined"!=typeof a&&j.push(a)}):j.push(b))}),1===j.length?e=g?j:j[0]:j.length>1&&(e=j),"undefined"==typeof e)break;b=e}return e}function f(a,e,f){var g,h=e,i=[];return a.forEach(function(a){if(Array.isArray(h)||(h=[h]),i=[],g=void 0,"literal"===a.type&&b(a.value)){var e=a.value;Number.isInteger(e)||(e=Math.floor(e)),e<0&&(e=h.length+e),g=h[e]}else h.forEach(function(e,g){var j=d(a,e,f);b(j)&&(j=[j]),c(j)?j.forEach(function(a){Number.isInteger(a)||(a=Math.floor(a)),a<0&&(a=h.length+a),a===g&&i.push(e)}):ga(j)&&i.push(e)});1===i.length?g=i[0]:i.length>1&&(g=i),h=g}),g}function g(a,b,c){var d;switch(a.value){case"+":case"-":case"*":case"/":case"%":d=o(a,b,c);break;case"=":case"!=":case"<":case"<=":case">":case">=":d=p(a,b,c);break;case"&":d=s(a,b,c);break;case"and":case"or":d=r(a,b,c);break;case"..":d=u(a,b,c);break;case":=":d=v(a,b,c);break;case"in":d=q(a,b,c);break;case"~>":d=A(a,b,c)}return d}function h(a,c,e){var f;switch(a.value){case"-":if(f=d(a.expression,c,e),!b(f))throw{code:"D1002",stack:(new Error).stack,position:a.position,token:a.value,value:f};f=-f;break;case"[":f=[],a.lhs.forEach(function(a){var b=d(a,c,e);"undefined"!=typeof b&&("["===a.value?f.push(b):f=ma(f,b))});break;case"{":f=t(a,c,e)}return f}function i(a,b,c){var d;if(Array.isArray(b)){var e=[];b.forEach(function(b){var d=i(a,b,c);"undefined"!=typeof d&&e.push(d)}),1===e.length?d=e[0]:e.length>1&&(d=e)}else null!==b&&"object"==typeof b&&(d=b[a.value]);return d}function j(a){return a.value}function k(a,b){var c,d=[];return null!==b&&"object"==typeof b&&Object.keys(b).forEach(function(a){var c=b[a];Array.isArray(c)?(c=l(c),d=ma(d,c)):d.push(c)}),1===d.length?c=d[0]:d.length>1&&(c=d),c}function l(a,b){return"undefined"==typeof b&&(b=[]),Array.isArray(a)?a.forEach(function(a){l(a,b)}):b.push(a),b}function m(a,b){var c,d=[];return"undefined"!=typeof b&&(n(b,d),c=1===d.length?d[0]:d),c}function n(a,b){Array.isArray(a)||b.push(a),Array.isArray(a)?a.forEach(function(a){n(a,b)}):null!==a&&"object"==typeof a&&Object.keys(a).forEach(function(c){n(a[c],b)})}function o(a,c,e){var f,g=d(a.lhs,c,e),h=d(a.rhs,c,e);if("undefined"==typeof g||"undefined"==typeof h)return f;if(!b(g))throw{code:"T2001",stack:(new Error).stack,position:a.position,token:a.value,value:g};if(!b(h))throw{code:"T2002",stack:(new Error).stack,position:a.position,token:a.value,value:h};switch(a.value){case"+":f=g+h;break;case"-":f=g-h;break;case"*":f=g*h;break;case"/":f=g/h;break;case"%":f=g%h}return f}function p(a,b,c){var e,f=d(a.lhs,b,c),g=d(a.rhs,b,c);if("undefined"==typeof f||"undefined"==typeof g)return!1;switch(a.value){case"=":e=f===g;break;case"!=":e=f!==g;break;case"<":e=f<g;break;case"<=":e=f<=g;break;case">":e=f>g;break;case">=":e=f>=g}return e}function q(a,b,c){var e=!1,f=d(a.lhs,b,c),g=d(a.rhs,b,c);if("undefined"==typeof f||"undefined"==typeof g)return!1;Array.isArray(g)||(g=[g]);for(var h=0;h<g.length;h++)if(g[h]===f){e=!0;break}return e}function r(a,b,c){var e;switch(a.value){case"and":e=ga(d(a.lhs,b,c))&&ga(d(a.rhs,b,c));break;case"or":e=ga(d(a.lhs,b,c))||ga(d(a.rhs,b,c))}return e}function s(a,b,c){var e,f=d(a.lhs,b,c),g=d(a.rhs,b,c),h="",i="";return"undefined"!=typeof f&&(h=U(f)),"undefined"!=typeof g&&(i=U(g)),e=h.concat(i)}function t(a,b,c){var e={},f={};Array.isArray(b)||(b=[b]),b.forEach(function(b){a.lhs.forEach(function(e){var g=d(e[0],b,c);if("string"!=typeof g)throw{code:"T1003",stack:(new Error).stack,position:a.position,value:g};var h={data:b,expr:e[1]};f.hasOwnProperty(g)?f[g].data=ma(f[g].data,b):f[g]=h})});for(var g in f){var h=f[g],i=d(h.expr,h.data,c);e[g]=i}return e}function u(a,b,c){var e,f=d(a.lhs,b,c),g=d(a.rhs,b,c);if("undefined"==typeof f||"undefined"==typeof g)return e;if(f>g)return e;if(!Number.isInteger(f))throw{code:"T2003",stack:(new Error).stack,position:a.position,token:a.value,value:f};if(!Number.isInteger(g))throw{code:"T2004",stack:(new Error).stack,position:a.position,token:a.value,value:g};e=new Array(g-f+1);for(var h=f,i=0;h<=g;h++,i++)e[i]=h;return e}function v(a,b,c){var e=d(a.rhs,b,c);if("variable"!==a.lhs.type)throw{code:"D2005",stack:(new Error).stack,position:a.position,token:a.value,value:"path"===a.lhs.type?a.lhs[0].value:a.lhs.value};return c.bind(a.lhs.value,e),e}function w(a,b,c){var e,f=d(a.condition,b,c);return ga(f)?e=d(a.then,b,c):"undefined"!=typeof a.else&&(e=d(a.else,b,c)),e}function x(a,b,c){var e,f=pa(c);return a.expressions.forEach(function(a){e=d(a,b,f)}),e}function y(a){a.value.lastIndex=0;var b=function(c){var d,e=a.value,f=e.exec(c);if(null!==f){if(d={match:f[0],start:f.index,end:f.index+f[0].length,groups:[]},f.length>1)for(var g=1;g<f.length;g++)d.groups.push(f[g]);d.next=function(){if(!(e.lastIndex>=c.length)){var d=b(c);if(d&&""===d.match&&e.lastIndex===a.value.lastIndex)throw{code:"D1004",stack:(new Error).stack,position:a.position,value:a.value.source};return d}}}return d};return b}function z(a,b,c){var d;return d=""===a.value?b:c.lookup(a.value)}function A(a,b,c){var e,f=d(a.lhs,b,c);if("function"===a.rhs.type)e=D(a.rhs,b,c,{context:f});else{var g=d(a.rhs,b,c);if(!B(g))throw{code:"T2006",stack:(new Error).stack,position:a.position,value:g};e=B(f)?E(xa,[f,g],c,null):E(g,[f],c,null)}return e}function B(a){return a&&(a._jsonata_function===!0||a._jsonata_lambda===!0)||"function"==typeof a}function C(a){return a&&a._jsonata_lambda===!0}function D(a,b,c,e){var f,g=[];a.arguments.forEach(function(a){g.push(d(a,b,c))}),e&&g.unshift(e.context);var h=d(a.procedure,b,c);if("undefined"==typeof h&&"path"===a.procedure.type&&c.lookup(a.procedure[0].value))throw{code:"T1005",stack:(new Error).stack,position:a.position,token:a.procedure[0].value};try{var i=g;h&&(i=I(h.signature,g,b)),f=E(h,i,b)}catch(b){throw b.position=a.position,b.token="path"===a.procedure.type?a.procedure[0].value:a.procedure.value,b}return f}function E(a,b,c){var e;for(e=F(a,b,c);C(e)&&e.thunk===!0;){var f=d(e.body.procedure,e.input,e.environment),g=[];e.body.arguments.forEach(function(a){g.push(d(a,e.input,e.environment))}),e=F(f,g,c)}return e}function F(a,b,c){var d;if(C(a))d=J(a,b);else if(a&&a._jsonata_function===!0)d=a.implementation.apply(c,b);else{if("function"!=typeof a)throw{code:"T1006",stack:(new Error).stack};d=a.apply(c,b)}return d}function G(a,b,c){var d={_jsonata_lambda:!0,input:b,environment:c,arguments:a.arguments,signature:a.signature,body:a.body};return a.thunk===!0&&(d.thunk=!0),d}function H(a,b,c){var e,f=[];a.arguments.forEach(function(a){"operator"===a.type&&"?"===a.value?f.push(a):f.push(d(a,b,c))});var g=d(a.procedure,b,c);if("undefined"==typeof g&&"path"===a.procedure.type&&c.lookup(a.procedure[0].value))throw{code:"T1007",stack:(new Error).stack,position:a.position,token:a.procedure[0].value};if(C(g))e=K(g,f);else if(g&&g._jsonata_function===!0)e=L(g.implementation,f);else{if("function"!=typeof g)throw{code:"T1008",stack:(new Error).stack,position:a.position,token:"path"===a.procedure.type?a.procedure[0].value:a.procedure.value};e=L(g,f)}return e}function I(a,b,c){if("undefined"==typeof a)return b;var d=a.validate(b,c);return d}function J(a,b){var c,e=pa(a.environment);return a.arguments.forEach(function(a,c){e.bind(a.value,b[c])}),c="function"==typeof a.body?M(a.body,e):d(a.body,a.input,e)}function K(a,b){var c=pa(a.environment),d=[];a.arguments.forEach(function(a,e){var f=b[e];f&&"operator"===f.type&&"?"===f.value?d.push(a):c.bind(a.value,f)});var e={_jsonata_lambda:!0,input:a.input,environment:c,arguments:d,body:a.body};return e}function L(a,b){var c=N(a);c=c.map(function(a){return"$"+a.trim()});var d="function("+c.join(", ")+"){ _ }",e=va(d);e.body=a;var f=K(e,b);return f}function M(a,b){var c=N(a),d=c.map(function(a){return b.lookup(a.trim())}),e=a.apply(null,d);return e}function N(a){var b=a.toString(),c=/\(([^\)]*)\)/.exec(b)[1],d=c.split(",");return d}function O(b,c){var d={_jsonata_function:!0,implementation:b};return"undefined"!=typeof c&&(d.signature=a(c)),d}function P(a){if("undefined"!=typeof a){var b=0;return a.forEach(function(a){b+=a}),b}}function Q(a){return"undefined"==typeof a?0:a.length}function R(a){if("undefined"!=typeof a&&0!==a.length)return Math.max.apply(Math,a)}function S(a){if("undefined"!=typeof a&&0!==a.length)return Math.min.apply(Math,a)}function T(a){if("undefined"!=typeof a&&0!==a.length){var b=0;return a.forEach(function(a){b+=a}),b/a.length}}function U(a){if("undefined"!=typeof a){var c;if("string"==typeof a)c=a;else if(B(a))c="";else{if("number"==typeof a&&!isFinite(a))throw{code:"D3001",value:a,stack:(new Error).stack};c=JSON.stringify(a,function(a,c){return"undefined"!=typeof c&&null!==c&&c.toPrecision&&b(c)?Number(c.toPrecision(13)):c&&B(c)?"":c})}return c}}function V(a,b,c){if("undefined"!=typeof a)return a.substr(b,c)}function W(a,b){if("undefined"!=typeof a){var c=a.indexOf(b);return c>-1?a.substr(0,c):a}}function X(a,b){if("undefined"!=typeof a){var c=a.indexOf(b);return c>-1?a.substr(c+b.length):a}}function Y(a){if("undefined"!=typeof a)return a.toLowerCase()}function Z(a){if("undefined"!=typeof a)return a.toUpperCase()}function $(a){if("undefined"!=typeof a)return a.length}function _(a){if("undefined"!=typeof a){var b=a.replace(/[ \t\n\r]+/gm," ");return" "===b.charAt(0)&&(b=b.substring(1))," "===b.charAt(b.length-1)&&(b=b.substring(0,b.length-1)),b}}function aa(a,b){if("undefined"!=typeof a){var c;if("string"==typeof b)c=a.indexOf(b)!==-1;else{var d=b(a);c="undefined"!=typeof d}return c}}function ba(a,b,c){if("undefined"!=typeof a){if(c<0)throw{stack:(new Error).stack,value:c,code:"D3040",index:3};var d=[];if("undefined"==typeof c||c>0){var e=0,f=b(a);if("undefined"!=typeof f)for(;"undefined"!=typeof f&&("undefined"==typeof c||e<c);)d.push({match:f.match,index:f.start,groups:f.groups}),f=f.next(),e++}return d}}function ca(a,b,c,d){if("undefined"!=typeof a){if(""===b)throw{code:"D3010",stack:(new Error).stack,value:b,index:2};if(d<0)throw{code:"D3011",stack:(new Error).stack,value:d,index:4};var e;e="string"==typeof c?function(a){for(var b="",d=0,e=c.indexOf("$",d);e!==-1&&d<c.length;){b+=c.substring(d,e),d=e+1;var f=c.charAt(d);if("$"===f)b+="$",d++;else if("0"===f)b+=a.match,d++;else{var g;if(g=0===a.groups.length?1:Math.floor(Math.log(a.groups.length)*Math.LOG10E)+1,e=parseInt(c.substring(d,d+g),10),g>1&&e>a.groups.length&&(e=parseInt(c.substring(d,d+g-1),10)),isNaN(e))b+="$";else{if(a.groups.length>0){var h=a.groups[e-1];"undefined"!=typeof h&&(b+=h)}d+=e.toString().length}}e=c.indexOf("$",d)}return b+=c.substring(d)}:c;var f="",g=0;if("undefined"==typeof d||d>0){var h=0;if("string"==typeof b){for(var i=a.indexOf(b,g);i!==-1&&("undefined"==typeof d||h<d);)f+=a.substring(g,i),f+=c,g=i+b.length,h++,i=a.indexOf(b,g);f+=a.substring(g)}else{var j=b(a);if("undefined"!=typeof j){for(;"undefined"!=typeof j&&("undefined"==typeof d||h<d);){f+=a.substring(g,j.start);var k=E(e,[j],null);if("string"!=typeof k)throw{code:"D3012",stack:(new Error).stack,value:k};f+=k,g=j.start+j.match.length,h++,j=j.next()}f+=a.substring(g)}else f=a}}else f=a;return f}}function da(a,b,c){if("undefined"!=typeof a){if(c<0)throw{code:"D3020",stack:(new Error).stack,value:c,index:3};var d=[];if("undefined"==typeof c||c>0)if("string"==typeof b)d=a.split(b,c);else{var e=0,f=b(a);if("undefined"!=typeof f){for(var g=0;"undefined"!=typeof f&&("undefined"==typeof c||e<c);)d.push(a.substring(g,f.start)),g=f.end,f=f.next(),e++;("undefined"==typeof c||e<c)&&d.push(a.substring(g))}else d=[a]}return d}}function ea(a,b){if("undefined"!=typeof a)return"undefined"==typeof b&&(b=""),a.join(b)}function fa(a){var b;if("undefined"!=typeof a){if("number"==typeof a)b=a;else{if("string"!=typeof a||!/^-?(0|([1-9][0-9]*))(\.[0-9]+)?([Ee][-+]?[0-9]+)?$/.test(a)||isNaN(parseFloat(a))||!isFinite(a))throw{code:"D3030",value:a,stack:(new Error).stack,index:1};b=parseFloat(a)}return b}}function ga(a){if("undefined"!=typeof a){var c=!1;if(Array.isArray(a)){if(1===a.length)c=ga(a[0]);else if(a.length>1){var d=a.filter(function(a){return ga(a)});c=d.length>0}}else"string"==typeof a?a.length>0&&(c=!0):b(a)?0!==a&&(c=!0):null!==a&&"object"==typeof a?Object.keys(a).length>0&&(C(a)||a._jsonata_function||(c=!0)):"boolean"==typeof a&&a===!0&&(c=!0);return c}}function ha(a){return!ga(a)}function ia(a,b){var c=arguments,d=[],e=b;e=[];for(var f=1;f<c.length;f++)e.push(c[f]);for(var g=0;g<e[0].length;g++){for(var h=[],i="function"==typeof a?a.length:a._jsonata_function===!0?a.implementation.length:a.arguments.length,j=0;j<i;j++)h.push(e[j][g]);d.push(E(a,h,null))}return d}function ja(a,b,c){var d;if(2!==a.length&&(a._jsonata_function!==!0||2!==a.implementation.length)&&2!==a.arguments.length)throw{stack:(new Error).stack,code:"D3050",index:1};var e;for("undefined"==typeof c&&b.length>0?(d=b[0],e=1):(d=c,e=0);e<b.length;)d=E(a,[d,b[e]],null),e++;return d}function ka(a){var b=[];if(Array.isArray(a)){var c={};a.forEach(function(a){var b=ka(a);Array.isArray(b)&&b.forEach(function(a){c[a]=!0})}),b=ka(c)}else null===a||"object"!=typeof a||C(a)?b=void 0:(b=Object.keys(a),0===b.length&&(b=void 0));return b}function la(a,b){var c=i({value:b},a);return c}function ma(a,b){return"undefined"==typeof a?b:"undefined"==typeof b?a:(Array.isArray(a)||(a=[a]),Array.isArray(b)||(b=[b]),Array.prototype.push.apply(a,b),a)}function na(a){return"undefined"!=typeof a}function oa(a){var b=[];if(Array.isArray(a))a.forEach(function(a){b=ma(b,oa(a))});else if(null===a||"object"!=typeof a||C(a))b=a;else for(var c in a){var d={};d[c]=a[c],b.push(d)}return b}function pa(a){var b={};return{bind:function(a,c){b[a]=c},lookup:function(c){var d;return b.hasOwnProperty(c)?d=b[c]:a&&(d=a.lookup(c)),d}}}function qa(a){var b="Unknown error";"undefined"!=typeof a.message&&(b=a.message);var c=ya[a.code];return"undefined"!=typeof c&&(b=c.replace(/\{\{([^}]+)}}/g,function(){return a[arguments[1]]})),b}function ra(a){var b;try{b=va(a)}catch(a){throw a.message=qa(a),a}var c=pa(wa);return{evaluate:function(a,e){if("undefined"!=typeof e){var f;f=pa(c);for(var g in e)f.bind(g,e[g])}else f=c;f.bind("$",a);var h;try{h=d(b,a,f)}catch(a){throw a.message=qa(a),a}return h},assign:function(a,b){c.bind(a,b)},registerFunction:function(a,b,d){var e=O(b,d);c.bind(a,e)}}}var sa={".":75,"[":80,"]":0,"{":70,"}":0,"(":80,")":0,",":0,"@":75,"#":70,";":80,":":80,"?":20,"+":50,"-":50,"*":60,"/":60,"%":60,"|":20,"=":40,"<":40,">":40,"`":80,"**":60,"..":20,":=":10,"!=":40,"<=":40,">=":40,"~>":40,and:30,or:25,in:40,"&":50,"!":0,"~":0},ta={'"':'"',"\\":"\\","/":"/",b:"\b",f:"\f",n:"\n",r:"\r",t:"\t"},ua=function(a){var b=0,c=a.length,d=function(a,c){var d={type:a,value:c,position:b};return d},e=function(){for(var d,e,f=b,g=0;b<c;){var h=a.charAt(b);if("/"===h&&"\\"!==a.charAt(b-1)&&0===g){if(d=a.substring(f,b),""===d)throw{code:"S0301",stack:(new Error).stack,position:b};for(b++,h=a.charAt(b),f=b;"i"===h||"m"===h;)b++,h=a.charAt(b);return e=a.substring(f,b)+"g",new RegExp(d,e)}"("!==h&&"["!==h&&"{"!==h||"\\"===a.charAt(b-1)||g++,")"!==h&&"]"!==h&&"}"!==h||"\\"===a.charAt(b-1)||g--,b++}throw{code:"S0302",stack:(new Error).stack,position:b}},f=function(f){if(b>=c)return null;for(var g=a.charAt(b);b<c&&" \t\n\r\v".indexOf(g)>-1;)b++,g=a.charAt(b);if(f!==!0&&"/"===g)return b++,d("regex",e());if("."===g&&"."===a.charAt(b+1))return b+=2,d("operator","..");if(":"===g&&"="===a.charAt(b+1))return b+=2,d("operator",":=");if("!"===g&&"="===a.charAt(b+1))return b+=2,d("operator","!=");if(">"===g&&"="===a.charAt(b+1))return b+=2,d("operator",">=");if("<"===g&&"="===a.charAt(b+1))return b+=2,d("operator","<=");if("*"===g&&"*"===a.charAt(b+1))return b+=2,d("operator","**");if("~"===g&&">"===a.charAt(b+1))return b+=2,d("operator","~>");if(sa.hasOwnProperty(g))return b++,d("operator",g);if('"'===g||"'"===g){var h=g;b++;for(var i="";b<c;){if(g=a.charAt(b),"\\"===g)if(b++,g=a.charAt(b),ta.hasOwnProperty(g))i+=ta[g];else{if("u"!==g)throw{code:"S0103",stack:(new Error).stack,position:b,token:g};var j=a.substr(b+1,4);if(!/^[0-9a-fA-F]+$/.test(j))throw{code:"S0104",stack:(new Error).stack,position:b};var k=parseInt(j,16);i+=String.fromCharCode(k),b+=4}else{if(g===h)return b++,d("string",i);i+=g}b++}throw{code:"S0101",stack:(new Error).stack,position:b}}var l=/^-?(0|([1-9][0-9]*))(\.[0-9]+)?([Ee][-+]?[0-9]+)?/,m=l.exec(a.substring(b));if(null!==m){var n=parseFloat(m[0]);if(!isNaN(n)&&isFinite(n))return b+=m[0].length,d("number",n);throw{code:"S0102",stack:(new Error).stack,position:b,token:m[0]}}for(var o,p,q=b;;)if(o=a.charAt(q),q===c||" \t\n\r\v".indexOf(o)>-1||sa.hasOwnProperty(o)){if("$"===a.charAt(b))return p=a.substring(b+1,q),b=q,d("variable",p);switch(p=a.substring(b,q),b=q,p){case"or":case"in":case"and":return d("operator",p);case"true":return d("value",!0);case"false":return d("value",!1);case"null":return d("value",null);default:return b===c&&""===p?null:d("name",p)}}else q++};return f},va=function(c){var d,e,f={},g={nud:function(){return this}},h=function(a,b){var c=f[a];return b=b||0,c?b>=c.lbp&&(c.lbp=b):(c=Object.create(g),c.id=c.value=a,c.lbp=b,f[a]=c),c},i=function(a,b){if(a&&d.id!==a){var g;throw g="(end)"===d.id?"S0203":"S0202",{code:g,stack:(new Error).stack,position:d.position,token:d.id,value:a}}var h=e(b);if(null===h)return d=f["(end)"],d.position=c.length,d;var i,j=h.value,k=h.type;switch(k){case"name":case"variable":i=f["(name)"];break;case"operator":if(i=f[j],!i)throw{code:"S0204",stack:(new Error).stack,position:h.position,token:j};break;case"string":case"number":case"value":k="literal",i=f["(literal)"];break;case"regex":k="regex",i=f["(regex)"];break;default:throw{code:"S0205",stack:(new Error).stack,position:h.position,token:j}}return d=Object.create(i),d.value=j,d.type=k,d.position=h.position,d},j=function(a){var b,c=d;for(i(null,!0),b=c.nud();a<d.lbp;)c=d,i(),b=c.led(b);return b},k=function(a,b,c){var d=b||sa[a],e=h(a,d);return e.led=c||function(a){return this.lhs=a,this.rhs=j(d),this.type="binary",this},e},l=function(a,b,c){var d=b||sa[a],e=h(a,d);return e.led=c||function(a){return this.lhs=a,this.rhs=j(d-1),this.type="binary",this},e},m=function(a,b){var c=h(a);return c.nud=b||function(){return this.expression=j(70),this.type="unary",this},c};h("(end)"),h("(name)"),h("(literal)"),h("(regex)"),h(":"),h(";"),h(","),h(")"),h("]"),h("}"),h(".."),k("."),k("+"),k("-"),k("*"),k("/"),k("%"),k("="),k("<"),k(">"),k("!="),k("<="),k(">="),k("&"),k("and"),k("or"),k("in"),l(":="),m("-"),k("~>"),m("*",function(){return this.type="wildcard",this}),m("**",function(){return this.type="descendant",this}),k("(",sa["("],function(b){if(this.procedure=b,this.type="function",this.arguments=[],")"!==d.id)for(;"operator"===d.type&&"?"===d.id?(this.type="partial",this.arguments.push(d),i("?")):this.arguments.push(j(0)),","===d.id;)i(",");if(i(")",!0),"name"===b.type&&("function"===b.value||"λ"===b.value)){if(this.arguments.forEach(function(a,b){if("variable"!==a.type)throw{code:"S0208",stack:(new Error).stack,position:a.position,token:a.value,value:b+1}}),this.type="lambda","<"===d.id){for(var c=d.position,e=1,f="<";e>0&&"{"!==d.id&&"(end)"!==d.id;){var g=i();">"===g.id?e--:"<"===g.id&&e++,f+=g.value}i(">");try{this.signature=a(f)}catch(a){throw a.position=c+a.offset,a}}i("{"),this.body=j(0),i("}")}return this}),m("(",function(){for(var a=[];")"!==d.id&&(a.push(j(0)),";"===d.id);)i(";");return i(")",!0),this.type="block",this.expressions=a,this}),m("[",function(){var a=[];if("]"!==d.id)for(;;){var b=j(0);if(".."===d.id){var c={type:"binary",value:"..",position:d.position,lhs:b};i(".."),c.rhs=j(0),b=c}if(a.push(b),","!==d.id)break;i(",")}return i("]",!0),this.lhs=a,this.type="unary",this}),k("[",sa["["],function(a){if("]"===d.id){for(var b=a;b&&"binary"===b.type&&"["===b.value;)b=b.lhs;return b.keepArray=!0,i("]"),a}return this.lhs=a,this.rhs=j(sa["]"]),this.type="binary",i("]",!0),this});var n=function(a){var b=[];if("}"!==d.id)for(;;){var c=j(0);i(":");var e=j(0);if(b.push([c,e]),","!==d.id)break;i(",")}return i("}",!0),"undefined"==typeof a?(this.lhs=b,this.type="unary"):(this.lhs=a,this.rhs=b,this.type="binary"),this};m("{",n),k("{",sa["{"],n),k("?",sa["?"],function(a){return this.type="condition",this.condition=a,this.then=j(0),":"===d.id&&(i(":"),this.else=j(0)),this});var o=function(a){var b;if("function"===a.type){var c={type:"lambda",thunk:!0,arguments:[],position:a.position};c.body=a,b=c}else if("condition"===a.type)a.then=o(a.then),a.else=o(a.else),b=a;else if("block"===a.type){var d=a.expressions.length;d>0&&(a.expressions[d-1]=o(a.expressions[d-1])),b=a}else b=a;return b},p=function(a){var c=[];switch(a.type){case"binary":switch(a.value){case".":var d=p(a.lhs);"path"===d.type?Array.prototype.push.apply(c,d):c.push(d);var e=p(a.rhs);"path"!==e.type&&(e=[e]),Array.prototype.push.apply(c,e),c.type="path";break;case"[":c=p(a.lhs);var f=c;if("path"===c.type&&(f=c[c.length-1]),"undefined"!=typeof f.group)throw{code:"S0209",stack:(new Error).stack,position:a.position};"undefined"==typeof f.predicate&&(f.predicate=[]),f.predicate.push(p(a.rhs));break;case"{":if(c=p(a.lhs),"undefined"!=typeof c.group)throw{code:"S0210",stack:(new Error).stack,position:a.position};c.group={lhs:a.rhs.map(function(a){return[p(a[0]),p(a[1])]}),position:a.position};break;default:c={type:a.type,value:a.value,position:a.position},c.lhs=p(a.lhs),c.rhs=p(a.rhs)}break;case"unary":c={type:a.type,value:a.value,position:a.position},"["===a.value?c.lhs=a.lhs.map(function(a){return p(a)}):"{"===a.value?c.lhs=a.lhs.map(function(a){return[p(a[0]),p(a[1])]}):(c.expression=p(a.expression),"-"===a.value&&"literal"===c.expression.type&&b(c.expression.value)&&(c=c.expression,c.value=-c.value));break;case"function":case"partial":c={type:a.type,name:a.name,value:a.value,position:a.position},c.arguments=a.arguments.map(function(a){return p(a)}),c.procedure=p(a.procedure);break;case"lambda":c={type:a.type,arguments:a.arguments,signature:a.signature,position:a.position};var g=p(a.body);c.body=o(g);break;case"condition":c={type:a.type,position:a.position},c.condition=p(a.condition),c.then=p(a.then),"undefined"!=typeof a.else&&(c.else=p(a.else));break;case"block":c={type:a.type,position:a.position},c.expressions=a.expressions.map(function(a){return p(a)});break;case"name":c=[a],c.type="path";break;case"literal":case"wildcard":case"descendant":case"variable":case"regex":c=a;break;case"operator":if("and"===a.value||"or"===a.value||"in"===a.value)a.type="name",c=p(a);else{if("?"!==a.value)throw{code:"S0201",stack:(new Error).stack,position:a.position,token:a.value};c=a}break;default:var h="S0206";throw"(end)"===a.id&&(h="S0207"),{code:h,stack:(new Error).stack,position:a.position,token:a.value}}return c};e=ua(c),i();var q=j(0);if("(end)"!==d.id)throw{code:"S0201",stack:(new Error).stack,position:d.position,token:d.value};return q=p(q)},wa=pa(null);Number.isInteger=Number.isInteger||function(a){return"number"==typeof a&&isFinite(a)&&Math.floor(a)===a};var xa=d(va("function($f, $g) { function($x){ $g($f($x)) } }"),null,wa);wa.bind("sum",O(P,"<a<n>:n>")),wa.bind("count",O(Q,"<a:n>")),wa.bind("max",O(R,"<a<n>:n>")),wa.bind("min",O(S,"<a<n>:n>")),wa.bind("average",O(T,"<a<n>:n>")),wa.bind("string",O(U,"<x-:s>")),wa.bind("substring",O(V,"<s-nn?:s>")),wa.bind("substringBefore",O(W,"<s-s:s>")),wa.bind("substringAfter",O(X,"<s-s:s>")),wa.bind("lowercase",O(Y,"<s-:s>")),wa.bind("uppercase",O(Z,"<s-:s>")),wa.bind("length",O($,"<s-:n>")),wa.bind("trim",O(_,"<s-:s>")),wa.bind("match",O(ba,"<s-f<s:o>n?:a<o>>")),wa.bind("contains",O(aa,"<s-(sf):b>")),wa.bind("replace",O(ca,"<s-(sf)(sf)n?:s>")),wa.bind("split",O(da,"<s-(sf)n?:a<s>>")),wa.bind("join",O(ea,"<a<s>s?:s>")),wa.bind("number",O(fa,"<(ns)-:n>")),wa.bind("boolean",O(ga,"<x-:b>")),wa.bind("not",O(ha,"<x-:b>")),wa.bind("map",O(ia,"<fa+>")),wa.bind("reduce",O(ja,"<faj?:j>")),wa.bind("keys",O(ka,"<x-:a<s>>")),wa.bind("lookup",O(la,"<x-s:x>")),wa.bind("append",O(ma,"<xx:a>")),wa.bind("exists",O(na,"<x:b>")),wa.bind("spread",O(oa,"<x-:a<o>>"));var ya={S0101:"No terminating quote found in string literal",S0102:"Number out of range: {{token}}",S0103:"unsupported escape sequence: \\{{token}}",S0104:"The escape sequence \\u must be followed by 4 hex digits",S0203:"Syntax error: expected '{{value}}' before end of expression",S0202:"Syntax error: expected '{{value}}', got '{{token}}'",S0204:"Unknown operator: {{token}}",S0205:"Unexpected token: {{token}}",S0208:"Parameter {{value}} of function definition must be a variable name (start with $)",S0209:"A predicate cannot follow a grouping expression in a step.",S0210:"Each step can only have one grouping expression.",S0201:"Syntax error: {{token}}",S0206:"Unknown expression type: {{token}}",S0207:"Syntax error: unexpected end of expression",S0301:"Empty regular expressions are not allowed",S0302:"No terminating / in regular expression",S0402:"Choice groups containing parameterized types not supported",S0401:"Type parameters can only be applied to functions and arrays",T0410:"Argument {{index}} of function '{{token}}' does not match function signature",T0411:"Context value is not a compatible type with argument {{index}} of function '{{token}}'",T0412:"Argument {{index}} of function '{{token}}' must be an array of {{type}}",D1001:"Number out of range: {{value}}",D1002:"Cannot negate a non-numeric value: {{value}}",T2001:"LHS of {{token}} operator must evaluate to a number",T2002:"RHS of {{token}} operator must evaluate to a number",T1003:"Key in object structure must evaluate to a string. Got: {{value}}",T2003:"LHS of range operator (..) must evaluate to an integer",T2004:"RHS of range operator (..) must evaluate to an integer",D2005:"Left hand side of := must be a variable name (start with $)",D1004:"Regular expression matches zero length string",T2006:"RHS of function application operator ~> is not a function",T1005:"Attempted to invoke a non-function. Did you mean '${{token}}'?",T1006:"Attempted to invoke a non-function",T1007:"Attempted to partially apply a non-function. Did you mean '${{token}}'?",T1008:"Attempted to partially apply a non-function",D3001:"Attempting to invoke string function on Infinity or NaN",D3010:"Second argument of replace function cannot be an empty string",D3011:"Forth argument of replace function must evaluate to a positive number",D3012:"Attempted to replace a matched string with a non-string value",D3020:"Third argument of split function must evaluate to a positive number",D3030:"Unable to cast value to a number: {{value}}"};return ra.parser=va,ra}();"undefined"!=typeof module&&(module.exports=jsonata),function(){function a(a,b){if(b<=0)return a;var c=new Array(b).join(" ");return a=a.replace(/^\s*/,c)}function b(b){for(var c,d,e=b.length,f=!1,g=[],h=[],i={"(":")","[":"]","{":"}"},j=0;j<e;j++){var k=b[j];if(f)k===c&&(f=!1,h.pop());else if("'"===k||'"'===k)f=!0,c=k,d={type:"string",pos:j},g.push(d),h.push(d);else if(";"===k)d={type:";",pos:j},g.push(d);else if(","===k)d={type:",",pos:j},g.push(d);else if(/[\(\[\{]/.test(k))d={type:"open-block",char:k,pos:j},g.push(d),h.push(d);else if(/[\}\)\]]/.test(k)){var l=h.pop();if(i[l.char]!==k)return b;l.width=j-l.pos,d={type:"close-block",pos:j,char:k,width:l.width},g.push(d)}}var m,n,o,p=b,q=0,r=0,s=[];return g.forEach(function(b){";"===b.type||","===b.type?s[s.length-1]&&(m=p.substring(0,r+b.pos+1),n=p.substring(r+b.pos+1),o=a(n,q),p=m+"\n"+o,r+=o.length-n.length+1):"open-block"===b.type?b.width>30?(s.push(!0),q+=4,m=p.substring(0,r+b.pos+1),n=p.substring(r+b.pos+1),o=a(n,q),p=m+"\n"+o,r+=o.length-n.length+1):s.push(!1):"close-block"===b.type&&(b.width>30&&(q-=4,m=p.substring(0,r+b.pos),n=p.substring(r+b.pos),o=a(n,q),p=m+"\n"+o,r+=o.length-n.length+1),s.pop())}),p}jsonata.format=b,jsonata.functions={$append:{args:["array","array"]},$average:{args:["value"]},$boolean:{args:["value"]},$contains:{args:["str","pattern"]},$count:{args:["array"]},$exists:{args:["value"]},$join:{args:["array","separator"]},$keys:{args:["object"]},$length:{args:["string"]},$lookup:{args:["object","key"]},$lowercase:{args:["string"]},$match:{args:["str","pattern","limit"]},$map:{args:[]},$max:{args:["array"]},$min:{args:["array"]},$not:{args:["value"]},$number:{args:["value"]},$reduce:{args:[]},$replace:{args:["str","pattern","replacement","limit"]},$split:{args:["string","separator","limit"]},$spread:{args:["object"]},$string:{args:["value"]},$substring:{args:["string","start","length"]},$substringAfter:{args:["string","chars"]
},$substringBefore:{args:["string","chars"]},$sum:{args:["array"]},$trim:{args:["str"]},$uppercase:{args:["string"]}},jsonata.getFunctionSnippet=function(a){var b="";if(jsonata.functions.hasOwnProperty(a)){var c=jsonata.functions[a];b="\\"+a+"(",c.args&&(b+=c.args.map(function(a,b){return"${"+(b+1)+":"+a+"}"}).join(", ")),b+=")\n"}return b}}();