@stackone/expressions 0.15.3 → 0.17.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 +42 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -388,6 +388,48 @@ Returns the values of an object as an array. If the input is not an object, null
|
|
|
388
388
|
"{{values($.user)}}"
|
|
389
389
|
```
|
|
390
390
|
|
|
391
|
+
##### String Functions
|
|
392
|
+
|
|
393
|
+
###### decodeBase64(encodedValue)
|
|
394
|
+
|
|
395
|
+
Decodes a Base64 encoded string and returns the decoded result.
|
|
396
|
+
|
|
397
|
+
- `encodedValue` (string): The Base64 encoded string to decode
|
|
398
|
+
- Returns: decoded string, or empty string if input is invalid
|
|
399
|
+
|
|
400
|
+
```js
|
|
401
|
+
// Decode a base64 string directly
|
|
402
|
+
"{{decodeBase64("SGVsbG8gV29ybGQ")}}" // Returns "Hello World"
|
|
403
|
+
|
|
404
|
+
// Decode from context variable
|
|
405
|
+
"{{decodeBase64($.encodedValue)}}" // Decodes the encodedValue from context
|
|
406
|
+
|
|
407
|
+
// Handles invalid cases gracefully
|
|
408
|
+
"{{decodeBase64(null)}}" // Returns ""
|
|
409
|
+
"{{decodeBase64("!")}}" // Returns ""
|
|
410
|
+
"{{decodeBase64(123)}}" // Returns ""
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
###### encodeBase64(value)
|
|
414
|
+
|
|
415
|
+
Encodes a string to Base64 and returns the encoded result.
|
|
416
|
+
|
|
417
|
+
- `value` (string): The string to encode
|
|
418
|
+
- Returns: encoded string, or empty string if input is invalid
|
|
419
|
+
|
|
420
|
+
```js
|
|
421
|
+
// Encode a string directly
|
|
422
|
+
"{{encodeBase64("Hello World")}}" // Returns "SGVsbG8gV29ybGQ"
|
|
423
|
+
|
|
424
|
+
// Encode from context variable
|
|
425
|
+
"{{encodeBase64($.value)}}" // Encodes the value from context
|
|
426
|
+
|
|
427
|
+
// Handles invalid cases gracefully
|
|
428
|
+
"{{encodeBase64(null)}}" // Returns ""
|
|
429
|
+
"{{encodeBase64(123)}}" // Returns ""
|
|
430
|
+
"{{encodeBase64("")}}" // Returns ""
|
|
431
|
+
```
|
|
432
|
+
|
|
391
433
|
For more information on the JEXL syntax, refer to the [JEXL Syntax documentation](https://commons.apache.org/proper/commons-jexl/reference/syntax.html).
|
|
392
434
|
|
|
393
435
|
### String Interpolation
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`@stackone/utils`)),l=s(require(`jexl`)),u=s(require(`jsonpath-plus`)),d=
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`@stackone/utils`)),l=s(require(`jexl`)),u=s(require(`jsonpath-plus`)),d=(e=()=>new l.Jexl)=>{let t=e();return t.addFunction(`nextAnniversary`,(e,t)=>(0,c.calculateNextAnniversary)({initialDate:e,format:t})),t.addFunction(`yearsElapsed`,(e,t,n)=>(0,c.calculateYearsElapsed)({startDate:e,endDate:n,format:t})),t.addFunction(`hasPassed`,(e,t,n)=>(0,c.dateHasPassed)({date:e,yearsToAdd:n,format:t})),t.addFunction(`now`,()=>new Date().toISOString()),t.addFunction(`includes`,(e,t)=>(0,c.isMissing)(e)||!Array.isArray(e)||e.length===0||(0,c.isMissing)(t)?!1:Array.isArray(t)?t.every(t=>e.includes(t)):e.includes(t)),t.addFunction(`includesSome`,(e,t)=>(0,c.isMissing)(e)||!Array.isArray(e)||e.length===0||(0,c.isMissing)(t)?!1:Array.isArray(t)?t.some(t=>e.includes(t)):e.includes(t)),t.addFunction(`present`,e=>(0,c.notMissing)(e)),t.addFunction(`missing`,e=>(0,c.isMissing)(e)),t.addFunction(`keys`,e=>(0,c.isMissing)(e)?[]:typeof e==`object`&&!Array.isArray(e)?Object.keys(e):[]),t.addFunction(`values`,e=>(0,c.isMissing)(e)?[]:typeof e==`object`&&!Array.isArray(e)?Object.values(e):[]),t.addFunction(`decodeBase64`,e=>{if((0,c.isMissing)(e)||typeof e!=`string`)return``;try{return(0,c.decodeFromBase64)(e)}catch{return``}}),t.addFunction(`encodeBase64`,e=>{if((0,c.isMissing)(e)||typeof e!=`string`)return``;try{return(0,c.encodeToBase64)(e)}catch{return``}}),t.addBinaryOp(`??`,0,(e,t)=>(0,c.isMissing)(e)?t:(0,c.isMissing)(t)?e:e??t),t},f=/\${([^}]+)}/g,p=e=>e.replace(/\$\./g,``).trim(),m=(e,t)=>{try{let n=e.compile(t);return n}catch{return null}},h=e=>{if(e.startsWith(`{{`)&&e.endsWith(`}}`))return e.slice(2,-2)},g=(e,t)=>{try{if(!e.startsWith(`$`))throw Error(`JSONPath expression must start with '$': ${e}`);if(e.includes(`$_`)||e.match(/\$[^.[]/g)||e.includes(`[`)&&!e.includes(`]`)||e.includes(` `)&&!e.includes(`[`))throw Error(`Invalid JSONPath expression: ${e}`);let n=(0,u.JSONPath)({path:e,json:t});return n.length===0?void 0:n.length===1?n[0]:n}catch{throw Error(`Invalid JSONPath expression: ${e}`)}},_=(e,t,n)=>e.replace(t,String(n)),v=(e,t,n,r)=>{let i=e.path.trim();if(!i)return r;try{let a=m(t,i);if(a){let t=a.evalSync(n);if(t!==void 0)return _(r,e.toReplace,t)}let o=S(`$.${i}`,n),s=o.value;return(0,c.notMissing)(s)?_(r,e.toReplace,s):r}catch{return r}},y=(e,t,n)=>{let r=e.match(f);if(!r)return;let i=r.length,a=Array(i);for(let e=0;e<i;e++)a[e]={toReplace:r[e],path:r[e].slice(2,-1)};return a.reduce((e,r)=>v(r,n,t,e),String(e))},b=(e,t,n,r)=>{if(Array.isArray(t)){let e=parseInt(n,10);if(!isNaN(e)&&(e<0||e>=t.length))return{value:void 0,error:`Invalid array index '${n}' at '${r}'`,availableKeys:t.map((e,t)=>t.toString())}}return(0,c.isObject)(t)?{value:void 0,error:`Key '${n}' not found at '${r}'`,availableKeys:Object.keys(t)}:{value:void 0,error:`${e} at '${r}'`,availableKeys:[]}},x=(e,t)=>{if(e[0]!==`$`)return{value:void 0,error:`JSON path must start with $`};let n=t,r=`$`;for(let t=1;t<e.length;t++){let i=e[t],a=i.startsWith(`[`)?`${r}${i}`:`${r}.${i}`;if(Array.isArray(n)){let e=parseInt(i,10);if(isNaN(e)||e<0||e>=n.length)return b(`Invalid array index`,n,i,r);n=n[e],r=a}else if((0,c.isObject)(n)){if(!Object.prototype.hasOwnProperty.call(n,i))return b(`Key not found`,n,i,r);n=n[i],r=a}else return{value:void 0,error:`Cannot access '${i}' at '${r}' - parent is not an object`,availableKeys:[]}}return{value:n}},S=(e,t)=>{let n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){let n=g(e,t);return n===void 0?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:n}}let r=e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[],i=r.map(e=>e.replace(/^\['([^']+)'\]$/,`$1`).replace(/^\["([^"]+)"\]$/,`$1`).replace(/^\[(\d+)\]$/,`$1`));return x(i,t)}catch(e){return{value:void 0,error:`Something went wrong with evaluation of JSON path: ${String(e)}`}}},C=(e,t)=>{let n=S(e,t);if(n.error)throw Error(`${n.error}${n.availableKeys?.length?`. Available keys: ${n.availableKeys.join(`, `)}`:``}`);return n.value},w=(e,t)=>{try{return g(e,t)}catch{throw Error(`Invalid JSON path: "${e}"`)}},T=(e,t,n)=>{let r=e?.trim();if(r==null||r===``)throw Error(`Empty expression`);let i=(0,c.isObject)(t)?t:{},a=d(),o=h(r),s=o??r,l=p(s),u=y(l,i,a);if(u)return u;if(!o&&r.startsWith(`$`))return n?.incrementalJsonPath?C(r,i):w(r,i);if(!u&&!o)return e;let f=m(a,l);if(!f||l===`.`)throw Error(`Invalid expression: "${r}"`);try{return f.evalSync(i)}catch{return}},E=(e,t)=>{try{return T(e,t)}catch{return null}},D=(e,t)=>{let n=e=>Array.isArray(e)?e.map(n):typeof e==`object`&&e?D(e,t):typeof e==`string`?E(e,t):e;return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,n(t)]))},O=e=>{try{return T(e),!0}catch{return!1}};exports.evaluate=T,exports.isValidExpression=O,exports.safeEvaluate=E,exports.safeEvaluateRecord=D;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{calculateNextAnniversary as e,calculateYearsElapsed as t,dateHasPassed as n,
|
|
1
|
+
import{calculateNextAnniversary as e,calculateYearsElapsed as t,dateHasPassed as n,decodeFromBase64 as r,encodeToBase64 as i,isMissing as a,isObject as o,notMissing as s}from"@stackone/utils";import*as c from"jexl";import{JSONPath as l}from"jsonpath-plus";const u=(o=()=>new c.Jexl)=>{let l=o();return l.addFunction(`nextAnniversary`,(t,n)=>e({initialDate:t,format:n})),l.addFunction(`yearsElapsed`,(e,n,r)=>t({startDate:e,endDate:r,format:n})),l.addFunction(`hasPassed`,(e,t,r)=>n({date:e,yearsToAdd:r,format:t})),l.addFunction(`now`,()=>new Date().toISOString()),l.addFunction(`includes`,(e,t)=>a(e)||!Array.isArray(e)||e.length===0||a(t)?!1:Array.isArray(t)?t.every(t=>e.includes(t)):e.includes(t)),l.addFunction(`includesSome`,(e,t)=>a(e)||!Array.isArray(e)||e.length===0||a(t)?!1:Array.isArray(t)?t.some(t=>e.includes(t)):e.includes(t)),l.addFunction(`present`,e=>s(e)),l.addFunction(`missing`,e=>a(e)),l.addFunction(`keys`,e=>a(e)?[]:typeof e==`object`&&!Array.isArray(e)?Object.keys(e):[]),l.addFunction(`values`,e=>a(e)?[]:typeof e==`object`&&!Array.isArray(e)?Object.values(e):[]),l.addFunction(`decodeBase64`,e=>{if(a(e)||typeof e!=`string`)return``;try{return r(e)}catch{return``}}),l.addFunction(`encodeBase64`,e=>{if(a(e)||typeof e!=`string`)return``;try{return i(e)}catch{return``}}),l.addBinaryOp(`??`,0,(e,t)=>a(e)?t:a(t)?e:e??t),l},d=/\${([^}]+)}/g,f=e=>e.replace(/\$\./g,``).trim(),p=(e,t)=>{try{let n=e.compile(t);return n}catch{return null}},m=e=>{if(e.startsWith(`{{`)&&e.endsWith(`}}`))return e.slice(2,-2)},h=(e,t)=>{try{if(!e.startsWith(`$`))throw Error(`JSONPath expression must start with '$': ${e}`);if(e.includes(`$_`)||e.match(/\$[^.[]/g)||e.includes(`[`)&&!e.includes(`]`)||e.includes(` `)&&!e.includes(`[`))throw Error(`Invalid JSONPath expression: ${e}`);let n=l({path:e,json:t});return n.length===0?void 0:n.length===1?n[0]:n}catch{throw Error(`Invalid JSONPath expression: ${e}`)}},g=(e,t,n)=>e.replace(t,String(n)),_=(e,t,n,r)=>{let i=e.path.trim();if(!i)return r;try{let a=p(t,i);if(a){let t=a.evalSync(n);if(t!==void 0)return g(r,e.toReplace,t)}let o=x(`$.${i}`,n),c=o.value;return s(c)?g(r,e.toReplace,c):r}catch{return r}},v=(e,t,n)=>{let r=e.match(d);if(!r)return;let i=r.length,a=Array(i);for(let e=0;e<i;e++)a[e]={toReplace:r[e],path:r[e].slice(2,-1)};return a.reduce((e,r)=>_(r,n,t,e),String(e))},y=(e,t,n,r)=>{if(Array.isArray(t)){let e=parseInt(n,10);if(!isNaN(e)&&(e<0||e>=t.length))return{value:void 0,error:`Invalid array index '${n}' at '${r}'`,availableKeys:t.map((e,t)=>t.toString())}}return o(t)?{value:void 0,error:`Key '${n}' not found at '${r}'`,availableKeys:Object.keys(t)}:{value:void 0,error:`${e} at '${r}'`,availableKeys:[]}},b=(e,t)=>{if(e[0]!==`$`)return{value:void 0,error:`JSON path must start with $`};let n=t,r=`$`;for(let t=1;t<e.length;t++){let i=e[t],a=i.startsWith(`[`)?`${r}${i}`:`${r}.${i}`;if(Array.isArray(n)){let e=parseInt(i,10);if(isNaN(e)||e<0||e>=n.length)return y(`Invalid array index`,n,i,r);n=n[e],r=a}else if(o(n)){if(!Object.prototype.hasOwnProperty.call(n,i))return y(`Key not found`,n,i,r);n=n[i],r=a}else return{value:void 0,error:`Cannot access '${i}' at '${r}' - parent is not an object`,availableKeys:[]}}return{value:n}},x=(e,t)=>{let n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){let n=h(e,t);return n===void 0?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:n}}let r=e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[],i=r.map(e=>e.replace(/^\['([^']+)'\]$/,`$1`).replace(/^\["([^"]+)"\]$/,`$1`).replace(/^\[(\d+)\]$/,`$1`));return b(i,t)}catch(e){return{value:void 0,error:`Something went wrong with evaluation of JSON path: ${String(e)}`}}},S=(e,t)=>{let n=x(e,t);if(n.error)throw Error(`${n.error}${n.availableKeys?.length?`. Available keys: ${n.availableKeys.join(`, `)}`:``}`);return n.value},C=(e,t)=>{try{return h(e,t)}catch{throw Error(`Invalid JSON path: "${e}"`)}},w=(e,t,n)=>{let r=e?.trim();if(r==null||r===``)throw Error(`Empty expression`);let i=o(t)?t:{},a=u(),s=m(r),c=s??r,l=f(c),d=v(l,i,a);if(d)return d;if(!s&&r.startsWith(`$`))return n?.incrementalJsonPath?S(r,i):C(r,i);if(!d&&!s)return e;let h=p(a,l);if(!h||l===`.`)throw Error(`Invalid expression: "${r}"`);try{return h.evalSync(i)}catch{return}},T=(e,t)=>{try{return w(e,t)}catch{return null}},E=(e,t)=>{let n=e=>Array.isArray(e)?e.map(n):typeof e==`object`&&e?E(e,t):typeof e==`string`?T(e,t):e;return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,n(t)]))},D=e=>{try{return w(e),!0}catch{return!1}};export{w as evaluate,D as isValidExpression,T as safeEvaluate,E as safeEvaluateRecord};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackone/expressions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"lint:fix": "npm run code:check:fix",
|
|
35
35
|
"test": "vitest run",
|
|
36
36
|
"test:watch": "vitest watch",
|
|
37
|
+
"test:typecheck": "tsc --noEmit --project tsconfig.tests.json",
|
|
37
38
|
"publish-release": "npm publish --access=public"
|
|
38
39
|
},
|
|
39
40
|
"keywords": [],
|
|
@@ -42,8 +43,7 @@
|
|
|
42
43
|
"dependencies": {
|
|
43
44
|
"@stackone/utils": "*",
|
|
44
45
|
"jexl": "^2.3.0",
|
|
45
|
-
"jsonpath-plus": "^10.3.0"
|
|
46
|
-
"lodash.get": "^4.4.2"
|
|
46
|
+
"jsonpath-plus": "^10.3.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/jexl": "^2.3.4",
|