@stackone/expressions 0.12.0 → 0.13.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/README.md +45 -0
- package/dist/index.es.mjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -264,6 +264,17 @@ Determines if a given date (optionally with added years) has passed.
|
|
|
264
264
|
// Returns true if April 10, 2025 is after January 1, 2025
|
|
265
265
|
```
|
|
266
266
|
|
|
267
|
+
###### now()
|
|
268
|
+
|
|
269
|
+
Returns the current date and time.
|
|
270
|
+
|
|
271
|
+
- Returns: string (ISO 8601 format)
|
|
272
|
+
|
|
273
|
+
```js
|
|
274
|
+
// Get the current date and time
|
|
275
|
+
"{{now()}}" // Returns "2025-04-10T12:00:00.000Z" (example)
|
|
276
|
+
```
|
|
277
|
+
|
|
267
278
|
##### Array Functions
|
|
268
279
|
|
|
269
280
|
###### includes(array, value)
|
|
@@ -312,6 +323,40 @@ Checks if an array includes at least one value from another array or a single va
|
|
|
312
323
|
|
|
313
324
|
##### Object Functions
|
|
314
325
|
|
|
326
|
+
###### present(object)
|
|
327
|
+
|
|
328
|
+
Checks if an object is present (not null or undefined).
|
|
329
|
+
|
|
330
|
+
- `object` (object): The object to check
|
|
331
|
+
- Returns: boolean
|
|
332
|
+
|
|
333
|
+
```js
|
|
334
|
+
// Check if an object is present
|
|
335
|
+
"{{present({})}}" // Returns true
|
|
336
|
+
"{{present(null)}}" // Returns false
|
|
337
|
+
"{{present(undefined)}}" // Returns false
|
|
338
|
+
"{{present('string')}}" // Returns true
|
|
339
|
+
"{{present(0)}}" // Returns true
|
|
340
|
+
"{{present([])}}" // Returns true
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
###### missing(object)
|
|
344
|
+
|
|
345
|
+
Checks if an object is missing (null or undefined).
|
|
346
|
+
|
|
347
|
+
- `object` (object): The object to check
|
|
348
|
+
- Returns: boolean
|
|
349
|
+
|
|
350
|
+
```js
|
|
351
|
+
// Check if an object is not present
|
|
352
|
+
"{{missing({})}}" // Returns false
|
|
353
|
+
"{{missing(null)}}" // Returns true
|
|
354
|
+
"{{missing(undefined)}}" // Returns true
|
|
355
|
+
"{{missing('string')}}" // Returns false
|
|
356
|
+
"{{missing(0)}}" // Returns false
|
|
357
|
+
"{{missing([])}}" // Returns false
|
|
358
|
+
```
|
|
359
|
+
|
|
315
360
|
###### keys(object)
|
|
316
361
|
|
|
317
362
|
Returns the keys of an object as an array. If the input is not an object, null, or undefined, returns an empty array.
|
package/dist/index.es.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{calculateNextAnniversary as r,calculateYearsElapsed as t,dateHasPassed as e,isMissing as a,notMissing as n,isObject as o}from"@stackone/utils";import*as i from"jexl";import s from"jsonpath";import l from"lodash.get";const c=/\${([^}]+)}/g,u=(r,t)=>{try{return r.compile(t)}catch{return null}},
|
|
1
|
+
import{calculateNextAnniversary as r,calculateYearsElapsed as t,dateHasPassed as e,isMissing as a,notMissing as n,isObject as o}from"@stackone/utils";import*as i from"jexl";import s from"jsonpath";import l from"lodash.get";const c=/\${([^}]+)}/g,u=(r,t)=>{try{return r.compile(t)}catch{return null}},d=(r,t)=>{s.parse(r);const e=s.query(t,r);if(0!==e.length)return 1===e.length?e[0]:e},y=(r,t,e)=>r.replace(t,String(e)),p=(r,t,e)=>{const a=r.match(c);if(!a)return;const o=a.length,i=new Array(o);for(let r=0;r<o;r++)i[r]={toReplace:a[r],path:a[r].slice(2,-1)};return i.reduce(((r,a)=>((r,t,e,a)=>{const o=r.path.trim();if(!o)return a;try{const i=u(t,o);if(i){const t=i.evalSync(e);if(void 0!==t)return y(a,r.toReplace,t)}const s=l(e,o);return n(s)?y(a,r.toReplace,s):a}catch{return a}})(a,e,t,r)),String(r))},h=(r,t,e,a)=>{if(Array.isArray(t)){const r=parseInt(e,10);if(!isNaN(r)&&(r<0||r>=t.length))return{value:void 0,error:`Invalid array index '${e}' at '${a}'`,availableKeys:t.map(((r,t)=>t.toString()))}}return o(t)?{value:void 0,error:`Key '${e}' not found at '${a}'`,availableKeys:Object.keys(t)}:{value:void 0,error:`${r} at '${a}'`,availableKeys:[]}},v=(r,t)=>{const e=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(e.test(r)){const e=d(r,t);return void 0===e?{value:void 0,error:`Invalid or empty JSONPath: '${r}'`}:{value:e}}const a=r.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[];return((r,t)=>{if("$"!==r[0])return{value:void 0,error:"JSON path must start with $"};let e=t,a="$";for(let t=1;t<r.length;t++){const n=r[t],i=n.startsWith("[")?`${a}${n}`:`${a}.${n}`;if(Array.isArray(e)){const r=parseInt(n,10);if(isNaN(r)||r<0||r>=e.length)return h("Invalid array index",e,n,a);e=e[r],a=i}else{if(!o(e))return{value:void 0,error:`Cannot access '${n}' at '${a}' - parent is not an object`,availableKeys:[]};if(!Object.prototype.hasOwnProperty.call(e,n))return h("Key not found",e,n,a);e=e[n],a=i}}return{value:e}})(a.map((r=>r.replace(/^\['([^']+)'\]$/,"$1").replace(/^\["([^"]+)"\]$/,"$1").replace(/^\[(\d+)\]$/,"$1"))),t)}catch(r){return{value:void 0,error:`Something went wrong with evaluation of JSON path: ${String(r)}`}}},f=(s,l,c)=>{const y=s?.trim();if(null==y||""===y)throw new Error("Empty expression");const h=o(l)?l:{},f=((o=()=>new i.Jexl)=>{const s=o();return s.addFunction("nextAnniversary",((t,e)=>r({initialDate:t,format:e}))),s.addFunction("yearsElapsed",((r,e,a)=>t({startDate:r,endDate:a,format:e}))),s.addFunction("hasPassed",((r,t,a)=>e({date:r,yearsToAdd:a,format:t}))),s.addFunction("now",(()=>(new Date).toISOString())),s.addFunction("includes",((r,t)=>!(a(r)||!Array.isArray(r)||0===r.length||a(t))&&(Array.isArray(t)?t.every((t=>r.includes(t))):r.includes(t)))),s.addFunction("includesSome",((r,t)=>!(a(r)||!Array.isArray(r)||0===r.length||a(t))&&(Array.isArray(t)?t.some((t=>r.includes(t))):r.includes(t)))),s.addFunction("present",(r=>n(r))),s.addFunction("missing",(r=>a(r))),s.addFunction("keys",(r=>a(r)||"object"!=typeof r||Array.isArray(r)?[]:Object.keys(r))),s.addFunction("values",(r=>a(r)||"object"!=typeof r||Array.isArray(r)?[]:Object.values(r))),s.addBinaryOp("??",0,((r,t)=>a(r)?t:a(t)?r:r??t)),s})(),$=(r=>{if(r.startsWith("{{")&&r.endsWith("}}"))return r.slice(2,-2)})(y),m=(r=>r.replace(/\$\./g,"").trim())($??y),g=p(m,h,f);if(g)return g;if(!$&&y.startsWith("$"))return c?.incrementalJsonPath?((r,t)=>{const e=v(r,t);if(e.error)throw new Error(`${e.error}${e.availableKeys?.length?`. Available keys: ${e.availableKeys.join(", ")}`:""}`);return e.value})(y,h):((r,t)=>{try{return d(r,t)}catch{throw new Error(`Invalid JSON path: "${r}"`)}})(y,h);if(!g&&!$)return s;const A=u(f,m);if(!A||"."===m)throw new Error(`Invalid expression: "${y}"`);try{return A.evalSync(h)}catch{return}},$=(r,t)=>{try{return f(r,t)}catch{return null}},m=(r,t)=>{const e=r=>Array.isArray(r)?r.map(e):"object"==typeof r&&null!==r?m(r,t):"string"==typeof r?$(r,t):r;return Object.fromEntries(Object.entries(r).map((([r,t])=>[r,e(t)])))},g=r=>{try{return f(r),!0}catch{return!1}};export{f as evaluate,g as isValidExpression,$ as safeEvaluate,m as safeEvaluateRecord};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var e=require("@stackone/utils"),r=require("jexl"),t=require("jsonpath"),n=require("lodash.get");function a(e){var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var i=a(r);const s=/\${([^}]+)}/g,o=(e,r)=>{try{return e.compile(r)}catch{return null}},c=(e,r)=>{t.parse(e);const n=t.query(r,e);if(0!==n.length)return 1===n.length?n[0]:n},l=(e,r,t)=>e.replace(r,String(t)),u=(r,t,a)=>{const i=r.match(s);if(!i)return;const c=i.length,u=new Array(c);for(let e=0;e<c;e++)u[e]={toReplace:i[e],path:i[e].slice(2,-1)};return u.reduce(((r,i)=>((r,t,a,i)=>{const s=r.path.trim();if(!s)return i;try{const c=o(t,s);if(c){const e=c.evalSync(a);if(void 0!==e)return l(i,r.toReplace,e)}const u=n(a,s);return e.notMissing(u)?l(i,r.toReplace,u):i}catch{return i}})(i,a,t,r)),String(r))},d=(r,t,n,a)=>{if(Array.isArray(t)){const e=parseInt(n,10);if(!isNaN(e)&&(e<0||e>=t.length))return{value:void 0,error:`Invalid array index '${n}' at '${a}'`,availableKeys:t.map(((e,r)=>r.toString()))}}return e.isObject(t)?{value:void 0,error:`Key '${n}' not found at '${a}'`,availableKeys:Object.keys(t)}:{value:void 0,error:`${r} at '${a}'`,availableKeys:[]}},y=(r,t)=>{const n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(r)){const e=c(r,t);return void 0===e?{value:void 0,error:`Invalid or empty JSONPath: '${r}'`}:{value:e}}const a=r.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[];return((r,t)=>{if("$"!==r[0])return{value:void 0,error:"JSON path must start with $"};let n=t,a="$";for(let t=1;t<r.length;t++){const i=r[t],s=i.startsWith("[")?`${a}${i}`:`${a}.${i}`;if(Array.isArray(n)){const e=parseInt(i,10);if(isNaN(e)||e<0||e>=n.length)return d("Invalid array index",n,i,a);n=n[e],a=s}else{if(!e.isObject(n))return{value:void 0,error:`Cannot access '${i}' at '${a}' - parent is not an object`,availableKeys:[]};if(!Object.prototype.hasOwnProperty.call(n,i))return d("Key not found",n,i,a);n=n[i],a=s}}return{value:n}})(a.map((e=>e.replace(/^\['([^']+)'\]$/,"$1").replace(/^\["([^"]+)"\]$/,"$1").replace(/^\[(\d+)\]$/,"$1"))),t)}catch(e){return{value:void 0,error:`Something went wrong with evaluation of JSON path: ${String(e)}`}}},p=(r,t,n)=>{const a=r?.trim();if(null==a||""===a)throw new Error("Empty expression");const s=e.isObject(t)?t:{},l=((r=()=>new i.Jexl)=>{const t=r();return t.addFunction("nextAnniversary",((r,t)=>e.calculateNextAnniversary({initialDate:r,format:t}))),t.addFunction("yearsElapsed",((r,t,n)=>e.calculateYearsElapsed({startDate:r,endDate:n,format:t}))),t.addFunction("hasPassed",((r,t,n)=>e.dateHasPassed({date:r,yearsToAdd:n,format:t}))),t.addFunction("now",(()=>(new Date).toISOString())),t.addFunction("includes",((r,t)=>!(e.isMissing(r)||!Array.isArray(r)||0===r.length||e.isMissing(t))&&(Array.isArray(t)?t.every((e=>r.includes(e))):r.includes(t)))),t.addFunction("includesSome",((r,t)=>!(e.isMissing(r)||!Array.isArray(r)||0===r.length||e.isMissing(t))&&(Array.isArray(t)?t.some((e=>r.includes(e))):r.includes(t)))),t.addFunction("present",(r=>e.notMissing(r))),t.addFunction("missing",(r=>e.isMissing(r))),t.addFunction("keys",(r=>e.isMissing(r)||"object"!=typeof r||Array.isArray(r)?[]:Object.keys(r))),t.addFunction("values",(r=>e.isMissing(r)||"object"!=typeof r||Array.isArray(r)?[]:Object.values(r))),t.addBinaryOp("??",0,((r,t)=>e.isMissing(r)?t:e.isMissing(t)?r:r??t)),t})(),d=(e=>{if(e.startsWith("{{")&&e.endsWith("}}"))return e.slice(2,-2)})(a),p=(e=>e.replace(/\$\./g,"").trim())(d??a),v=u(p,s,l);if(v)return v;if(!d&&a.startsWith("$"))return n?.incrementalJsonPath?((e,r)=>{const t=y(e,r);if(t.error)throw new Error(`${t.error}${t.availableKeys?.length?`. Available keys: ${t.availableKeys.join(", ")}`:""}`);return t.value})(a,s):((e,r)=>{try{return c(e,r)}catch{throw new Error(`Invalid JSON path: "${e}"`)}})(a,s);if(!v&&!d)return r;const f=o(l,p);if(!f||"."===p)throw new Error(`Invalid expression: "${a}"`);try{return f.evalSync(s)}catch{return}},v=(e,r)=>{try{return p(e,r)}catch{return null}},f=(e,r)=>{const t=e=>Array.isArray(e)?e.map(t):"object"==typeof e&&null!==e?f(e,r):"string"==typeof e?v(e,r):e;return Object.fromEntries(Object.entries(e).map((([e,r])=>[e,t(r)])))};exports.evaluate=p,exports.isValidExpression=e=>{try{return p(e),!0}catch{return!1}},exports.safeEvaluate=v,exports.safeEvaluateRecord=f;
|