jsonata-ui-core
Version:
Core AST and serializers for jsonata-ui
3 lines (2 loc) • 2.03 kB
JavaScript
function e(e){return/\s/.test(e)||["null","false","true"].includes(e)||/^\d/.test(e)||!/^[a-zA-Z()._]+$/.test(e)?"`"+e+"`":e}exports.serializer=function r(n){if(n){if("binary"===n.type)return r(n.lhs)+" "+n.value+" "+r(n.rhs);if("function"===n.type)return r(n.procedure)+"("+(n.arguments?n.arguments.map(r).join(", "):"")+")";if("variable"===n.type){var t="";n.stages&&(t=n.stages.map(r).join(""));var u=n.predicate?n.predicate.map(r).join():"";return"$"+n.value+u+t}if("wildcard"===n.type)return n.value;if("descendant"===n.type)return n.value;if("number"===n.type)return JSON.stringify(n.value);if("string"===n.type)return JSON.stringify(n.value);if("name"===n.type){var i="";n.stages&&(i=n.stages.map(r).join(""));var a="";n.focus&&(a="@"+(n.index?"$"+n.index:"")+(n.focus?"$"+n.focus:""));var s=n.value;return(s=e(s))+a+i}if("filter"===n.type)return"["+r(n.expr)+"]";if("bind"===n.type)return r(n.lhs)+" "+n.value+" "+r(n.rhs);if("lambda"===n.type)return"function ("+n.arguments.map(function(e){return r(e)}).join(", ")+") {"+r(n.body)+"}";if("condition"===n.type)return r(n.condition)+" ? "+r(n.then)+(n.else?" : "+r(n.else):"");if("value"===n.type){if(null===n.value)return"null";if(!1===n.value)return"false";if(!0===n.value)return"true";throw Error("Unhandled value node "+n.value)}if("block"===n.type)return"("+n.expressions.map(r).join("; ")+")";if("path"===n.type)return n.steps.reduce(function(e,n){return(e.length?e.concat(["sort"===n.type?"^":"."]):[]).concat([r(n)])},[]).join("");if("apply"===n.type)return r(n.lhs)+" "+n.value+" "+r(n.rhs);if("sort"===n.type)return"("+n.terms.map(function(e){return e.descending?">":"<"+r(e.expression)})+")";if("unary"===n.type){if("{"===n.value&&"unary"===n.type)return n.value+"\n\t"+n.lhs.map(function(e){return r(e[0])+":"+r(e[1])}).join(",\n\t")+"\n}";if("["===n.value)return n.value+n.expressions.map(r).join(", ")+"]";throw Error("Unhandled unary node "+n.value)}return"Error: Invalid node type."}},exports.escapeString=e,exports.module=!0;
//# sourceMappingURL=index.js.map