@stackone/expressions 0.19.0 → 0.20.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 CHANGED
@@ -513,6 +513,32 @@ Pads the start of a string with another string until it reaches the target lengt
513
513
  "{{padStart($.id, 8, '0')}}";
514
514
  ```
515
515
 
516
+ ###### regexMatch(value, pattern, groupIndex?)
517
+
518
+ Extracts a value from a string using a regular expression pattern. Returns the specified capture group, or null if no match is found.
519
+
520
+ - `value` (string): The string to search in
521
+ - `pattern` (string): The regex pattern as a string (without delimiters)
522
+ - `groupIndex` (number, optional): Capture group index to return (default: 1 for first capture group, 0 for full match)
523
+ - Returns: matched string from the specified group, or null if no match or group doesn't exist
524
+
525
+ ```js
526
+ // Extract parameter from URL or header
527
+ "{{regexMatch('<https://api.com?after=abc123&limit=2>; rel=\"next\"', 'after=([^&>]+)', 1)}}"; // Returns "abc123"
528
+
529
+ // Extract full match (group 0)
530
+ "{{regexMatch('Hello World', 'World', 0)}}"; // Returns "World"
531
+
532
+ // Extract with capture group
533
+ "{{regexMatch('user_id=12345', 'user_id=(\\d+)', 1)}}"; // Returns "12345"
534
+
535
+ // No match returns null
536
+ "{{regexMatch('Hello World', 'foo', 1)}}"; // Returns null
537
+
538
+ // Extract from context variable
539
+ "{{regexMatch($.linkHeader, 'after=([^&>]+)', 1)}}";
540
+ ```
541
+
516
542
  For more information on the JEXL syntax, refer to the [JEXL Syntax documentation](https://commons.apache.org/proper/commons-jexl/reference/syntax.html).
517
543
 
518
544
  ### String Interpolation
package/dist/index.cjs 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));let c=require(`@stackone/utils`),l=require(`jexl`);l=s(l);let u=require(`jsonpath-plus`);const d=`capitalize`,f=(e,t=`first`)=>(0,c.isMissing)(e)||typeof e!=`string`?``:(t===`each`?`each`:`first`)==`each`?e.split(/(\s+)/).map(e=>e.trim()?e.charAt(0).toUpperCase()+e.slice(1):e).join(``):e.charAt(0).toUpperCase()+e.slice(1),p=`padStart`,m=(e,t,n=` `)=>{if(e==null)return``;let r=String(e);if((0,c.isMissing)(t)||typeof t!=`number`||t<0)return r;let i=typeof n==`string`?n:` `;return r.padStart(t,i)},h=`truncate`,g=(e,t,n=`...`)=>{if((0,c.isMissing)(e)||typeof e!=`string`)return``;if((0,c.isMissing)(t)||typeof t!=`number`||t<0)return e;let r=typeof n==`string`?n:`...`;if(e.length<=t)return e;let i=Math.max(0,t-r.length);return i<=0?e.slice(0,t):e.slice(0,i)+r},_=(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.addFunction(`deltaFromNowMs`,(e,t)=>{let n=Date.now(),r;if(typeof e==`number`)r=t===`seconds`?e*1e3:e;else if(typeof e==`string`)r=t===`timestamp`?parseInt(e,10):t===`seconds`?parseFloat(e)*1e3:new Date(e).getTime();else throw Error(`Unsupported date input type`);return r-n}),t.addBinaryOp(`??`,0,(e,t)=>(0,c.isMissing)(e)?t:(0,c.isMissing)(t)?e:e??t),t.addFunction(`capitalize`,f),t.addFunction(`padStart`,m),t.addFunction(`truncate`,g),t},v=/\${([^}]+)}/g,y=e=>e.replace(/\$\./g,``).trim(),b=(e,t)=>{try{return e.compile(t)}catch{return null}},x=e=>{if(e.startsWith(`{{`)&&e.endsWith(`}}`))return e.slice(2,-2)},S=(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}`)}},C=(e,t,n)=>e.replace(t,String(n)),w=(e,t,n,r)=>{let i=e.path.trim();if(!i)return r;try{let a=b(t,i);if(a){let t=a.evalSync(n);if(t!==void 0)return C(r,e.toReplace,t)}let o=O(`$.${i}`,n).value;return(0,c.notMissing)(o)?C(r,e.toReplace,o):r}catch{return r}},T=(e,t,n)=>{let r=e.match(v);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)=>w(r,n,t,e),String(e))},E=(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:[]}},D=(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 E(`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 E(`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}},O=(e,t)=>{let n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){let n=S(e,t);return n===void 0?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:n}}return D((e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[]).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)}`}}},k=(e,t)=>{let n=O(e,t);if(n.error)throw Error(`${n.error}${n.availableKeys?.length?`. Available keys: ${n.availableKeys.join(`, `)}`:``}`);return n.value},A=(e,t)=>{try{return S(e,t)}catch{throw Error(`Invalid JSON path: "${e}"`)}},j=(e,t,n)=>{let r=e?.trim();if(r==null||r===``)throw Error(`Empty expression`);let i=(0,c.isObject)(t)?t:{},a=_(),o=x(r),s=y(o??r),l=T(s,i,a);if(l)return l;if(!o&&r.startsWith(`$`))return n?.incrementalJsonPath?k(r,i):A(r,i);if(!l&&!o)return e;let u=b(a,s);if(!u||s===`.`)throw Error(`Invalid expression: "${r}"`);try{return u.evalSync(i)}catch{return}},M=(e,t)=>{try{return j(e,t)}catch{return null}},N=(e,t)=>{let n=e=>Array.isArray(e)?e.map(n):typeof e==`object`&&e?N(e,t):typeof e==`string`?M(e,t):e;return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,n(t)]))},P=e=>{try{return j(e),!0}catch{return!1}};exports.evaluate=j,exports.isValidExpression=P,exports.safeEvaluate=M,exports.safeEvaluateRecord=N;
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));let c=require(`@stackone/utils`),l=require(`jexl`);l=s(l);let u=require(`jsonpath-plus`);const d=`capitalize`,f=(e,t=`first`)=>(0,c.isMissing)(e)||typeof e!=`string`?``:(t===`each`?`each`:`first`)==`each`?e.split(/(\s+)/).map(e=>e.trim()?e.charAt(0).toUpperCase()+e.slice(1):e).join(``):e.charAt(0).toUpperCase()+e.slice(1),p=`padStart`,m=(e,t,n=` `)=>{if(e==null)return``;let r=String(e);if((0,c.isMissing)(t)||typeof t!=`number`||t<0)return r;let i=typeof n==`string`?n:` `;return r.padStart(t,i)},h=`regexMatch`,g=(e,t,n=1)=>{if((0,c.isMissing)(e)||typeof e!=`string`||(0,c.isMissing)(t)||typeof t!=`string`||t.length===0)return null;let r=typeof n==`number`?n:1;if(r<0)return null;try{let n=new RegExp(t),i=e.match(n);if(!i)return null;let a=i[r];return a===void 0?null:a}catch{return null}},_=`truncate`,v=(e,t,n=`...`)=>{if((0,c.isMissing)(e)||typeof e!=`string`)return``;if((0,c.isMissing)(t)||typeof t!=`number`||t<0)return e;let r=typeof n==`string`?n:`...`;if(e.length<=t)return e;let i=Math.max(0,t-r.length);return i<=0?e.slice(0,t):e.slice(0,i)+r},y=(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.addFunction(`deltaFromNowMs`,(e,t)=>{let n=Date.now(),r;if(typeof e==`number`)r=t===`seconds`?e*1e3:e;else if(typeof e==`string`)r=t===`timestamp`?parseInt(e,10):t===`seconds`?parseFloat(e)*1e3:new Date(e).getTime();else throw Error(`Unsupported date input type`);return r-n}),t.addBinaryOp(`??`,0,(e,t)=>(0,c.isMissing)(e)?t:(0,c.isMissing)(t)?e:e??t),t.addFunction(`capitalize`,f),t.addFunction(`regexMatch`,g),t.addFunction(`padStart`,m),t.addFunction(`truncate`,v),t},b=/\${([^}]+)}/g,x=e=>e.replace(/\$\./g,``).trim(),S=(e,t)=>{try{return e.compile(t)}catch{return null}},C=e=>{if(e.startsWith(`{{`)&&e.endsWith(`}}`))return e.slice(2,-2)},w=(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}`)}},T=(e,t,n)=>e.replace(t,String(n)),E=(e,t,n,r)=>{let i=e.path.trim();if(!i)return r;try{let a=S(t,i);if(a){let t=a.evalSync(n);if(t!==void 0)return T(r,e.toReplace,t)}let o=A(`$.${i}`,n).value;return(0,c.notMissing)(o)?T(r,e.toReplace,o):r}catch{return r}},D=(e,t,n)=>{let r=e.match(b);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)=>E(r,n,t,e),String(e))},O=(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:[]}},k=(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 O(`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 O(`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}},A=(e,t)=>{let n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){let n=w(e,t);return n===void 0?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:n}}return k((e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[]).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)}`}}},j=(e,t)=>{let n=A(e,t);if(n.error)throw Error(`${n.error}${n.availableKeys?.length?`. Available keys: ${n.availableKeys.join(`, `)}`:``}`);return n.value},M=(e,t)=>{try{return w(e,t)}catch{throw Error(`Invalid JSON path: "${e}"`)}},N=(e,t,n)=>{let r=e?.trim();if(r==null||r===``)throw Error(`Empty expression`);let i=(0,c.isObject)(t)?t:{},a=y(),o=C(r),s=x(o??r),l=D(s,i,a);if(l)return l;if(!o&&r.startsWith(`$`))return n?.incrementalJsonPath?j(r,i):M(r,i);if(!l&&!o)return e;let u=S(a,s);if(!u||s===`.`)throw Error(`Invalid expression: "${r}"`);try{return u.evalSync(i)}catch{return}},P=(e,t)=>{try{return N(e,t)}catch{return null}},F=(e,t)=>{let n=e=>Array.isArray(e)?e.map(n):typeof e==`object`&&e?F(e,t):typeof e==`string`?P(e,t):e;return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,n(t)]))},I=e=>{try{return N(e),!0}catch{return!1}};exports.evaluate=N,exports.isValidExpression=I,exports.safeEvaluate=P,exports.safeEvaluateRecord=F;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
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=(e,t=`first`)=>a(e)||typeof e!=`string`?``:(t===`each`?`each`:`first`)==`each`?e.split(/(\s+)/).map(e=>e.trim()?e.charAt(0).toUpperCase()+e.slice(1):e).join(``):e.charAt(0).toUpperCase()+e.slice(1),d=(e,t,n=` `)=>{if(e==null)return``;let r=String(e);if(a(t)||typeof t!=`number`||t<0)return r;let i=typeof n==`string`?n:` `;return r.padStart(t,i)},f=(e,t,n=`...`)=>{if(a(e)||typeof e!=`string`)return``;if(a(t)||typeof t!=`number`||t<0)return e;let r=typeof n==`string`?n:`...`;if(e.length<=t)return e;let i=Math.max(0,t-r.length);return i<=0?e.slice(0,t):e.slice(0,i)+r},p=(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.addFunction(`deltaFromNowMs`,(e,t)=>{let n=Date.now(),r;if(typeof e==`number`)r=t===`seconds`?e*1e3:e;else if(typeof e==`string`)r=t===`timestamp`?parseInt(e,10):t===`seconds`?parseFloat(e)*1e3:new Date(e).getTime();else throw Error(`Unsupported date input type`);return r-n}),l.addBinaryOp(`??`,0,(e,t)=>a(e)?t:a(t)?e:e??t),l.addFunction(`capitalize`,u),l.addFunction(`padStart`,d),l.addFunction(`truncate`,f),l},m=/\${([^}]+)}/g,h=e=>e.replace(/\$\./g,``).trim(),g=(e,t)=>{try{return e.compile(t)}catch{return null}},_=e=>{if(e.startsWith(`{{`)&&e.endsWith(`}}`))return e.slice(2,-2)},v=(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}`)}},y=(e,t,n)=>e.replace(t,String(n)),b=(e,t,n,r)=>{let i=e.path.trim();if(!i)return r;try{let a=g(t,i);if(a){let t=a.evalSync(n);if(t!==void 0)return y(r,e.toReplace,t)}let o=w(`$.${i}`,n).value;return s(o)?y(r,e.toReplace,o):r}catch{return r}},x=(e,t,n)=>{let r=e.match(m);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)=>b(r,n,t,e),String(e))},S=(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:[]}},C=(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 S(`Invalid array index`,n,i,r);n=n[e],r=a}else if(o(n)){if(!Object.prototype.hasOwnProperty.call(n,i))return S(`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}},w=(e,t)=>{let n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){let n=v(e,t);return n===void 0?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:n}}return C((e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[]).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)}`}}},T=(e,t)=>{let n=w(e,t);if(n.error)throw Error(`${n.error}${n.availableKeys?.length?`. Available keys: ${n.availableKeys.join(`, `)}`:``}`);return n.value},E=(e,t)=>{try{return v(e,t)}catch{throw Error(`Invalid JSON path: "${e}"`)}},D=(e,t,n)=>{let r=e?.trim();if(r==null||r===``)throw Error(`Empty expression`);let i=o(t)?t:{},a=p(),s=_(r),c=h(s??r),l=x(c,i,a);if(l)return l;if(!s&&r.startsWith(`$`))return n?.incrementalJsonPath?T(r,i):E(r,i);if(!l&&!s)return e;let u=g(a,c);if(!u||c===`.`)throw Error(`Invalid expression: "${r}"`);try{return u.evalSync(i)}catch{return}},O=(e,t)=>{try{return D(e,t)}catch{return null}},k=(e,t)=>{let n=e=>Array.isArray(e)?e.map(n):typeof e==`object`&&e?k(e,t):typeof e==`string`?O(e,t):e;return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,n(t)]))},A=e=>{try{return D(e),!0}catch{return!1}};export{D as evaluate,A as isValidExpression,O as safeEvaluate,k as safeEvaluateRecord};
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=(e,t=`first`)=>a(e)||typeof e!=`string`?``:(t===`each`?`each`:`first`)==`each`?e.split(/(\s+)/).map(e=>e.trim()?e.charAt(0).toUpperCase()+e.slice(1):e).join(``):e.charAt(0).toUpperCase()+e.slice(1),d=(e,t,n=` `)=>{if(e==null)return``;let r=String(e);if(a(t)||typeof t!=`number`||t<0)return r;let i=typeof n==`string`?n:` `;return r.padStart(t,i)},f=(e,t,n=1)=>{if(a(e)||typeof e!=`string`||a(t)||typeof t!=`string`||t.length===0)return null;let r=typeof n==`number`?n:1;if(r<0)return null;try{let n=new RegExp(t),i=e.match(n);if(!i)return null;let a=i[r];return a===void 0?null:a}catch{return null}},p=(e,t,n=`...`)=>{if(a(e)||typeof e!=`string`)return``;if(a(t)||typeof t!=`number`||t<0)return e;let r=typeof n==`string`?n:`...`;if(e.length<=t)return e;let i=Math.max(0,t-r.length);return i<=0?e.slice(0,t):e.slice(0,i)+r},m=(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.addFunction(`deltaFromNowMs`,(e,t)=>{let n=Date.now(),r;if(typeof e==`number`)r=t===`seconds`?e*1e3:e;else if(typeof e==`string`)r=t===`timestamp`?parseInt(e,10):t===`seconds`?parseFloat(e)*1e3:new Date(e).getTime();else throw Error(`Unsupported date input type`);return r-n}),l.addBinaryOp(`??`,0,(e,t)=>a(e)?t:a(t)?e:e??t),l.addFunction(`capitalize`,u),l.addFunction(`regexMatch`,f),l.addFunction(`padStart`,d),l.addFunction(`truncate`,p),l},h=/\${([^}]+)}/g,g=e=>e.replace(/\$\./g,``).trim(),_=(e,t)=>{try{return e.compile(t)}catch{return null}},v=e=>{if(e.startsWith(`{{`)&&e.endsWith(`}}`))return e.slice(2,-2)},y=(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}`)}},b=(e,t,n)=>e.replace(t,String(n)),x=(e,t,n,r)=>{let i=e.path.trim();if(!i)return r;try{let a=_(t,i);if(a){let t=a.evalSync(n);if(t!==void 0)return b(r,e.toReplace,t)}let o=T(`$.${i}`,n).value;return s(o)?b(r,e.toReplace,o):r}catch{return r}},S=(e,t,n)=>{let r=e.match(h);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)=>x(r,n,t,e),String(e))},C=(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:[]}},w=(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 C(`Invalid array index`,n,i,r);n=n[e],r=a}else if(o(n)){if(!Object.prototype.hasOwnProperty.call(n,i))return C(`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}},T=(e,t)=>{let n=/(\.\.)|(\[\*\])|(\[\?\()|(\[\d+:\d+\])/;try{if(n.test(e)){let n=y(e,t);return n===void 0?{value:void 0,error:`Invalid or empty JSONPath: '${e}'`}:{value:n}}return w((e.match(/\$|(?:\['([^']+)'\])|(?:\["([^"]+)"\])|\[\d+\]|[^[\].]+/g)||[]).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)}`}}},E=(e,t)=>{let n=T(e,t);if(n.error)throw Error(`${n.error}${n.availableKeys?.length?`. Available keys: ${n.availableKeys.join(`, `)}`:``}`);return n.value},D=(e,t)=>{try{return y(e,t)}catch{throw Error(`Invalid JSON path: "${e}"`)}},O=(e,t,n)=>{let r=e?.trim();if(r==null||r===``)throw Error(`Empty expression`);let i=o(t)?t:{},a=m(),s=v(r),c=g(s??r),l=S(c,i,a);if(l)return l;if(!s&&r.startsWith(`$`))return n?.incrementalJsonPath?E(r,i):D(r,i);if(!l&&!s)return e;let u=_(a,c);if(!u||c===`.`)throw Error(`Invalid expression: "${r}"`);try{return u.evalSync(i)}catch{return}},k=(e,t)=>{try{return O(e,t)}catch{return null}},A=(e,t)=>{let n=e=>Array.isArray(e)?e.map(n):typeof e==`object`&&e?A(e,t):typeof e==`string`?k(e,t):e;return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,n(t)]))},j=e=>{try{return O(e),!0}catch{return!1}};export{O as evaluate,j as isValidExpression,k as safeEvaluate,A as safeEvaluateRecord};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackone/expressions",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",