@stackone/expressions 0.12.0 → 0.14.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 +48 -2
- package/dist/index.es.mjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -111,7 +111,7 @@ When the expression starts with `$`, it is treated as a JSON Path expression and
|
|
|
111
111
|
| `*` | Wildcard. All elements in an array, or all properties of an object |
|
|
112
112
|
| `..` | Recursive descent |
|
|
113
113
|
| `[]` | Subscript operator |
|
|
114
|
-
| `[,]` | Union operator |
|
|
114
|
+
| `[,]` | Union operator (e.g., `$.a[b,c]` for multiple properties) |
|
|
115
115
|
| `[start:end:step]` | Array slice operator |
|
|
116
116
|
| `?(expression)` | Filter expression |
|
|
117
117
|
| `()` | Script expression |
|
|
@@ -123,10 +123,11 @@ Examples:
|
|
|
123
123
|
'$.user.name' // Returns "John"
|
|
124
124
|
'$.user.age' // Returns 30
|
|
125
125
|
'$.user[*]' // Returns ["John", 30]
|
|
126
|
+
'$.user[name,age]' // Returns ["John", 30] (union operator)
|
|
126
127
|
'$["info/email"]' // Returns "info@email.com"
|
|
127
128
|
```
|
|
128
129
|
|
|
129
|
-
For more information on JSON Path syntax, refer to the original [JSON Path
|
|
130
|
+
For more information on JSON Path syntax, refer to the [JSONPath Plus documentation](https://github.com/s3u/JSONPath) and the original [JSON Path specification](https://goessner.net/articles/JsonPath/).
|
|
130
131
|
|
|
131
132
|
### JEXL Expressions
|
|
132
133
|
|
|
@@ -264,6 +265,17 @@ Determines if a given date (optionally with added years) has passed.
|
|
|
264
265
|
// Returns true if April 10, 2025 is after January 1, 2025
|
|
265
266
|
```
|
|
266
267
|
|
|
268
|
+
###### now()
|
|
269
|
+
|
|
270
|
+
Returns the current date and time.
|
|
271
|
+
|
|
272
|
+
- Returns: string (ISO 8601 format)
|
|
273
|
+
|
|
274
|
+
```js
|
|
275
|
+
// Get the current date and time
|
|
276
|
+
"{{now()}}" // Returns "2025-04-10T12:00:00.000Z" (example)
|
|
277
|
+
```
|
|
278
|
+
|
|
267
279
|
##### Array Functions
|
|
268
280
|
|
|
269
281
|
###### includes(array, value)
|
|
@@ -312,6 +324,40 @@ Checks if an array includes at least one value from another array or a single va
|
|
|
312
324
|
|
|
313
325
|
##### Object Functions
|
|
314
326
|
|
|
327
|
+
###### present(object)
|
|
328
|
+
|
|
329
|
+
Checks if an object is present (not null or undefined).
|
|
330
|
+
|
|
331
|
+
- `object` (object): The object to check
|
|
332
|
+
- Returns: boolean
|
|
333
|
+
|
|
334
|
+
```js
|
|
335
|
+
// Check if an object is present
|
|
336
|
+
"{{present({})}}" // Returns true
|
|
337
|
+
"{{present(null)}}" // Returns false
|
|
338
|
+
"{{present(undefined)}}" // Returns false
|
|
339
|
+
"{{present('string')}}" // Returns true
|
|
340
|
+
"{{present(0)}}" // Returns true
|
|
341
|
+
"{{present([])}}" // Returns true
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
###### missing(object)
|
|
345
|
+
|
|
346
|
+
Checks if an object is missing (null or undefined).
|
|
347
|
+
|
|
348
|
+
- `object` (object): The object to check
|
|
349
|
+
- Returns: boolean
|
|
350
|
+
|
|
351
|
+
```js
|
|
352
|
+
// Check if an object is not present
|
|
353
|
+
"{{missing({})}}" // Returns false
|
|
354
|
+
"{{missing(null)}}" // Returns true
|
|
355
|
+
"{{missing(undefined)}}" // Returns true
|
|
356
|
+
"{{missing('string')}}" // Returns false
|
|
357
|
+
"{{missing(0)}}" // Returns false
|
|
358
|
+
"{{missing([])}}" // Returns false
|
|
359
|
+
```
|
|
360
|
+
|
|
315
361
|
###### keys(object)
|
|
316
362
|
|
|
317
363
|
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
|
|
1
|
+
import{calculateNextAnniversary as r,calculateYearsElapsed as t,dateHasPassed as e,isMissing as n,notMissing as a,isObject as i}from"@stackone/utils";import*as o from"jexl";import{JSONPath as s}from"jsonpath-plus";import l from"lodash.get";const c=/\${([^}]+)}/g,u=(r,t)=>{try{return r.compile(t)}catch{return null}},d=(r,t)=>{try{if(!r.startsWith("$"))throw new Error(`JSONPath expression must start with '$': ${r}`);if(r.includes("$_")||r.match(/\$[^.\[]/g)||r.includes("[")&&!r.includes("]")||r.includes(" ")&&!r.includes("["))throw new Error(`Invalid JSONPath expression: ${r}`);const e=s({path:r,json:t});if(0===e.length)return;return 1===e.length?e[0]:e}catch{throw new Error(`Invalid JSONPath expression: ${r}`)}},y=(r,t,e)=>r.replace(t,String(e)),h=(r,t,e)=>{const n=r.match(c);if(!n)return;const i=n.length,o=new Array(i);for(let r=0;r<i;r++)o[r]={toReplace:n[r],path:n[r].slice(2,-1)};return o.reduce(((r,n)=>((r,t,e,n)=>{const i=r.path.trim();if(!i)return n;try{const o=u(t,i);if(o){const t=o.evalSync(e);if(void 0!==t)return y(n,r.toReplace,t)}const s=l(e,i);return a(s)?y(n,r.toReplace,s):n}catch{return n}})(n,e,t,r)),String(r))},p=(r,t,e,n)=>{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 '${n}'`,availableKeys:t.map(((r,t)=>t.toString()))}}return i(t)?{value:void 0,error:`Key '${e}' not found at '${n}'`,availableKeys:Object.keys(t)}:{value:void 0,error:`${r} at '${n}'`,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 n=r.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[];return((r,t)=>{if("$"!==r[0])return{value:void 0,error:"JSON path must start with $"};let e=t,n="$";for(let t=1;t<r.length;t++){const a=r[t],o=a.startsWith("[")?`${n}${a}`:`${n}.${a}`;if(Array.isArray(e)){const r=parseInt(a,10);if(isNaN(r)||r<0||r>=e.length)return p("Invalid array index",e,a,n);e=e[r],n=o}else{if(!i(e))return{value:void 0,error:`Cannot access '${a}' at '${n}' - parent is not an object`,availableKeys:[]};if(!Object.prototype.hasOwnProperty.call(e,a))return p("Key not found",e,a,n);e=e[a],n=o}}return{value:e}})(n.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 p=i(l)?l:{},f=((i=()=>new o.Jexl)=>{const s=i();return s.addFunction("nextAnniversary",((t,e)=>r({initialDate:t,format:e}))),s.addFunction("yearsElapsed",((r,e,n)=>t({startDate:r,endDate:n,format:e}))),s.addFunction("hasPassed",((r,t,n)=>e({date:r,yearsToAdd:n,format:t}))),s.addFunction("now",(()=>(new Date).toISOString())),s.addFunction("includes",((r,t)=>!(n(r)||!Array.isArray(r)||0===r.length||n(t))&&(Array.isArray(t)?t.every((t=>r.includes(t))):r.includes(t)))),s.addFunction("includesSome",((r,t)=>!(n(r)||!Array.isArray(r)||0===r.length||n(t))&&(Array.isArray(t)?t.some((t=>r.includes(t))):r.includes(t)))),s.addFunction("present",(r=>a(r))),s.addFunction("missing",(r=>n(r))),s.addFunction("keys",(r=>n(r)||"object"!=typeof r||Array.isArray(r)?[]:Object.keys(r))),s.addFunction("values",(r=>n(r)||"object"!=typeof r||Array.isArray(r)?[]:Object.values(r))),s.addBinaryOp("??",0,((r,t)=>n(r)?t:n(t)?r:r??t)),s})(),$=(r=>{if(r.startsWith("{{")&&r.endsWith("}}"))return r.slice(2,-2)})(y),m=(r=>r.replace(/\$\./g,"").trim())($??y),w=h(m,p,f);if(w)return w;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,p):((r,t)=>{try{return d(r,t)}catch{throw new Error(`Invalid JSON path: "${r}"`)}})(y,p);if(!w&&!$)return s;const g=u(f,m);if(!g||"."===m)throw new Error(`Invalid expression: "${y}"`);try{return g.evalSync(p)}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)])))},w=r=>{try{return f(r),!0}catch{return!1}};export{f as evaluate,w as isValidExpression,$ as safeEvaluate,m as safeEvaluateRecord};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var r=require("@stackone/utils"),e=require("jexl"),t=require("jsonpath"),
|
|
1
|
+
"use strict";var r=require("@stackone/utils"),e=require("jexl"),t=require("jsonpath-plus"),n=require("lodash.get");function a(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=a(e);const s=/\${([^}]+)}/g,o=(r,e)=>{try{return r.compile(e)}catch{return null}},c=(r,e)=>{try{if(!r.startsWith("$"))throw new Error(`JSONPath expression must start with '$': ${r}`);if(r.includes("$_")||r.match(/\$[^.\[]/g)||r.includes("[")&&!r.includes("]")||r.includes(" ")&&!r.includes("["))throw new Error(`Invalid JSONPath expression: ${r}`);const n=t.JSONPath({path:r,json:e});if(0===n.length)return;return 1===n.length?n[0]:n}catch{throw new Error(`Invalid JSONPath expression: ${r}`)}},l=(r,e,t)=>r.replace(e,String(t)),u=(e,t,a)=>{const i=e.match(s);if(!i)return;const c=i.length,u=new Array(c);for(let r=0;r<c;r++)u[r]={toReplace:i[r],path:i[r].slice(2,-1)};return u.reduce(((e,i)=>((e,t,a,i)=>{const s=e.path.trim();if(!s)return i;try{const c=o(t,s);if(c){const r=c.evalSync(a);if(void 0!==r)return l(i,e.toReplace,r)}const u=n(a,s);return r.notMissing(u)?l(i,e.toReplace,u):i}catch{return i}})(i,a,t,e)),String(e))},d=(e,t,n,a)=>{if(Array.isArray(t)){const r=parseInt(n,10);if(!isNaN(r)&&(r<0||r>=t.length))return{value:void 0,error:`Invalid array index '${n}' at '${a}'`,availableKeys:t.map(((r,e)=>e.toString()))}}return r.isObject(t)?{value:void 0,error:`Key '${n}' not found at '${a}'`,availableKeys:Object.keys(t)}:{value:void 0,error:`${e} at '${a}'`,availableKeys:[]}},y=(e,t)=>{const n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){const r=c(e,t);return void 0===r?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:r}}const a=e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[];return((e,t)=>{if("$"!==e[0])return{value:void 0,error:"JSON path must start with $"};let n=t,a="$";for(let t=1;t<e.length;t++){const i=e[t],s=i.startsWith("[")?`${a}${i}`:`${a}.${i}`;if(Array.isArray(n)){const r=parseInt(i,10);if(isNaN(r)||r<0||r>=n.length)return d("Invalid array index",n,i,a);n=n[r],a=s}else{if(!r.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((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)}`}}},h=(e,t,n)=>{const a=e?.trim();if(null==a||""===a)throw new Error("Empty expression");const s=r.isObject(t)?t:{},l=((e=()=>new i.Jexl)=>{const t=e();return t.addFunction("nextAnniversary",((e,t)=>r.calculateNextAnniversary({initialDate:e,format:t}))),t.addFunction("yearsElapsed",((e,t,n)=>r.calculateYearsElapsed({startDate:e,endDate:n,format:t}))),t.addFunction("hasPassed",((e,t,n)=>r.dateHasPassed({date:e,yearsToAdd:n,format:t}))),t.addFunction("now",(()=>(new Date).toISOString())),t.addFunction("includes",((e,t)=>!(r.isMissing(e)||!Array.isArray(e)||0===e.length||r.isMissing(t))&&(Array.isArray(t)?t.every((r=>e.includes(r))):e.includes(t)))),t.addFunction("includesSome",((e,t)=>!(r.isMissing(e)||!Array.isArray(e)||0===e.length||r.isMissing(t))&&(Array.isArray(t)?t.some((r=>e.includes(r))):e.includes(t)))),t.addFunction("present",(e=>r.notMissing(e))),t.addFunction("missing",(e=>r.isMissing(e))),t.addFunction("keys",(e=>r.isMissing(e)||"object"!=typeof e||Array.isArray(e)?[]:Object.keys(e))),t.addFunction("values",(e=>r.isMissing(e)||"object"!=typeof e||Array.isArray(e)?[]:Object.values(e))),t.addBinaryOp("??",0,((e,t)=>r.isMissing(e)?t:r.isMissing(t)?e:e??t)),t})(),d=(r=>{if(r.startsWith("{{")&&r.endsWith("}}"))return r.slice(2,-2)})(a),h=(r=>r.replace(/\$\./g,"").trim())(d??a),p=u(h,s,l);if(p)return p;if(!d&&a.startsWith("$"))return n?.incrementalJsonPath?((r,e)=>{const t=y(r,e);if(t.error)throw new Error(`${t.error}${t.availableKeys?.length?`. Available keys: ${t.availableKeys.join(", ")}`:""}`);return t.value})(a,s):((r,e)=>{try{return c(r,e)}catch{throw new Error(`Invalid JSON path: "${r}"`)}})(a,s);if(!p&&!d)return e;const v=o(l,h);if(!v||"."===h)throw new Error(`Invalid expression: "${a}"`);try{return v.evalSync(s)}catch{return}},p=(r,e)=>{try{return h(r,e)}catch{return null}},v=(r,e)=>{const t=r=>Array.isArray(r)?r.map(t):"object"==typeof r&&null!==r?v(r,e):"string"==typeof r?p(r,e):r;return Object.fromEntries(Object.entries(r).map((([r,e])=>[r,t(e)])))};exports.evaluate=h,exports.isValidExpression=r=>{try{return h(r),!0}catch{return!1}},exports.safeEvaluate=p,exports.safeEvaluateRecord=v;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackone/expressions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.es.mjs",
|
|
@@ -34,12 +34,11 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@stackone/utils": "*",
|
|
36
36
|
"jexl": "^2.3.0",
|
|
37
|
-
"jsonpath": "^
|
|
37
|
+
"jsonpath-plus": "^10.3.0",
|
|
38
38
|
"lodash.get": "^4.4.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/jexl": "^2.3.4",
|
|
42
|
-
"@types/jsonpath": "^0.2.4",
|
|
43
42
|
"@types/lodash.get": "^4.4.9"
|
|
44
43
|
}
|
|
45
44
|
}
|