json-variables 12.0.7 → 12.0.8
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/json-variables.esm.js +2 -2
- package/dist/json-variables.umd.js +11 -11
- package/package.json +7 -7
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name json-variables
|
|
3
3
|
* @fileoverview Resolves custom-marked, cross-referenced paths in parsed JSON
|
|
4
|
-
* @version 12.0.
|
|
4
|
+
* @version 12.0.8
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/json-variables/}
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import{traverse as C}from"ast-monkey-traverse";import{isMatch as x}from"matcher";import h from"object-path";import{arrayiffy as F}from"arrayiffy-if-string";import{strFindHeadsTails as I}from"string-find-heads-tails";import{getByKey as _}from"ast-get-values-by-key";import{Ranges as B}from"ranges-push";import{rApply as A}from"ranges-apply";import{remDup as L}from"string-remove-duplicate-heads-tails";import{matchLeftIncl as U,matchRightIncl as M}from"string-match-left-right";var j="12.0.
|
|
10
|
+
import{traverse as C}from"ast-monkey-traverse";import{isMatch as x}from"matcher";import h from"object-path";import{arrayiffy as F}from"arrayiffy-if-string";import{strFindHeadsTails as I}from"string-find-heads-tails";import{getByKey as _}from"ast-get-values-by-key";import{Ranges as B}from"ranges-push";import{rApply as A}from"ranges-apply";import{remDup as L}from"string-remove-duplicate-heads-tails";import{matchLeftIncl as U,matchRightIncl as M}from"string-match-left-right";var j="12.0.8";var he=j,q=Object.prototype.hasOwnProperty,K={heads:"%%_",tails:"_%%",headsNoWrap:"%%-",tailsNoWrap:"-%%",lookForDataContainers:!0,dataContainerIdentifierTails:"_data",wrapHeadsWith:"",wrapTailsWith:"",dontWrapVars:[],preventDoubleWrapping:!0,wrapGlobalFlipSwitch:!0,noSingleMarkers:!1,resolveToBoolIfAnyValuesContainBool:!0,resolveToFalseIfAnyValuesContainBool:!0,throwWhenNonStringInsertedInString:!1,allowUnresolved:!1};function u(e){return typeof e=="string"}function G(e){return typeof e=="number"}function O(e){return typeof e=="boolean"}function N(e){return e===null}function V(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function z(e){return e!=null}function g(e){return u(e)?e.trim():e}function Y(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=0,n=e.length;t<n;t++)if(e[t]===".")return e.slice(0,t)}return e}function Q(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=0,n=e.length;t<n;t++)if(e[t]===".")return e.slice(t+1)}return e}function J(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=e.length;t--;)if(e[t]===".")return e.slice(0,t)}return e}function P(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=e.length;t--;)if(e[t]===".")return e.slice(t+1)}return e}function T(e,t){return typeof e!="string"||!e.trim()?!1:!!(e.includes(t.heads)||e.includes(t.tails)||u(t.headsNoWrap)&&t.headsNoWrap.length&&e.includes(t.headsNoWrap)||u(t.tailsNoWrap)&&t.tailsNoWrap.length&&e.includes(t.tailsNoWrap))}function X(e,t,n){let o,i;return typeof e=="string"&&e.length&&M(e,0,t,{trimBeforeMatching:!0,cb:(r,a,l)=>(o=l,!0)})&&U(e,e.length-1,n,{trimBeforeMatching:!0,cb:(r,a,l)=>(i=(l||0)+1,!0)})?e.slice(o,i):e}function H(e,t,n=!1,o,i,r){if(t.wrapHeadsWith||(t.wrapHeadsWith=""),t.wrapTailsWith||(t.wrapTailsWith=""),u(e)&&!n&&t.wrapGlobalFlipSwitch&&!t.dontWrapVars.some(a=>x(r,a))&&(!t.preventDoubleWrapping||t.preventDoubleWrapping&&u(e)&&!e.includes(t.wrapHeadsWith)&&!e.includes(t.wrapTailsWith)))return`${t.wrapHeadsWith}${e}${t.wrapTailsWith}`;if(n){if(!u(e))return e;let a=L(e,{heads:t.wrapHeadsWith,tails:t.wrapTailsWith});return u(a)?X(a,t.wrapHeadsWith,t.wrapTailsWith):a}return e}function Z(e,t,n,o){let i;if(n.indexOf(".")!==-1){let r=n,a=!0;if(o.lookForDataContainers&&typeof o.dataContainerIdentifierTails=="string"&&o.dataContainerIdentifierTails.length&&!r.endsWith(o.dataContainerIdentifierTails)){let l=h.get(e,r+o.dataContainerIdentifierTails);V(l)&&h.get(l,t)&&(i=h.get(l,t),a=!1)}for(;a&&r.indexOf(".")!==-1;){if(r=J(r),P(r)===t)throw new Error(`json-variables/findValues(): [THROW_ID_20] While trying to resolve: "${t}" at path "${n}", we encountered a closed loop. The parent key "${P(r)}" is called the same as the variable "${t}" we're looking for.`);if(o.lookForDataContainers&&typeof o.dataContainerIdentifierTails=="string"&&o.dataContainerIdentifierTails.length&&!r.endsWith(o.dataContainerIdentifierTails)){let l=h.get(e,r+o.dataContainerIdentifierTails);V(l)&&h.get(l,t)&&(i=h.get(l,t),a=!1)}if(i===void 0){let l=h.get(e,r);V(l)&&h.get(l,t)&&(i=h.get(l,t),a=!1)}}}if(i===void 0){let r=h.get(e,t);r!==void 0&&(i=r)}if(i===void 0)if(t.indexOf(".")===-1){let r=_(e,t);if(r.length){for(let a=0,l=r.length;a<l;a++)if(u(r[a].val)||O(r[a].val)||N(r[a].val)){i=r[a].val;break}else if(G(r[a].val)){i=String(r[a].val);break}else if(Array.isArray(r[a].val)){i=r[a].val.join("");break}}}else{let r=_(e,Y(t));if(r.length)for(let a=0,l=r.length;a<l;a++){let y=h.get(r[a].val,Q(t));y&&u(y)&&(i=y)}}return i}function R(e,t,n,o,i=[]){if(i.includes(n)){let c="";if(i.length>1){let b=` \u2192
|
|
11
11
|
`;c=i.reduce((D,m,S)=>D+(S===0?"":b)+(m===n?"\u{1F4A5} ":" ")+m,` Here's the path we travelled up until we hit the recursion:
|
|
12
12
|
|
|
13
13
|
`),c+=`${b}\u{1F4A5} ${n}`}throw new Error(`json-variables/resolveString(): [THROW_ID_19] While trying to resolve: "${t}" at path "${n}", we encountered a closed loop, the key is referencing itself."${c}`)}let r={},a=Array.from(i);a.push(n);let l=new B;function y(c,b,D){for(let m=0,S=c.length;m<S;m++){let f=c[m],p=t.slice(f.headsEndAt,f.tailsStartAt);if(p.length===0)l.push(f.headsStartAt,f.tailsEndAt);else if(q.call(r,p)&&u(r[p]))l.push(f.headsStartAt,f.tailsEndAt,r[p]);else{let s=Z(e,p.trim(),n,o);if(s===void 0)if(o.allowUnresolved===!0)s="";else if(typeof o.allowUnresolved=="string")s=o.allowUnresolved;else throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_18] We couldn't find the value to resolve the variable ${t.slice(f.headsEndAt,f.tailsStartAt)}. We're at path: "${n}".`);if(!D&&o.throwWhenNonStringInsertedInString&&!u(s))throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_23] While resolving the variable ${t.slice(f.headsEndAt,f.tailsStartAt)} at path ${n}, it resolved into a non-string value, ${JSON.stringify(s,null,4)}. This is happening because options setting "throwWhenNonStringInsertedInString" is active (set to "true").`);if(O(s)){if(o.resolveToBoolIfAnyValuesContainBool)return l.wipe(),o.resolveToFalseIfAnyValuesContainBool?!1:s;s=""}else{if(N(s)&&D)return l.wipe(),s;Array.isArray(s)?s=String(s.join("")):N(s)?s="":s=String(s)}let v=n.includes(".")?`${J(n)}.${p}`:p;if(T(s,o)){let w=H(R(e,s,v,o,a),o,b,a,v,p.trim());typeof w=="string"&&l.push(f.headsStartAt,f.tailsEndAt,w)}else{r[p]=s;let w=H(s,o,b,a,v,p.trim());typeof w=="string"&&l.push(f.headsStartAt,f.tailsEndAt,w)}}}}let d;try{d=I(t,o.heads,o.tails,{source:"",throwWhenSomethingWrongIsDetected:!1})}catch(c){throw new Error(`json-variables/resolveString(): [THROW_ID_17] While trying to resolve string: "${t}" at path ${n}, something wrong with heads and tails was detected! Here's the internal error message:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name json-variables
|
|
3
3
|
* @fileoverview Resolves custom-marked, cross-referenced paths in parsed JSON
|
|
4
|
-
* @version 12.0.
|
|
4
|
+
* @version 12.0.8
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/json-variables/}
|
|
@@ -37,7 +37,7 @@ ${JSON.stringify(t,null,4)} (type ${typeof t})`);else i=$({},Lt);let l=e.filter(
|
|
|
37
37
|
${JSON.stringify(r,null,4)}`);if(C(this.ranges)&&Array.isArray(this.last())&&i===this.last()[1]){if(this.last()[1]=l,this.last()[2],this.last()[2]!==null&&C(r)){let o=this.last()[2]&&this.last()[2].length&&(!((n=this.opts)!=null&&n.mergeType)||this.opts.mergeType===1)?`${this.last()[2]}${r}`:r;this.opts.limitToBeAddedWhitespace&&(o=pe(o,this.opts.limitLinebreaksCount)),z(o)&&!o.length||(this.last()[2]=o)}}else{this.ranges||(this.ranges=[]);let o=r!==void 0&&!(z(r)&&!r.length)?[i,l,r&&this.opts.limitToBeAddedWhitespace?pe(r,this.opts.limitLinebreaksCount):r]:[i,l];this.ranges.push(o)}}else throw V(i)&&i>=0?new TypeError(`ranges-push/Ranges/add(): [THROW_ID_10] "to" value, the second input argument, must be a natural number or zero! Currently it's of a type "${typeof l}" equal to: ${JSON.stringify(l,null,4)}`):new TypeError(`ranges-push/Ranges/add(): [THROW_ID_09] "from" value, the first input argument, must be a natural number or zero! Currently it's of a type "${typeof i}" equal to: ${JSON.stringify(i,null,4)}`)}push(e,t,r){this.add(e,t,r)}current(){return Array.isArray(this.ranges)&&this.ranges.length?(this.ranges=qn(this.ranges,{mergeType:this.opts.mergeType}),this.ranges&&this.opts.limitToBeAddedWhitespace?this.ranges.map(e=>C(e[2])?[e[0],e[1],pe(e[2],this.opts.limitLinebreaksCount)]:e):this.ranges):null}wipe(){this.ranges=[]}replace(e){if(Array.isArray(e)&&e.length)if(Array.isArray(e[0])&&V(e[0][0]))this.ranges=Array.from(e);else throw new Error(`ranges-push/Ranges/replace(): [THROW_ID_11] Single range was given but we expected array of arrays! The first element, ${JSON.stringify(e[0],null,4)} should be an array and its first element should be an integer, a string index.`);else this.ranges=[]}last(){return Array.isArray(this.ranges)&&this.ranges.length?this.ranges[this.ranges.length-1]:null}};var Kn={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function Je(e,t){if(!Array.isArray(e)||!e.length)return e;let r=$($({},Kn),t),i,l;if(r.strictlyTwoElementsInRangeArrays&&!e.every((s,g)=>!Array.isArray(s)||s.length!==2?(i=g,l=s.length,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${i}th range (${JSON.stringify(e[i],null,4)}) has not two but ${l} elements!`);if(!e.every((s,g)=>!Array.isArray(s)||!Number.isInteger(s[0])||s[0]<0||!Number.isInteger(s[1])||s[1]<0?(i=g,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${i}th range (${JSON.stringify(e[i],null,4)}) does not consist of only natural numbers!`);let n=e.length**2,o=0;return Array.from(e).sort((s,g)=>(r.progressFn&&(o+=1,r.progressFn(Math.floor(o*100/n))),s[0]===g[0]?s[1]<g[1]?-1:s[1]>g[1]?1:0:s[0]<g[0]?-1:1))}var qt={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function Ut(e,t){function r(a){return!!a&&typeof a=="object"&&!Array.isArray(a)}if(!Array.isArray(e)||!e.length)return null;let i;if(t)if(r(t)){if(i=$($({},qt),t),i.progressFn&&r(i.progressFn)&&!Object.keys(i.progressFn).length)i.progressFn=null;else if(i.progressFn&&typeof i.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof i.progressFn}", equal to ${JSON.stringify(i.progressFn,null,4)}`);if(![1,2,"1","2"].includes(i.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof i.mergeType}", equal to ${JSON.stringify(i.mergeType,null,4)}`);if(typeof i.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof i.joinRangesThatTouchEdges}", equal to ${JSON.stringify(i.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:
|
|
38
38
|
${JSON.stringify(t,null,4)} (type ${typeof t})`);else i=$({},qt);let l=e.filter(a=>Array.isArray(a)).map(a=>[...a]).filter(a=>a[2]!==void 0||a[0]!==a[1]),n,o,s;i.progressFn?n=Je(l,{progressFn:a=>{s=Math.floor(a/5),s!==o&&(o=s,i.progressFn(s))}}):n=Je(l);let g=n.length-1;for(let a=g;a>0;a--)i.progressFn&&(s=Math.floor((1-a/g)*78)+21,s!==o&&s>o&&(o=s,i.progressFn(s))),(n[a][0]<=n[a-1][0]||!i.joinRangesThatTouchEdges&&n[a][0]<n[a-1][1]||i.joinRangesThatTouchEdges&&n[a][0]<=n[a-1][1])&&(n[a-1][0]=Math.min(n[a][0],n[a-1][0]),n[a-1][1]=Math.max(n[a][1],n[a-1][1]),n[a][2]!==void 0&&(n[a-1][0]>=n[a][0]||n[a-1][1]<=n[a][1])&&n[a-1][2]!==null&&(n[a][2]===null&&n[a-1][2]!==null?n[a-1][2]=null:n[a-1][2]!=null?+i.mergeType==2&&n[a-1][0]===n[a][0]?n[a-1][2]=n[a][2]:n[a-1][2]+=n[a][2]:n[a-1][2]=n[a][2]),n.splice(a,1),a=n.length);return n.length?n:null}var Gn=!0,Fe="Invariant failed";function Kt(e,t){if(!e){if(Gn)throw new Error(Fe);var r=typeof t=="function"?t():t,i=r?"".concat(Fe,": ").concat(r):Fe;throw new Error(i)}}function L(e,t,r){let i=0,l=0;if(arguments.length===0)throw new Error("ranges-apply: [THROW_ID_01] inputs missing!");if(typeof e!="string")throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(t&&!Array.isArray(t))throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(r&&typeof r!="function")throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof r}, equal to: ${JSON.stringify(r,null,4)}`);if(!t||!t.filter(h=>h).length)return e;let n;Array.isArray(t)&&Number.isInteger(t[0])&&Number.isInteger(t[1])?n=[Array.from(t)]:n=Array.from(t);let o=n.length,s=0;n.filter(h=>h).forEach((h,c)=>{if(r&&(i=Math.floor(s/o*10),i!==l&&(l=i,r(i))),!Array.isArray(h))throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${c}th element not an array: ${JSON.stringify(h,null,4)}, which is ${typeof h}`);if(!Number.isInteger(h[0])){if(!Number.isInteger(+h[0])||+h[0]<0)throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${c}th element, array ${JSON.stringify(h,null,0)}. Its first element is not an integer, string index, but ${typeof h[0]}, equal to: ${JSON.stringify(h[0],null,4)}.`);n[c][0]=+n[c][0]}if(!Number.isInteger(h[1])){if(!Number.isInteger(+h[1])||+h[1]<0)throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${c}th element, array ${JSON.stringify(h,null,0)}. Its second element is not an integer, string index, but ${typeof h[1]}, equal to: ${JSON.stringify(h[1],null,4)}.`);n[c][1]=+n[c][1]}s+=1});let g=Ut(n,{progressFn:h=>{r&&(i=10+Math.floor(h/10),i!==l&&(l=i,r(i)))}});Kt(g);let a=g.length;if(a>0){let h=e.slice(g[a-1][1]);e=g.reduce((c,T,y,f)=>{r&&(i=20+Math.floor(y/a*80),i!==l&&(l=i,r(i)));let w=y===0?0:f[y-1][1],d=f[y][0];return`${c}${e.slice(w,d)}${f[y][2]||""}`},""),e+=h}return e}var Qn={classicTrim:!1,cr:!1,lf:!1,tab:!1,space:!0,nbsp:!1};function Y(e,t){if(typeof e!="string")throw new Error(`string-trim-spaces-only: [THROW_ID_01] input must be string! It was given as ${typeof e}, equal to:
|
|
39
39
|
${JSON.stringify(e,null,4)}`);let r=$($({},Qn),t);function i(o){return r.classicTrim&&!o.trim()||!r.classicTrim&&(r.space&&o===" "||r.cr&&o==="\r"||r.lf&&o===`
|
|
40
|
-
`||r.tab&&o===" "||r.nbsp&&o==="\xA0")}let l,n;if(e.length){if(i(e[0]))for(let o=0,s=e.length;o<s;o++){if(!i(e[o])){l=o;break}if(o===e.length-1)return{res:"",ranges:[[0,e.length]]}}if(i(e[e.length-1])){for(let o=e.length;o--;)if(!i(e[o])){n=o+1;break}}return l?n?{res:e.slice(l,n),ranges:[[0,l],[n,e.length]]}:{res:e.slice(l),ranges:[[0,l]]}:n?{res:e.slice(0,n),ranges:[[n,e.length]]}:{res:e,ranges:[]}}return{res:"",ranges:[]}}var zn={heads:["{{"],tails:["}}"]};function Gt(e,t){let r=Object.prototype.hasOwnProperty;if(e===void 0)throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_01] The input is missing!");if(typeof e!="string")return e;if(t&&!le(t))throw new Error(`string-remove-duplicate-heads-tails: [THROW_ID_03] The given options are not a plain object but ${typeof t}!`);let i=$({},t);if(i&&r.call(i,"heads"))if(I(i.heads).every(w=>typeof w=="string"||Array.isArray(w)))typeof i.heads=="string"&&(i.heads=I(i.heads));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_04] The resolvedOpts.heads contains elements which are not string-type!");if(i&&r.call(i,"tails"))if(I(i.tails).every(w=>typeof w=="string"||Array.isArray(w)))typeof i.tails=="string"&&(i.tails=I(i.tails));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_05] The resolvedOpts.tails contains elements which are not string-type!");let l=Y(e).res;if(l.length===0)return e;e=l;let n=$($({},zn),i);n.heads=n.heads.map(w=>w.trim()),n.tails=n.tails.map(w=>w.trim());let o=!1,s=!1,g=new Z({limitToBeAddedWhitespace:!0}),a=new Z({limitToBeAddedWhitespace:!0}),h=!0,c=!0,T="";function y(w,d){let u;return S(w,0,d.heads,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})&&S(w,u,d.tails,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})?w.slice(u):w}for(;e!==y(e,n);)e=Y(y(e,n)).res;function f(w,d){let u;return!k(w,w.length-1,d.tails,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})||!u?w:k(w,u,d.heads,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})?w.slice(0,u+1):w}for(;e!==f(e,n);)e=Y(f(e,n)).res;if(!n.heads.length||!S(e,0,n.heads,{trimBeforeMatching:!0})||!n.tails.length||!k(e,e.length-1,n.tails,{trimBeforeMatching:!0}))return Y(e).res;for(let w=0,d=e.length;w<d;w++)if(e[w].trim()!==""){let u;if(S(e,w,n.heads,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})&&u){c=!0,h&&(h=!0);let p;S(e,u,n.tails,{trimBeforeMatching:!0,cb:(m,b,A)=>(p=A,!0)})&&g.push(w,p),a.current()&&o&&T!=="tails"&&g.push(a.current()),o||a.current()&&(g.push(a.current()),a.wipe()),a.push(w,u),T="heads",w=u-1;continue}if(S(e,w,n.tails,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=Number.isInteger(b)?b:e.length,!0)})&&u){c=!0,h?(T==="heads"&&a.wipe(),h=!1):a.push(w,u),T="tails",w=u-1;continue}h&&(h=!0),c&&!o?(o=!0,c=!1):c&&!s?(s=!0,h=!0,c=!1,T==="heads"&&a.wipe()):c&&s&&a.wipe()}return a.current()&&g.push(a.current()),g.current()?L(e,g.current()).trim():e.trim()}var Qt="12.0.
|
|
40
|
+
`||r.tab&&o===" "||r.nbsp&&o==="\xA0")}let l,n;if(e.length){if(i(e[0]))for(let o=0,s=e.length;o<s;o++){if(!i(e[o])){l=o;break}if(o===e.length-1)return{res:"",ranges:[[0,e.length]]}}if(i(e[e.length-1])){for(let o=e.length;o--;)if(!i(e[o])){n=o+1;break}}return l?n?{res:e.slice(l,n),ranges:[[0,l],[n,e.length]]}:{res:e.slice(l),ranges:[[0,l]]}:n?{res:e.slice(0,n),ranges:[[n,e.length]]}:{res:e,ranges:[]}}return{res:"",ranges:[]}}var zn={heads:["{{"],tails:["}}"]};function Gt(e,t){let r=Object.prototype.hasOwnProperty;if(e===void 0)throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_01] The input is missing!");if(typeof e!="string")return e;if(t&&!le(t))throw new Error(`string-remove-duplicate-heads-tails: [THROW_ID_03] The given options are not a plain object but ${typeof t}!`);let i=$({},t);if(i&&r.call(i,"heads"))if(I(i.heads).every(w=>typeof w=="string"||Array.isArray(w)))typeof i.heads=="string"&&(i.heads=I(i.heads));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_04] The resolvedOpts.heads contains elements which are not string-type!");if(i&&r.call(i,"tails"))if(I(i.tails).every(w=>typeof w=="string"||Array.isArray(w)))typeof i.tails=="string"&&(i.tails=I(i.tails));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_05] The resolvedOpts.tails contains elements which are not string-type!");let l=Y(e).res;if(l.length===0)return e;e=l;let n=$($({},zn),i);n.heads=n.heads.map(w=>w.trim()),n.tails=n.tails.map(w=>w.trim());let o=!1,s=!1,g=new Z({limitToBeAddedWhitespace:!0}),a=new Z({limitToBeAddedWhitespace:!0}),h=!0,c=!0,T="";function y(w,d){let u;return S(w,0,d.heads,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})&&S(w,u,d.tails,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})?w.slice(u):w}for(;e!==y(e,n);)e=Y(y(e,n)).res;function f(w,d){let u;return!k(w,w.length-1,d.tails,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})||!u?w:k(w,u,d.heads,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})?w.slice(0,u+1):w}for(;e!==f(e,n);)e=Y(f(e,n)).res;if(!n.heads.length||!S(e,0,n.heads,{trimBeforeMatching:!0})||!n.tails.length||!k(e,e.length-1,n.tails,{trimBeforeMatching:!0}))return Y(e).res;for(let w=0,d=e.length;w<d;w++)if(e[w].trim()!==""){let u;if(S(e,w,n.heads,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=b,!0)})&&u){c=!0,h&&(h=!0);let p;S(e,u,n.tails,{trimBeforeMatching:!0,cb:(m,b,A)=>(p=A,!0)})&&g.push(w,p),a.current()&&o&&T!=="tails"&&g.push(a.current()),o||a.current()&&(g.push(a.current()),a.wipe()),a.push(w,u),T="heads",w=u-1;continue}if(S(e,w,n.tails,{trimBeforeMatching:!0,cb:(p,m,b)=>(u=Number.isInteger(b)?b:e.length,!0)})&&u){c=!0,h?(T==="heads"&&a.wipe(),h=!1):a.push(w,u),T="tails",w=u-1;continue}h&&(h=!0),c&&!o?(o=!0,c=!1):c&&!s?(s=!0,h=!0,c=!1,T==="heads"&&a.wipe()):c&&s&&a.wipe()}return a.current()&&g.push(a.current()),g.current()?L(e,g.current()).trim():e.trim()}var Qt="12.0.8";var Yn=Qt,Xn=Object.prototype.hasOwnProperty,Yt={heads:"%%_",tails:"_%%",headsNoWrap:"%%-",tailsNoWrap:"-%%",lookForDataContainers:!0,dataContainerIdentifierTails:"_data",wrapHeadsWith:"",wrapTailsWith:"",dontWrapVars:[],preventDoubleWrapping:!0,wrapGlobalFlipSwitch:!0,noSingleMarkers:!1,resolveToBoolIfAnyValuesContainBool:!0,resolveToFalseIfAnyValuesContainBool:!0,throwWhenNonStringInsertedInString:!1,allowUnresolved:!1};function _(e){return typeof e=="string"}function ei(e){return typeof e=="number"}function ke(e){return typeof e=="boolean"}function de(e){return e===null}function X(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function ti(e){return e!=null}function O(e){return _(e)?e.trim():e}function ri(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=0,r=e.length;t<r;t++)if(e[t]===".")return e.slice(0,t)}return e}function ni(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=0,r=e.length;t<r;t++)if(e[t]===".")return e.slice(t+1)}return e}function Xt(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=e.length;t--;)if(e[t]===".")return e.slice(0,t)}return e}function zt(e){if(typeof e=="string"&&e.length&&e.indexOf(".")!==-1){for(let t=e.length;t--;)if(e[t]===".")return e.slice(t+1)}return e}function Le(e,t){return typeof e!="string"||!e.trim()?!1:!!(e.includes(t.heads)||e.includes(t.tails)||_(t.headsNoWrap)&&t.headsNoWrap.length&&e.includes(t.headsNoWrap)||_(t.tailsNoWrap)&&t.tailsNoWrap.length&&e.includes(t.tailsNoWrap))}function ii(e,t,r){let i,l;return typeof e=="string"&&e.length&&S(e,0,t,{trimBeforeMatching:!0,cb:(n,o,s)=>(i=s,!0)})&&k(e,e.length-1,r,{trimBeforeMatching:!0,cb:(n,o,s)=>(l=(s||0)+1,!0)})?e.slice(i,l):e}function Zt(e,t,r=!1,i,l,n){if(t.wrapHeadsWith||(t.wrapHeadsWith=""),t.wrapTailsWith||(t.wrapTailsWith=""),_(e)&&!r&&t.wrapGlobalFlipSwitch&&!t.dontWrapVars.some(o=>ge(n,o))&&(!t.preventDoubleWrapping||t.preventDoubleWrapping&&_(e)&&!e.includes(t.wrapHeadsWith)&&!e.includes(t.wrapTailsWith)))return`${t.wrapHeadsWith}${e}${t.wrapTailsWith}`;if(r){if(!_(e))return e;let o=Gt(e,{heads:t.wrapHeadsWith,tails:t.wrapTailsWith});return _(o)?ii(o,t.wrapHeadsWith,t.wrapTailsWith):o}return e}function si(e,t,r,i){let l;if(r.indexOf(".")!==-1){let n=r,o=!0;if(i.lookForDataContainers&&typeof i.dataContainerIdentifierTails=="string"&&i.dataContainerIdentifierTails.length&&!n.endsWith(i.dataContainerIdentifierTails)){let s=W.default.get(e,n+i.dataContainerIdentifierTails);X(s)&&W.default.get(s,t)&&(l=W.default.get(s,t),o=!1)}for(;o&&n.indexOf(".")!==-1;){if(n=Xt(n),zt(n)===t)throw new Error(`json-variables/findValues(): [THROW_ID_20] While trying to resolve: "${t}" at path "${r}", we encountered a closed loop. The parent key "${zt(n)}" is called the same as the variable "${t}" we're looking for.`);if(i.lookForDataContainers&&typeof i.dataContainerIdentifierTails=="string"&&i.dataContainerIdentifierTails.length&&!n.endsWith(i.dataContainerIdentifierTails)){let s=W.default.get(e,n+i.dataContainerIdentifierTails);X(s)&&W.default.get(s,t)&&(l=W.default.get(s,t),o=!1)}if(l===void 0){let s=W.default.get(e,n);X(s)&&W.default.get(s,t)&&(l=W.default.get(s,t),o=!1)}}}if(l===void 0){let n=W.default.get(e,t);n!==void 0&&(l=n)}if(l===void 0)if(t.indexOf(".")===-1){let n=Be(e,t);if(n.length){for(let o=0,s=n.length;o<s;o++)if(_(n[o].val)||ke(n[o].val)||de(n[o].val)){l=n[o].val;break}else if(ei(n[o].val)){l=String(n[o].val);break}else if(Array.isArray(n[o].val)){l=n[o].val.join("");break}}}else{let n=Be(e,ri(t));if(n.length)for(let o=0,s=n.length;o<s;o++){let g=W.default.get(n[o].val,ni(t));g&&_(g)&&(l=g)}}return l}function er(e,t,r,i,l=[]){if(l.includes(r)){let y="";if(l.length>1){let f=` \u2192
|
|
41
41
|
`;y=l.reduce((w,d,u)=>w+(u===0?"":f)+(d===r?"\u{1F4A5} ":" ")+d,` Here's the path we travelled up until we hit the recursion:
|
|
42
42
|
|
|
43
43
|
`),y+=`${f}\u{1F4A5} ${r}`}throw new Error(`json-variables/resolveString(): [THROW_ID_19] While trying to resolve: "${t}" at path "${r}", we encountered a closed loop, the key is referencing itself."${y}`)}let n={},o=Array.from(l);o.push(r);let s=new Z;function g(y,f,w){for(let d=0,u=y.length;d<u;d++){let p=y[d],m=t.slice(p.headsEndAt,p.tailsStartAt);if(m.length===0)s.push(p.headsStartAt,p.tailsEndAt);else if(Xn.call(n,m)&&_(n[m]))s.push(p.headsStartAt,p.tailsEndAt,n[m]);else{let b=si(e,m.trim(),r,i);if(b===void 0)if(i.allowUnresolved===!0)b="";else if(typeof i.allowUnresolved=="string")b=i.allowUnresolved;else throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_18] We couldn't find the value to resolve the variable ${t.slice(p.headsEndAt,p.tailsStartAt)}. We're at path: "${r}".`);if(!w&&i.throwWhenNonStringInsertedInString&&!_(b))throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_23] While resolving the variable ${t.slice(p.headsEndAt,p.tailsStartAt)} at path ${r}, it resolved into a non-string value, ${JSON.stringify(b,null,4)}. This is happening because options setting "throwWhenNonStringInsertedInString" is active (set to "true").`);if(ke(b)){if(i.resolveToBoolIfAnyValuesContainBool)return s.wipe(),i.resolveToFalseIfAnyValuesContainBool?!1:b;b=""}else{if(de(b)&&w)return s.wipe(),b;Array.isArray(b)?b=String(b.join("")):de(b)?b="":b=String(b)}let A=r.includes(".")?`${Xt(r)}.${m}`:m;if(Le(b,i)){let E=Zt(er(e,b,A,i,o),i,f,o,A,m.trim());typeof E=="string"&&s.push(p.headsStartAt,p.tailsEndAt,E)}else{n[m]=b;let E=Zt(b,i,f,o,A,m.trim());typeof E=="string"&&s.push(p.headsStartAt,p.tailsEndAt,E)}}}}let a;try{a=Me(t,i.heads,i.tails,{source:"",throwWhenSomethingWrongIsDetected:!1})}catch(y){throw new Error(`json-variables/resolveString(): [THROW_ID_17] While trying to resolve string: "${t}" at path ${r}, something wrong with heads and tails was detected! Here's the internal error message:
|
|
@@ -79,7 +79,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
79
79
|
/**
|
|
80
80
|
* @name string-match-left-right
|
|
81
81
|
* @fileoverview Match substrings on the left or right of a given index, ignoring whitespace
|
|
82
|
-
* @version 9.0.
|
|
82
|
+
* @version 9.0.8
|
|
83
83
|
* @author Roy Revelt, Codsen Ltd
|
|
84
84
|
* @license MIT
|
|
85
85
|
* {@link https://codsen.com/os/string-match-left-right/}
|
|
@@ -87,7 +87,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
87
87
|
/**
|
|
88
88
|
* @name string-find-heads-tails
|
|
89
89
|
* @fileoverview Finds where are arbitrary templating marker heads and tails located
|
|
90
|
-
* @version 6.0.
|
|
90
|
+
* @version 6.0.8
|
|
91
91
|
* @author Roy Revelt, Codsen Ltd
|
|
92
92
|
* @license MIT
|
|
93
93
|
* {@link https://codsen.com/os/string-find-heads-tails/}
|
|
@@ -95,7 +95,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
95
95
|
/**
|
|
96
96
|
* @name ast-get-values-by-key
|
|
97
97
|
* @fileoverview Extract values and paths from AST by keys OR set them by keys
|
|
98
|
-
* @version 5.0.
|
|
98
|
+
* @version 5.0.8
|
|
99
99
|
* @author Roy Revelt, Codsen Ltd
|
|
100
100
|
* @license MIT
|
|
101
101
|
* {@link https://codsen.com/os/ast-get-values-by-key/}
|
|
@@ -111,7 +111,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
111
111
|
/**
|
|
112
112
|
* @name ranges-push
|
|
113
113
|
* @fileoverview Gather string index ranges
|
|
114
|
-
* @version 7.0.
|
|
114
|
+
* @version 7.0.5
|
|
115
115
|
* @author Roy Revelt, Codsen Ltd
|
|
116
116
|
* @license MIT
|
|
117
117
|
* {@link https://codsen.com/os/ranges-push/}
|
|
@@ -119,7 +119,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
119
119
|
/**
|
|
120
120
|
* @name ranges-sort
|
|
121
121
|
* @fileoverview Sort string index ranges
|
|
122
|
-
* @version 6.0.
|
|
122
|
+
* @version 6.0.5
|
|
123
123
|
* @author Roy Revelt, Codsen Ltd
|
|
124
124
|
* @license MIT
|
|
125
125
|
* {@link https://codsen.com/os/ranges-sort/}
|
|
@@ -127,7 +127,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
127
127
|
/**
|
|
128
128
|
* @name ranges-merge
|
|
129
129
|
* @fileoverview Merge and sort string index ranges
|
|
130
|
-
* @version 9.0.
|
|
130
|
+
* @version 9.0.5
|
|
131
131
|
* @author Roy Revelt, Codsen Ltd
|
|
132
132
|
* @license MIT
|
|
133
133
|
* {@link https://codsen.com/os/ranges-merge/}
|
|
@@ -135,7 +135,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
135
135
|
/**
|
|
136
136
|
* @name ranges-apply
|
|
137
137
|
* @fileoverview Take an array of string index ranges, delete/replace the string according to them
|
|
138
|
-
* @version 7.0.
|
|
138
|
+
* @version 7.0.5
|
|
139
139
|
* @author Roy Revelt, Codsen Ltd
|
|
140
140
|
* @license MIT
|
|
141
141
|
* {@link https://codsen.com/os/ranges-apply/}
|
|
@@ -143,7 +143,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
143
143
|
/**
|
|
144
144
|
* @name string-trim-spaces-only
|
|
145
145
|
* @fileoverview Like String.trim() but you can choose granularly what to trim
|
|
146
|
-
* @version 5.0.
|
|
146
|
+
* @version 5.0.5
|
|
147
147
|
* @author Roy Revelt, Codsen Ltd
|
|
148
148
|
* @license MIT
|
|
149
149
|
* {@link https://codsen.com/os/string-trim-spaces-only/}
|
|
@@ -151,7 +151,7 @@ Please check the following key: ${o}`);if(s!==void 0?n=s:n=o,n==="")return n;if(
|
|
|
151
151
|
/**
|
|
152
152
|
* @name string-remove-duplicate-heads-tails
|
|
153
153
|
* @fileoverview Detect and (recursively) remove head and tail wrappings around the input string
|
|
154
|
-
* @version 7.0.
|
|
154
|
+
* @version 7.0.8
|
|
155
155
|
* @author Roy Revelt, Codsen Ltd
|
|
156
156
|
* @license MIT
|
|
157
157
|
* {@link https://codsen.com/os/string-remove-duplicate-heads-tails/}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-variables",
|
|
3
|
-
"version": "12.0.
|
|
3
|
+
"version": "12.0.8",
|
|
4
4
|
"description": "Resolves custom-marked, cross-referenced paths in parsed JSON",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"comb",
|
|
@@ -79,14 +79,14 @@
|
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
81
|
"arrayiffy-if-string": "^5.0.3",
|
|
82
|
-
"ast-get-values-by-key": "^5.0.
|
|
82
|
+
"ast-get-values-by-key": "^5.0.8",
|
|
83
83
|
"ast-monkey-traverse": "^4.0.6",
|
|
84
84
|
"matcher": "^5.0.0",
|
|
85
85
|
"object-path": "^0.11.8",
|
|
86
|
-
"ranges-apply": "^7.0.
|
|
87
|
-
"ranges-push": "^7.0.
|
|
88
|
-
"string-find-heads-tails": "^6.0.
|
|
89
|
-
"string-match-left-right": "^9.0.
|
|
90
|
-
"string-remove-duplicate-heads-tails": "^7.0.
|
|
86
|
+
"ranges-apply": "^7.0.5",
|
|
87
|
+
"ranges-push": "^7.0.5",
|
|
88
|
+
"string-find-heads-tails": "^6.0.8",
|
|
89
|
+
"string-match-left-right": "^9.0.8",
|
|
90
|
+
"string-remove-duplicate-heads-tails": "^7.0.8"
|
|
91
91
|
}
|
|
92
92
|
}
|