@peter.naydenov/morph 3.1.2 → 3.1.3

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/Changelog.md CHANGED
@@ -1,6 +1,12 @@
1
1
  ## Release History
2
2
 
3
3
 
4
+
5
+ ### 3.1.3 (2025-08-19)
6
+ - [x] Some minor fixes and cleanups;
7
+
8
+
9
+
4
10
  ### 3.1.2 (2025-08-19)
5
11
  - [x] Fix: Argument 'full' contains wrong data;
6
12
 
package/dist/morph.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("@peter.naydenov/walk"),t=require("@peter.naydenov/stack");function r(e){return function t(r){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:s,TG_SIZE_S:l}=e;let i,c,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(i=r.indexOf(a),0<i&&f.push(r.slice(0,i)),-1==i)return f.push(r),f;{if(u=r.indexOf(a,i+s),c=r.indexOf(o),-1==c)return n("missingClosing");if(c<i)return n("closedBeforeOpened");if(c+=l,-1!=u&&u<c)return n("newBeforeClosed");f.push(r.slice(i,c));let e=t(r.slice(c));return f.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...o):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function l(n,l=!1,i={}){let{hasError:c,placeholders:u,chop:f,helpers:d,handshake:p,snippets:h}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:l}=a,i=[],c={},u="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let f=null;const d=r(a)(u);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(s)){const a=r.exec(e);if(!a)return;let o={index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null,name:a[3]?a[3].trim():null};i.push(o),c[i.length-1]=o,o.name&&(c[o.name]=o)}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(f=`Error: Missing helper: ${e}`,!1))))})),{hasError:f,placeholders:i,chop:d,helpers:n,handshake:o,snippets:c}}(n);if(c){function m(){return c}return l?[!1,m]:m}{const b=structuredClone(u);function y(r="render",n={},a={},...l){const c=structuredClone(f);let m=!1;if(!["render","debug","snippets"].includes(r)&&!r.startsWith("snippets"))return`Error: Wrong command "${r}". Available commands: render, debug, snippets.`;if(r.startsWith("snippets")&&r.includes(":")){m=!0;let e=r.split(":").slice(1)[0].trim().split(",").map((e=>e.trim()));u=e.map((e=>h[e]))}else"snippets"===r?m=!0:u=structuredClone(b);if("string"==typeof n)switch(n){case"raw":return c.join("");case"demo":if(!p)return"Error: No handshake data.";n=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>c[e.index])).join(", ");default:return`Error: Wrong instruction "${n}". Available instructions: raw, demo, handshake, placeholders.`}const y=[],g={};let v=o(n),E={...i,...a};return n=e({data:n}),"null"===v?c.join(""):("array"!==v&&(n=[n]),"null"===v?c.join(""):(n.forEach((r=>{u.forEach((a=>{const{index:l,data:i,action:u}=a,f=!u&&i,p=structuredClone(g),h={dependencies:E,memory:p};let m=r;if(i&&i.includes("/")?m.hasOwnProperty(i)?m=m[i]:i.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===i||null===i||"@root"===i?m=r:i&&(m=m[i]),f){switch(o(m)){case"function":return void(c[l]=m());case"primitive":return void(c[l]=m);case"array":return void("primitive"===o(m[0])&&(c[l]=m[0]));case"object":return void(m.text&&(c[l]=m.text))}}else{let{dataDeepLevel:a,nestedData:i}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,u),f=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(u,a),a);for(let t of f){let{type:a,name:l,level:c}=t;(i[c]||[]).forEach(((t,u)=>{let f=o(t);switch(a){case"route":switch(f){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:e});null!=a&&("object"===n?t[r].text=s(e,a,d,E):t[r]=s(e,a,d,E))}));break;case"object":t.text=s(t,l,d,E)}break;case"save":g[l]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(f){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?d[l]({data:e(),...h,full:e}):d[l]({data:e,...h,full:e})));break;case"object":i[c]=[d[l]({data:t,...h,full:n})];break;case"function":i[c]=[d[l]({data:t(),...h,full:n})];break;case"primitive":i[c]=d[l]({data:t,...h,full:n})}break;case"render":const p="function"==typeof d[l];switch(f){case"array":p?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:e});null==a&&(t[r]=null),"object"===n?e.text=a:t[r]=a})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=s(e,l,d,E);null==a?t[r]=null:"object"===n?e.text=a:t[r]=a}));break;case"function":i[c]=d[l]({data:t(),...h,full:n});break;case"primitive":i[c]=p?d[l]({data:t,...h,full:n}):s(t,l,d,E);break;case"object":p?i[c][u].text=d[l]({data:t,...h,full:n}):t.text=s(t,l,d,E)}break;case"extendedRender":"function"==typeof d[l]&&i[0].forEach(((e,t)=>i[0][t]=d[l]({data:e,...h,full:e})));break;case"mix":if(""===l)switch(f){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>i[c][e]=t.text)):i[c]=t.text;for(let b=c-1;b>=0;b--)i[b]=e({data:i[b],objectCallback:m});function m({value:e,breadcrumbs:t}){return i[c][t]?i[c][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let y=d[l]({data:t,...h,full:n}),v=o(y);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,v){case"primitive":t[0]=y;break;case"array":t.push(...y)}}}}))}if(i instanceof Array&&1===i.length&&i[0]instanceof Array&&(i=i[0]),null==i[0])return;let p=o(i[0]),b=i[0];switch(p){case"primitive":if(null==b)return;c[l]=b;break;case"object":if(null==b.text)return;c[l]=b.text;break;case"array":const e=o(b[0]);c[l]="object"===e?b.map((e=>e.text)).join(""):b.join("")}}})),m?y.push(u.map((e=>c[e.index])).join("<~>")):y.push(c.join(""))})),"array"===v?y:l?l.reduce(((e,t)=>"function"!=typeof t?e:t(e,E)),y.join("")):y.join("")))}return l?[!0,y]:y}}const i={default:{}};const c={build:l,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,s=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=l(t,!0,...r);s=e[0],o=e[1]}s?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}};module.exports=c;
1
+ "use strict";var e=require("@peter.naydenov/walk"),t=require("@peter.naydenov/stack");function r(e){return function t(r){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:s,TG_SIZE_S:l}=e;let i,c,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(i=r.indexOf(a),0<i&&f.push(r.slice(0,i)),-1==i)return f.push(r),f;{if(u=r.indexOf(a,i+s),c=r.indexOf(o),-1==c)return n("missingClosing");if(c<i)return n("closedBeforeOpened");if(c+=l,-1!=u&&u<c)return n("newBeforeClosed");f.push(r.slice(i,c));let e=t(r.slice(c));return f.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,o,s,...l){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const i="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),i?n[t]({theData:e,dependencies:s,full:o},...l):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function l(n,l=!1,i={}){let{hasError:c,placeholders:u,chop:f,helpers:d,handshake:p,snippets:h}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:l}=a,i=[],c={},u="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let f=null;const d=r(a)(u);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(s)){const a=r.exec(e);if(!a)return;let o={index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null,name:a[3]?a[3].trim():null};i.push(o),c[i.length-1]=o,o.name&&(c[o.name]=o)}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(f=`Error: Missing helper: ${e}`,!1))))})),{hasError:f,placeholders:i,chop:d,helpers:n,handshake:o,snippets:c}}(n);if(c){function m(){return c}return l?[!1,m]:m}{const b=structuredClone(u);function y(r="render",n={},a={},...l){const c=structuredClone(f);let m=!1;if(!["render","debug","snippets"].includes(r)&&!r.startsWith("snippets"))return`Error: Wrong command "${r}". Available commands: render, debug, snippets.`;if(r.startsWith("snippets")&&r.includes(":")){m=!0;let e=r.split(":").slice(1)[0].trim().split(",").map((e=>e.trim()));u=e.map((e=>h[e]))}else"snippets"===r?m=!0:u=structuredClone(b);if("string"==typeof n)switch(n){case"raw":return c.join("");case"demo":if(!p)return"Error: No handshake data.";n=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>c[e.index])).join(", ");default:return`Error: Wrong instruction "${n}". Available instructions: raw, demo, handshake, placeholders.`}const y=[],g={};let v=o(n),E={...i,...a};n=e({data:n});let k=e({data:n});return"null"===v?c.join(""):("array"!==v&&(n=[n]),"null"===v?c.join(""):(n.forEach((r=>{u.forEach((a=>{const{index:l,data:i,action:u}=a,f=!u&&i,p=structuredClone(g),h={dependencies:E,memory:p};let m=r;if(i&&i.includes("/")?m.hasOwnProperty(i)?m=m[i]:i.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===i||null===i||"@root"===i?m=r:i&&(m=m[i]),f){switch(o(m)){case"function":return void(c[l]=m());case"primitive":return void(c[l]=m);case"array":return void("primitive"===o(m[0])&&(c[l]=m[0]));case"object":return void(m.text&&(c[l]=m.text))}}else{let{dataDeepLevel:a,nestedData:i}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,u),f=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(u,a),a);for(let t of f){let{type:a,name:l,level:c}=t;(i[c]||[]).forEach(((t,u)=>{let f=o(t);switch(a){case"route":switch(f){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:e});null!=a&&("object"===n?t[r].text=s(e,a,d,k,E):t[r]=s(e,a,d,k,E))}));break;case"object":t.text=s(t,l,d,k,E)}break;case"save":g[l]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(f){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?d[l]({data:e(),...h,full:e}):d[l]({data:e,...h,full:e})));break;case"object":i[c]=[d[l]({data:t,...h,full:n})];break;case"function":i[c]=[d[l]({data:t(),...h,full:n})];break;case"primitive":i[c]=d[l]({data:t,...h,full:n})}break;case"render":const p="function"==typeof d[l];switch(f){case"array":p?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:k});null==a&&(t[r]=null),"object"===n?e.text=a:t[r]=a})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=s(e,l,d,k,E);null==a?t[r]=null:"object"===n?e.text=a:t[r]=a}));break;case"function":i[c]=d[l]({data:t(),...h,full:n});break;case"primitive":i[c]=p?d[l]({data:t,...h,full:n}):s(t,l,d,k,E);break;case"object":p?i[c][u].text=d[l]({data:t,...h,full:n}):t.text=s(t,l,d,k,E)}break;case"extendedRender":"function"==typeof d[l]&&i[0].forEach(((e,t)=>i[0][t]=d[l]({data:e,...h,full:e})));break;case"mix":if(""===l)switch(f){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>i[c][e]=t.text)):i[c]=t.text;for(let b=c-1;b>=0;b--)i[b]=e({data:i[b],objectCallback:m});function m({value:e,breadcrumbs:t}){return i[c][t]?i[c][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let y=d[l]({data:t,...h,full:n}),v=o(y);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,v){case"primitive":t[0]=y;break;case"array":t.push(...y)}}}}))}if(i instanceof Array&&1===i.length&&i[0]instanceof Array&&(i=i[0]),null==i[0])return;let p=o(i[0]),b=i[0];switch(p){case"primitive":if(null==b)return;c[l]=b;break;case"object":if(null==b.text)return;c[l]=b.text;break;case"array":const e=o(b[0]);c[l]="object"===e?b.map((e=>e.text)).join(""):b.join("")}}})),m?y.push(u.map((e=>c[e.index])).join("<~>")):y.push(c.join(""))})),"array"===v?y:l?l.reduce(((e,t)=>"function"!=typeof t?e:t(e,E)),y.join("")):y.join("")))}return l?[!0,y]:y}}const i={default:{}};const c={build:l,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,s=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=l(t,!0,...r);s=e[0],o=e[1]}s?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}};module.exports=c;
@@ -1 +1 @@
1
- import e from"@peter.naydenov/walk";import t from"@peter.naydenov/stack";function r(e){return function t(r){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:s,TG_SIZE_S:l}=e;let i,c,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(i=r.indexOf(a),0<i&&f.push(r.slice(0,i)),-1==i)return f.push(r),f;{if(u=r.indexOf(a,i+s),c=r.indexOf(o),-1==c)return n("missingClosing");if(c<i)return n("closedBeforeOpened");if(c+=l,-1!=u&&u<c)return n("newBeforeClosed");f.push(r.slice(i,c));let e=t(r.slice(c));return f.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...o):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function l(n,l=!1,i={}){let{hasError:c,placeholders:u,chop:f,helpers:d,handshake:p,snippets:h}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:l}=a,i=[],c={},u="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let f=null;const d=r(a)(u);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(s)){const a=r.exec(e);if(!a)return;let o={index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null,name:a[3]?a[3].trim():null};i.push(o),c[i.length-1]=o,o.name&&(c[o.name]=o)}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(f=`Error: Missing helper: ${e}`,!1))))})),{hasError:f,placeholders:i,chop:d,helpers:n,handshake:o,snippets:c}}(n);if(c){function m(){return c}return l?[!1,m]:m}{const b=structuredClone(u);function y(r="render",n={},a={},...l){const c=structuredClone(f);let m=!1;if(!["render","debug","snippets"].includes(r)&&!r.startsWith("snippets"))return`Error: Wrong command "${r}". Available commands: render, debug, snippets.`;if(r.startsWith("snippets")&&r.includes(":")){m=!0;let e=r.split(":").slice(1)[0].trim().split(",").map((e=>e.trim()));u=e.map((e=>h[e]))}else"snippets"===r?m=!0:u=structuredClone(b);if("string"==typeof n)switch(n){case"raw":return c.join("");case"demo":if(!p)return"Error: No handshake data.";n=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>c[e.index])).join(", ");default:return`Error: Wrong instruction "${n}". Available instructions: raw, demo, handshake, placeholders.`}const y=[],g={};let v=o(n),E={...i,...a};return n=e({data:n}),"null"===v?c.join(""):("array"!==v&&(n=[n]),"null"===v?c.join(""):(n.forEach((r=>{u.forEach((a=>{const{index:l,data:i,action:u}=a,f=!u&&i,p=structuredClone(g),h={dependencies:E,memory:p};let m=r;if(i&&i.includes("/")?m.hasOwnProperty(i)?m=m[i]:i.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===i||null===i||"@root"===i?m=r:i&&(m=m[i]),f){switch(o(m)){case"function":return void(c[l]=m());case"primitive":return void(c[l]=m);case"array":return void("primitive"===o(m[0])&&(c[l]=m[0]));case"object":return void(m.text&&(c[l]=m.text))}}else{let{dataDeepLevel:a,nestedData:i}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,u),f=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(u,a),a);for(let t of f){let{type:a,name:l,level:c}=t;(i[c]||[]).forEach(((t,u)=>{let f=o(t);switch(a){case"route":switch(f){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:e});null!=a&&("object"===n?t[r].text=s(e,a,d,E):t[r]=s(e,a,d,E))}));break;case"object":t.text=s(t,l,d,E)}break;case"save":g[l]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(f){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?d[l]({data:e(),...h,full:e}):d[l]({data:e,...h,full:e})));break;case"object":i[c]=[d[l]({data:t,...h,full:n})];break;case"function":i[c]=[d[l]({data:t(),...h,full:n})];break;case"primitive":i[c]=d[l]({data:t,...h,full:n})}break;case"render":const p="function"==typeof d[l];switch(f){case"array":p?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:e});null==a&&(t[r]=null),"object"===n?e.text=a:t[r]=a})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=s(e,l,d,E);null==a?t[r]=null:"object"===n?e.text=a:t[r]=a}));break;case"function":i[c]=d[l]({data:t(),...h,full:n});break;case"primitive":i[c]=p?d[l]({data:t,...h,full:n}):s(t,l,d,E);break;case"object":p?i[c][u].text=d[l]({data:t,...h,full:n}):t.text=s(t,l,d,E)}break;case"extendedRender":"function"==typeof d[l]&&i[0].forEach(((e,t)=>i[0][t]=d[l]({data:e,...h,full:e})));break;case"mix":if(""===l)switch(f){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>i[c][e]=t.text)):i[c]=t.text;for(let b=c-1;b>=0;b--)i[b]=e({data:i[b],objectCallback:m});function m({value:e,breadcrumbs:t}){return i[c][t]?i[c][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let y=d[l]({data:t,...h,full:n}),v=o(y);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,v){case"primitive":t[0]=y;break;case"array":t.push(...y)}}}}))}if(i instanceof Array&&1===i.length&&i[0]instanceof Array&&(i=i[0]),null==i[0])return;let p=o(i[0]),b=i[0];switch(p){case"primitive":if(null==b)return;c[l]=b;break;case"object":if(null==b.text)return;c[l]=b.text;break;case"array":const e=o(b[0]);c[l]="object"===e?b.map((e=>e.text)).join(""):b.join("")}}})),m?y.push(u.map((e=>c[e.index])).join("<~>")):y.push(c.join(""))})),"array"===v?y:l?l.reduce(((e,t)=>"function"!=typeof t?e:t(e,E)),y.join("")):y.join("")))}return l?[!0,y]:y}}const i={default:{}};const c={build:l,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,s=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=l(t,!0,...r);s=e[0],o=e[1]}s?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}};export{c as default};
1
+ import e from"@peter.naydenov/walk";import t from"@peter.naydenov/stack";function r(e){return function t(r){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:s,TG_SIZE_S:l}=e;let i,c,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(i=r.indexOf(a),0<i&&f.push(r.slice(0,i)),-1==i)return f.push(r),f;{if(u=r.indexOf(a,i+s),c=r.indexOf(o),-1==c)return n("missingClosing");if(c<i)return n("closedBeforeOpened");if(c+=l,-1!=u&&u<c)return n("newBeforeClosed");f.push(r.slice(i,c));let e=t(r.slice(c));return f.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,o,s,...l){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const i="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),i?n[t]({theData:e,dependencies:s,full:o},...l):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function l(n,l=!1,i={}){let{hasError:c,placeholders:u,chop:f,helpers:d,handshake:p,snippets:h}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:l}=a,i=[],c={},u="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let f=null;const d=r(a)(u);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(s)){const a=r.exec(e);if(!a)return;let o={index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null,name:a[3]?a[3].trim():null};i.push(o),c[i.length-1]=o,o.name&&(c[o.name]=o)}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(f=`Error: Missing helper: ${e}`,!1))))})),{hasError:f,placeholders:i,chop:d,helpers:n,handshake:o,snippets:c}}(n);if(c){function m(){return c}return l?[!1,m]:m}{const b=structuredClone(u);function y(r="render",n={},a={},...l){const c=structuredClone(f);let m=!1;if(!["render","debug","snippets"].includes(r)&&!r.startsWith("snippets"))return`Error: Wrong command "${r}". Available commands: render, debug, snippets.`;if(r.startsWith("snippets")&&r.includes(":")){m=!0;let e=r.split(":").slice(1)[0].trim().split(",").map((e=>e.trim()));u=e.map((e=>h[e]))}else"snippets"===r?m=!0:u=structuredClone(b);if("string"==typeof n)switch(n){case"raw":return c.join("");case"demo":if(!p)return"Error: No handshake data.";n=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>c[e.index])).join(", ");default:return`Error: Wrong instruction "${n}". Available instructions: raw, demo, handshake, placeholders.`}const y=[],g={};let v=o(n),E={...i,...a};n=e({data:n});let k=e({data:n});return"null"===v?c.join(""):("array"!==v&&(n=[n]),"null"===v?c.join(""):(n.forEach((r=>{u.forEach((a=>{const{index:l,data:i,action:u}=a,f=!u&&i,p=structuredClone(g),h={dependencies:E,memory:p};let m=r;if(i&&i.includes("/")?m.hasOwnProperty(i)?m=m[i]:i.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===i||null===i||"@root"===i?m=r:i&&(m=m[i]),f){switch(o(m)){case"function":return void(c[l]=m());case"primitive":return void(c[l]=m);case"array":return void("primitive"===o(m[0])&&(c[l]=m[0]));case"object":return void(m.text&&(c[l]=m.text))}}else{let{dataDeepLevel:a,nestedData:i}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,u),f=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(u,a),a);for(let t of f){let{type:a,name:l,level:c}=t;(i[c]||[]).forEach(((t,u)=>{let f=o(t);switch(a){case"route":switch(f){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:e});null!=a&&("object"===n?t[r].text=s(e,a,d,k,E):t[r]=s(e,a,d,k,E))}));break;case"object":t.text=s(t,l,d,k,E)}break;case"save":g[l]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(f){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?d[l]({data:e(),...h,full:e}):d[l]({data:e,...h,full:e})));break;case"object":i[c]=[d[l]({data:t,...h,full:n})];break;case"function":i[c]=[d[l]({data:t(),...h,full:n})];break;case"primitive":i[c]=d[l]({data:t,...h,full:n})}break;case"render":const p="function"==typeof d[l];switch(f){case"array":p?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=d[l]({data:e,...h,full:k});null==a&&(t[r]=null),"object"===n?e.text=a:t[r]=a})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),a=s(e,l,d,k,E);null==a?t[r]=null:"object"===n?e.text=a:t[r]=a}));break;case"function":i[c]=d[l]({data:t(),...h,full:n});break;case"primitive":i[c]=p?d[l]({data:t,...h,full:n}):s(t,l,d,k,E);break;case"object":p?i[c][u].text=d[l]({data:t,...h,full:n}):t.text=s(t,l,d,k,E)}break;case"extendedRender":"function"==typeof d[l]&&i[0].forEach(((e,t)=>i[0][t]=d[l]({data:e,...h,full:e})));break;case"mix":if(""===l)switch(f){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>i[c][e]=t.text)):i[c]=t.text;for(let b=c-1;b>=0;b--)i[b]=e({data:i[b],objectCallback:m});function m({value:e,breadcrumbs:t}){return i[c][t]?i[c][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let y=d[l]({data:t,...h,full:n}),v=o(y);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,v){case"primitive":t[0]=y;break;case"array":t.push(...y)}}}}))}if(i instanceof Array&&1===i.length&&i[0]instanceof Array&&(i=i[0]),null==i[0])return;let p=o(i[0]),b=i[0];switch(p){case"primitive":if(null==b)return;c[l]=b;break;case"object":if(null==b.text)return;c[l]=b.text;break;case"array":const e=o(b[0]);c[l]="object"===e?b.map((e=>e.text)).join(""):b.join("")}}})),m?y.push(u.map((e=>c[e.index])).join("<~>")):y.push(c.join(""))})),"array"===v?y:l?l.reduce(((e,t)=>"function"!=typeof t?e:t(e,E)),y.join("")):y.join("")))}return l?[!0,y]:y}}const i={default:{}};const c={build:l,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,s=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=l(t,!0,...r);s=e[0],o=e[1]}s?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}};export{c as default};
package/dist/morph.umd.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@peter.naydenov/walk")):"function"==typeof define&&define.amd?define(["@peter.naydenov/walk"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).morph=t(e.walk)}(this,(function(e){"use strict";function t(e){return function t(n){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:l,TG_SIZE_S:s}=e;let i,c,u,f=[];if("string"!=typeof n)return r("notAString");if(0==n.length)return[];if(i=n.indexOf(a),0<i&&f.push(n.slice(0,i)),-1==i)return f.push(n),f;{if(u=n.indexOf(a,i+l),c=n.indexOf(o),-1==c)return r("missingClosing");if(c<i)return r("closedBeforeOpened");if(c+=s,-1!=u&&u<c)return r("newBeforeClosed");f.push(n.slice(i,c));let e=t(n.slice(c));return f.concat(e)}}}function r(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}function*n(e,t){let r=function(e={}){let t=[],{type:r,limit:n,onLimit:a}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),o="LIFO"===r.toUpperCase(),l=!1;function s(e=1,r=0){let n=[];return r>0&&Array.from({length:r}).map((()=>t.pop())),1==e?t.pop():(Array.from({length:e}).map((()=>{let e=t.pop();null!=e&&n.push(e)})),n)}function i(e=1,r=0){let n=[],a=t.length-r;return e>1&&Array.from({length:e}).map((()=>{null!=t[a-1]&&n.push(t[a-1]),a--})),1==e?t[t.length-1]:n}function c(){}return c.prototype={pull:s,pullReverse:function(e=1,t=0){let r=s(e,t);return r instanceof Array?r.reverse():r},peek:i,peekReverse:function(e=1,t=0){const r=i(e,t);return r instanceof Array?r.reverse():r},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=o?function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(0,-o+n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=e instanceof Array?t.concat(e):t.concat([e]),l=!!n&&t.length===n,i||void 0}}:function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(o-n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=r?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),l=!!n&&t.length===n,i||void 0}},new c}({type:"LIFO"});for(let n=0;n<=t;n++)r.push(e[n]);for(;r&&!r.isEmpty();){let e=yield r.pull();e&&r.push(e)}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function l(e,r,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const l="function"==typeof n[r];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),l?n[r](e,...o):function(e,r){if(null==r)return null;const n=t(a)(e),o=a;return n.forEach(((e,t)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();r.hasOwnProperty(a)&&null!=r[a]&&(n[t]=r[a])}})),n.join("")}(n[r],e)}function s(r,s=!1,i={}){let{hasError:c,placeholders:u,chop:f,helpers:p,handshake:d,snippets:h}=function(e){const{template:r,helpers:n={},handshake:o}=e,{TG_PRX:l,TG_SFX:s}=a,i=[],c={},u="string"==typeof r?r.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):r;let f=null;const p=t(a)(u);return"string"==typeof p?f=p:p.forEach(((e,t)=>{const r=RegExp(l+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(l)){const a=r.exec(e);if(!a)return;let o={index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null,name:a[3]?a[3].trim():null};i.push(o),c[i.length-1]=o,o.name&&(c[o.name]=o)}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(f=`Error: Missing helper: ${e}`,!1))))})),{hasError:f,placeholders:i,chop:p,helpers:n,handshake:o,snippets:c}}(r);if(c){function m(){return c}return s?[!1,m]:m}{const y=structuredClone(u);function g(t="render",r={},a={},...s){const c=structuredClone(f);let m=!1;if(!["render","debug","snippets"].includes(t)&&!t.startsWith("snippets"))return`Error: Wrong command "${t}". Available commands: render, debug, snippets.`;if(t.startsWith("snippets")&&t.includes(":")){m=!0;let e=t.split(":").slice(1)[0].trim().split(",").map((e=>e.trim()));u=e.map((e=>h[e]))}else"snippets"===t?m=!0:u=structuredClone(y);if("string"==typeof r)switch(r){case"raw":return c.join("");case"demo":if(!d)return"Error: No handshake data.";r=d;break;case"handshake":return d?structuredClone(d):"Error: No handshake data.";case"placeholders":return u.map((e=>c[e.index])).join(", ");default:return`Error: Wrong instruction "${r}". Available instructions: raw, demo, handshake, placeholders.`}const g=[],b={};let v=o(r),E={...i,...a};return r=e({data:r}),"null"===v?c.join(""):("array"!==v&&(r=[r]),"null"===v?c.join(""):(r.forEach((t=>{u.forEach((a=>{const{index:s,data:i,action:u}=a,f=!u&&i,d=structuredClone(b),h={dependencies:E,memory:d};let m=t;if(i&&i.includes("/")?m.hasOwnProperty(i)?m=m[i]:i.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===i||null===i||"@root"===i?m=t:i&&(m=m[i]),f){switch(o(m)){case"function":return void(c[s]=m());case"primitive":return void(c[s]=m);case"array":return void("primitive"===o(m[0])&&(c[s]=m[0]));case"object":return void(m.text&&(c[s]=m.text))}}else{let{dataDeepLevel:a,nestedData:i}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,u),f=n(function(e,t=10){let r={},n=[...e],a=0,o=0,l=0;n.forEach((e=>{"#"===e&&l++})),l<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${l} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(u,a),a);for(let n of f){let{type:a,name:s,level:c}=n;(i[c]||[]).forEach(((n,u)=>{let f=o(n);switch(a){case"route":switch(f){case"array":n.forEach(((e,t)=>{if(null==e)return;const r=o(e),a=p[s]({data:e,...h,full:e});null!=a&&("object"===r?n[t].text=l(e,a,p,E):n[t]=l(e,a,p,E))}));break;case"object":n.text=l(n,s,p,E)}break;case"save":b[s]=structuredClone(n);break;case"overwrite":t=structuredClone(n);break;case"data":switch(f){case"array":n.forEach(((e,t)=>n[t]=e instanceof Function?p[s]({data:e(),...h,full:e}):p[s]({data:e,...h,full:e})));break;case"object":i[c]=[p[s]({data:n,...h,full:r})];break;case"function":i[c]=[p[s]({data:n(),...h,full:r})];break;case"primitive":i[c]=p[s]({data:n,...h,full:r})}break;case"render":const d="function"==typeof p[s];switch(f){case"array":d?n.forEach(((e,t)=>{if(null==e)return;const r=o(e),a=p[s]({data:e,...h,full:e});null==a&&(n[t]=null),"object"===r?e.text=a:n[t]=a})):n.forEach(((e,t)=>{if(null==e)return;const r=o(e),a=l(e,s,p,E);null==a?n[t]=null:"object"===r?e.text=a:n[t]=a}));break;case"function":i[c]=p[s]({data:n(),...h,full:r});break;case"primitive":i[c]=d?p[s]({data:n,...h,full:r}):l(n,s,p,E);break;case"object":d?i[c][u].text=p[s]({data:n,...h,full:r}):n.text=l(n,s,p,E)}break;case"extendedRender":"function"==typeof p[s]&&i[0].forEach(((e,t)=>i[0][t]=p[s]({data:e,...h,full:e})));break;case"mix":if(""===s)switch(f){case"object":Object.keys(n).find((e=>e.includes("/")))?Object.entries(n).forEach((([e,t])=>i[c][e]=t.text)):i[c]=n.text;for(let y=c-1;y>=0;y--)i[y]=e({data:i[y],objectCallback:m});function m({value:e,breadcrumbs:t}){return i[c][t]?i[c][t]:e}break;case"array":n.forEach(((e,t)=>{if(t>0){let r=o(e);if(null==e)return;n[0]+="object"===r?`${e.text}`:`${e}`,n.toSpliced(t,1)}else{let t=o(e);if(n[0]="",null===t)return;n[0]="object"===t?`${e.text}`:`${e}`}})),n.length=1}else{let g=p[s]({data:n,...h,full:r}),v=o(g);switch(n.forEach(((e,t)=>n.splice(t,1))),n.length=0,v){case"primitive":n[0]=g;break;case"array":n.push(...g)}}}}))}if(i instanceof Array&&1===i.length&&i[0]instanceof Array&&(i=i[0]),null==i[0])return;let d=o(i[0]),y=i[0];switch(d){case"primitive":if(null==y)return;c[s]=y;break;case"object":if(null==y.text)return;c[s]=y.text;break;case"array":const e=o(y[0]);c[s]="object"===e?y.map((e=>e.text)).join(""):y.join("")}}})),m?g.push(u.map((e=>c[e.index])).join("<~>")):g.push(c.join(""))})),"array"===v?g:s?s.reduce(((e,t)=>"function"!=typeof t?e:t(e,E)),g.join("")):g.join("")))}return s?[!0,g]:g}}const i={default:{}};return{build:s,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,l=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=s(t,!0,...r);l=e[0],o=e[1]}l?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}}}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@peter.naydenov/walk")):"function"==typeof define&&define.amd?define(["@peter.naydenov/walk"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).morph=t(e.walk)}(this,(function(e){"use strict";function t(e){return function t(n){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:l,TG_SIZE_S:s}=e;let i,c,u,f=[];if("string"!=typeof n)return r("notAString");if(0==n.length)return[];if(i=n.indexOf(a),0<i&&f.push(n.slice(0,i)),-1==i)return f.push(n),f;{if(u=n.indexOf(a,i+l),c=n.indexOf(o),-1==c)return r("missingClosing");if(c<i)return r("closedBeforeOpened");if(c+=s,-1!=u&&u<c)return r("newBeforeClosed");f.push(n.slice(i,c));let e=t(n.slice(c));return f.concat(e)}}}function r(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}function*n(e,t){let r=function(e={}){let t=[],{type:r,limit:n,onLimit:a}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),o="LIFO"===r.toUpperCase(),l=!1;function s(e=1,r=0){let n=[];return r>0&&Array.from({length:r}).map((()=>t.pop())),1==e?t.pop():(Array.from({length:e}).map((()=>{let e=t.pop();null!=e&&n.push(e)})),n)}function i(e=1,r=0){let n=[],a=t.length-r;return e>1&&Array.from({length:e}).map((()=>{null!=t[a-1]&&n.push(t[a-1]),a--})),1==e?t[t.length-1]:n}function c(){}return c.prototype={pull:s,pullReverse:function(e=1,t=0){let r=s(e,t);return r instanceof Array?r.reverse():r},peek:i,peekReverse:function(e=1,t=0){const r=i(e,t);return r instanceof Array?r.reverse():r},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=o?function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(0,-o+n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=e instanceof Array?t.concat(e):t.concat([e]),l=!!n&&t.length===n,i||void 0}}:function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(o-n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=r?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),l=!!n&&t.length===n,i||void 0}},new c}({type:"LIFO"});for(let n=0;n<=t;n++)r.push(e[n]);for(;r&&!r.isEmpty();){let e=yield r.pull();e&&r.push(e)}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function l(e,r,n,o,l,...s){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const i="function"==typeof n[r];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),i?n[r]({theData:e,dependencies:l,full:o},...s):function(e,r){if(null==r)return null;const n=t(a)(e),o=a;return n.forEach(((e,t)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();r.hasOwnProperty(a)&&null!=r[a]&&(n[t]=r[a])}})),n.join("")}(n[r],e)}function s(r,s=!1,i={}){let{hasError:c,placeholders:u,chop:f,helpers:p,handshake:d,snippets:h}=function(e){const{template:r,helpers:n={},handshake:o}=e,{TG_PRX:l,TG_SFX:s}=a,i=[],c={},u="string"==typeof r?r.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):r;let f=null;const p=t(a)(u);return"string"==typeof p?f=p:p.forEach(((e,t)=>{const r=RegExp(l+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(l)){const a=r.exec(e);if(!a)return;let o={index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null,name:a[3]?a[3].trim():null};i.push(o),c[i.length-1]=o,o.name&&(c[o.name]=o)}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(f=`Error: Missing helper: ${e}`,!1))))})),{hasError:f,placeholders:i,chop:p,helpers:n,handshake:o,snippets:c}}(r);if(c){function m(){return c}return s?[!1,m]:m}{const y=structuredClone(u);function g(t="render",r={},a={},...s){const c=structuredClone(f);let m=!1;if(!["render","debug","snippets"].includes(t)&&!t.startsWith("snippets"))return`Error: Wrong command "${t}". Available commands: render, debug, snippets.`;if(t.startsWith("snippets")&&t.includes(":")){m=!0;let e=t.split(":").slice(1)[0].trim().split(",").map((e=>e.trim()));u=e.map((e=>h[e]))}else"snippets"===t?m=!0:u=structuredClone(y);if("string"==typeof r)switch(r){case"raw":return c.join("");case"demo":if(!d)return"Error: No handshake data.";r=d;break;case"handshake":return d?structuredClone(d):"Error: No handshake data.";case"placeholders":return u.map((e=>c[e.index])).join(", ");default:return`Error: Wrong instruction "${r}". Available instructions: raw, demo, handshake, placeholders.`}const g=[],b={};let v=o(r),E={...i,...a};r=e({data:r});let k=e({data:r});return"null"===v?c.join(""):("array"!==v&&(r=[r]),"null"===v?c.join(""):(r.forEach((t=>{u.forEach((a=>{const{index:s,data:i,action:u}=a,f=!u&&i,d=structuredClone(b),h={dependencies:E,memory:d};let m=t;if(i&&i.includes("/")?m.hasOwnProperty(i)?m=m[i]:i.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===i||null===i||"@root"===i?m=t:i&&(m=m[i]),f){switch(o(m)){case"function":return void(c[s]=m());case"primitive":return void(c[s]=m);case"array":return void("primitive"===o(m[0])&&(c[s]=m[0]));case"object":return void(m.text&&(c[s]=m.text))}}else{let{dataDeepLevel:a,nestedData:i}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,u),f=n(function(e,t=10){let r={},n=[...e],a=0,o=0,l=0;n.forEach((e=>{"#"===e&&l++})),l<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${l} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(u,a),a);for(let n of f){let{type:a,name:s,level:c}=n;(i[c]||[]).forEach(((n,u)=>{let f=o(n);switch(a){case"route":switch(f){case"array":n.forEach(((e,t)=>{if(null==e)return;const r=o(e),a=p[s]({data:e,...h,full:e});null!=a&&("object"===r?n[t].text=l(e,a,p,k,E):n[t]=l(e,a,p,k,E))}));break;case"object":n.text=l(n,s,p,k,E)}break;case"save":b[s]=structuredClone(n);break;case"overwrite":t=structuredClone(n);break;case"data":switch(f){case"array":n.forEach(((e,t)=>n[t]=e instanceof Function?p[s]({data:e(),...h,full:e}):p[s]({data:e,...h,full:e})));break;case"object":i[c]=[p[s]({data:n,...h,full:r})];break;case"function":i[c]=[p[s]({data:n(),...h,full:r})];break;case"primitive":i[c]=p[s]({data:n,...h,full:r})}break;case"render":const d="function"==typeof p[s];switch(f){case"array":d?n.forEach(((e,t)=>{if(null==e)return;const r=o(e),a=p[s]({data:e,...h,full:k});null==a&&(n[t]=null),"object"===r?e.text=a:n[t]=a})):n.forEach(((e,t)=>{if(null==e)return;const r=o(e),a=l(e,s,p,k,E);null==a?n[t]=null:"object"===r?e.text=a:n[t]=a}));break;case"function":i[c]=p[s]({data:n(),...h,full:r});break;case"primitive":i[c]=d?p[s]({data:n,...h,full:r}):l(n,s,p,k,E);break;case"object":d?i[c][u].text=p[s]({data:n,...h,full:r}):n.text=l(n,s,p,k,E)}break;case"extendedRender":"function"==typeof p[s]&&i[0].forEach(((e,t)=>i[0][t]=p[s]({data:e,...h,full:e})));break;case"mix":if(""===s)switch(f){case"object":Object.keys(n).find((e=>e.includes("/")))?Object.entries(n).forEach((([e,t])=>i[c][e]=t.text)):i[c]=n.text;for(let y=c-1;y>=0;y--)i[y]=e({data:i[y],objectCallback:m});function m({value:e,breadcrumbs:t}){return i[c][t]?i[c][t]:e}break;case"array":n.forEach(((e,t)=>{if(t>0){let r=o(e);if(null==e)return;n[0]+="object"===r?`${e.text}`:`${e}`,n.toSpliced(t,1)}else{let t=o(e);if(n[0]="",null===t)return;n[0]="object"===t?`${e.text}`:`${e}`}})),n.length=1}else{let g=p[s]({data:n,...h,full:r}),v=o(g);switch(n.forEach(((e,t)=>n.splice(t,1))),n.length=0,v){case"primitive":n[0]=g;break;case"array":n.push(...g)}}}}))}if(i instanceof Array&&1===i.length&&i[0]instanceof Array&&(i=i[0]),null==i[0])return;let d=o(i[0]),y=i[0];switch(d){case"primitive":if(null==y)return;c[s]=y;break;case"object":if(null==y.text)return;c[s]=y.text;break;case"array":const e=o(y[0]);c[s]="object"===e?y.map((e=>e.text)).join(""):y.join("")}}})),m?g.push(u.map((e=>c[e.index])).join("<~>")):g.push(c.join(""))})),"array"===v?g:s?s.reduce(((e,t)=>"function"!=typeof t?e:t(e,E)),g.join("")):g.join("")))}return s?[!0,g]:g}}const i={default:{}};return{build:s,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,l=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=s(t,!0,...r);l=e[0],o=e[1]}l?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}}}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peter.naydenov/morph",
3
3
  "description": "Template engine",
4
- "version": "3.1.2",
4
+ "version": "3.1.3",
5
5
  "license": "MIT",
6
6
  "author": "Peter Naydenov",
7
7
  "main": "./src/main.js",
@@ -112,7 +112,8 @@ function build ( tpl, extra=false, buildDependencies={} ) {
112
112
 
113
113
  let topLevelType = _defineDataType ( d );
114
114
  let deps = { ...buildDependencies, ...dependencies }
115
- d = walk ({data:d}) // Creates copy of data to avoid mutation of the original
115
+ d = walk ({data:d}) // Creates copy of data to avoid mutation of the original
116
+ let original = walk ({data:d}) // Creates copy of data to avoid mutation of the original
116
117
  if ( topLevelType === 'null' ) return cuts.join ( '' )
117
118
  if ( topLevelType !== 'array' ) d = [ d ]
118
119
 
@@ -127,8 +128,7 @@ function build ( tpl, extra=false, buildDependencies={} ) {
127
128
  , mem = structuredClone ( memory )
128
129
  , extendArguments = { dependencies: deps, memory:mem }
129
130
  ;
130
- let info = dElement;
131
-
131
+ let info = dElement;
132
132
 
133
133
  if ( data && data.includes('/') ) {
134
134
  if ( info.hasOwnProperty ( data )) {
@@ -188,12 +188,12 @@ function build ( tpl, extra=false, buildDependencies={} ) {
188
188
  const dType = _defineDataType ( d )
189
189
  const routeName = helpers[name]( {data:d, ...extendArguments, full: d });
190
190
  if ( routeName == null ) return
191
- if ( dType === 'object' ) theData[i]['text'] = render ( d, routeName, helpers, deps )
192
- else theData[i] = render ( d, routeName, helpers, deps )
191
+ if ( dType === 'object' ) theData[i]['text'] = render ( d, routeName, helpers, original, deps )
192
+ else theData[i] = render ( d, routeName, helpers, original, deps )
193
193
  })
194
194
  break
195
195
  case 'object':
196
- theData['text'] = render ( theData, name, helpers, deps )
196
+ theData['text'] = render ( theData, name, helpers, original, deps )
197
197
  break
198
198
  }
199
199
  break
@@ -227,7 +227,7 @@ function build ( tpl, extra=false, buildDependencies={} ) {
227
227
  if ( isRenderFunction ) theData.forEach ( (d,i) => {
228
228
  if ( d == null ) return
229
229
  const dType = _defineDataType ( d );
230
- const text = helpers[name]( {data:d, ...extendArguments, full: d });
230
+ const text = helpers[name]( {data:d, ...extendArguments, full: original });
231
231
 
232
232
  if ( text == null ) theData[i] = null
233
233
  if ( dType === 'object' ) d['text'] = text
@@ -237,9 +237,9 @@ function build ( tpl, extra=false, buildDependencies={} ) {
237
237
  if ( d == null ) return
238
238
  const
239
239
  dType = _defineDataType ( d )
240
- , text = render ( d, name, helpers, deps )
240
+ , text = render ( d, name, helpers, original, deps )
241
241
  ;
242
- if ( text == null ) theData[i] = null
242
+ if ( text == null ) theData[i] = null
243
243
  else if ( dType === 'object' ) d['text'] = text
244
244
  else theData[i] = text
245
245
  })
@@ -249,12 +249,12 @@ function build ( tpl, extra=false, buildDependencies={} ) {
249
249
  break
250
250
  case 'primitive':
251
251
  if ( isRenderFunction ) nestedData[level] = helpers[name]({ data:theData, ...extendArguments, full: d} )
252
- else nestedData[level] = render ( theData, name, helpers, deps )
252
+ else nestedData[level] = render ( theData, name, helpers, original, deps )
253
253
  break
254
254
  case 'object':
255
255
  if ( isRenderFunction ) nestedData[level][iData]['text'] = helpers[name]({ data:theData, ...extendArguments, full: d })
256
256
  else {
257
- theData [ 'text' ] = render ( theData, name, helpers, deps )
257
+ theData [ 'text' ] = render ( theData, name, helpers, original, deps )
258
258
  }
259
259
  break
260
260
  } // switch renderDataType
@@ -13,7 +13,7 @@ import _renderHolder from "./_renderHolder.js"
13
13
  *
14
14
  * @returns {string} - Rendered string.
15
15
  */
16
- function render ( theData, name, helpers, ...args ) {
16
+ function render ( theData, name, helpers, original, dependencies, ...args ) {
17
17
  // *** Executes rendering and return the results
18
18
  if ( theData instanceof Object ) { // Make sure all properties are not objects
19
19
  Object.entries ( theData ).forEach ( ([key, value]) => {
@@ -27,7 +27,8 @@ function render ( theData, name, helpers, ...args ) {
27
27
  } // setRenderData func.
28
28
  const isRenderFunction = typeof helpers[name] === 'function'; // Render could be a function or template.
29
29
  theData = setRenderData ( theData )
30
- if ( isRenderFunction ) return helpers[name]( theData, ...args )
30
+
31
+ if ( isRenderFunction ) return helpers[name]( { theData, dependencies, full:original}, ...args )
31
32
  else return _renderHolder ( helpers[name], theData )
32
33
  } // render func.
33
34