@stackone/expressions 0.4.0 → 0.5.0
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/dist/index.es.mjs +1 -1
- package/dist/index.js +1 -1
- package/dist/types/evaluate.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.es.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{notMissing as r,isObject as t}from"@stackone/utils";import*as e from"jexl";import n from"jsonpath";import o from"lodash.get";const a=/\${([^}]+)}/g,
|
|
1
|
+
import{notMissing as r,isObject as t}from"@stackone/utils";import*as e from"jexl";import n from"jsonpath";import o from"lodash.get";const a=/\${([^}]+)}/g,c=(c,s)=>{const i=c?.trim();if(null==i||""===i)throw new Error("Empty expression");const l=t(s)?s:{},u=(r=>{if(r.startsWith("{{")&&r.endsWith("}}"))return r.slice(2,-2)})(i),p=u??i,h=((r=()=>new e.Jexl)=>r())(),f=(r=>r.replace(/\$\./g,"").trim())(p),m=((t,e)=>{const n=t.match(a)?.map((r=>({toReplace:r,path:r.slice(2,-1)})));if(!n)return;let c=String(t);for(const t of n){const n=o(e,t.path);r(n)&&(c=c.replace(t.toReplace,`${String(n)}`))}return c})(f,l);if(m)return m;try{if(!u&&i.startsWith("$"))return((r,t)=>(n.parse(r),n.query(t,r)[0]))(i,l)}catch(r){throw new Error(`Invalid JSON path: "${i}"`)}if(!m&&!u)return c;const y=((r,t)=>{try{return r.compile(t)}catch(r){return null}})(h,f);if(!y||"."===f)throw new Error(`Invalid expression: "${i}"`);try{return y.evalSync(l)}catch(r){return}},s=(r,t)=>{try{return c(r,t)}catch(r){return null}},i=r=>{try{return c(r),!0}catch(r){return!1}};export{c as evaluate,i as isValidExpression,s as safeEvaluate};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var r=require("@stackone/utils"),e=require("jexl"),t=require("jsonpath"),n=require("lodash.get");function
|
|
1
|
+
"use strict";var r=require("@stackone/utils"),e=require("jexl"),t=require("jsonpath"),n=require("lodash.get");function c(r){var e=Object.create(null);return r&&Object.keys(r).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(r,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:function(){return r[t]}})}})),e.default=r,Object.freeze(e)}var i=c(e);const a=/\${([^}]+)}/g,s=(e,c)=>{const s=e?.trim();if(null==s||""===s)throw new Error("Empty expression");const o=r.isObject(c)?c:{},u=(r=>{if(r.startsWith("{{")&&r.endsWith("}}"))return r.slice(2,-2)})(s),l=u??s,p=((r=()=>new i.Jexl)=>r())(),f=(r=>r.replace(/\$\./g,"").trim())(l),h=((e,t)=>{const c=e.match(a)?.map((r=>({toReplace:r,path:r.slice(2,-1)})));if(!c)return;let i=String(e);for(const e of c){const c=n(t,e.path);r.notMissing(c)&&(i=i.replace(e.toReplace,`${String(c)}`))}return i})(f,o);if(h)return h;try{if(!u&&s.startsWith("$"))return((r,e)=>(t.parse(r),t.query(e,r)[0]))(s,o)}catch(r){throw new Error(`Invalid JSON path: "${s}"`)}if(!h&&!u)return e;const y=((r,e)=>{try{return r.compile(e)}catch(r){return null}})(p,f);if(!y||"."===f)throw new Error(`Invalid expression: "${s}"`);try{return y.evalSync(o)}catch(r){return}};exports.evaluate=s,exports.isValidExpression=r=>{try{return s(r),!0}catch(r){return!1}},exports.safeEvaluate=(r,e)=>{try{return s(r,e)}catch(r){return null}};
|
package/dist/types/evaluate.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export declare const evaluateExpression: (expression?: string | null, context?: unknown) => unknown;
|
|
2
2
|
export declare const safeEvaluateExpression: (expression?: string | null, context?: unknown) => unknown;
|
|
3
|
+
export declare const safeEvaluateRecord: (record: Record<string, unknown>, context?: unknown) => Record<string, unknown>;
|