@peter.naydenov/morph 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,8 +60,10 @@ const htmlBlock = myTemplate ( { name: 'Peter' } ) // Provide data to the
60
60
  Morph contains also a builtin template storage. Instead of creating variable for each template, we can use the storage.
61
61
 
62
62
  ```js
63
- morph.add ( 'myTemplate', myTemplateDescription ) // add template to the storage. Automatically builds the render function
64
- const htmlBlock = morph.get ( 'myTemplate' )({ name: 'Peter' }) // get template from the storage and render it
63
+ // add template to the storage. Automatically builds the render function
64
+ morph.add ( 'myTemplate', myTemplateDescription )
65
+ // get template from the storage and render it
66
+ const htmlBlock = morph.get ( 'myTemplate' )({ name: 'Peter' })
65
67
  ```
66
68
 
67
69
  Let's see a more complex example before we go into details:
@@ -93,7 +95,22 @@ const htmlBlock = myTemplate ( { person: {
93
95
 
94
96
 
95
97
 
96
- ## Template structure
98
+ ## Methods
99
+
100
+ ```js
101
+ build : 'Build a component from template description'
102
+ , get : 'Get a component from component storage'
103
+ , add : 'Add a component to component storage'
104
+ , list : 'List the names of all components in the component storage'
105
+ , clear : 'Clear up all the components in the storage'
106
+ , remove : 'Remove a template from component storage'
107
+ ```
108
+
109
+
110
+
111
+
112
+
113
+ ## Template Description Structure
97
114
  Templates are objects with tree properties: `template`, `helpers`, and `handshake`:
98
115
  ```js
99
116
  const myTemplateDescription = {
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:c}=e;let i,l,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),l=r.indexOf(o),-1==l)return n("missingClosing");if(l<i)return n("closedBeforeOpened");if(l+=c,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(i,l));let e=t(r.slice(l));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."}}function a(t){const r={};let n=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]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:a}){let o=!1;return n=a.split("/").length-1,isNaN(e)||(o=!0),r[n]||(r[n]=o?[]:{}),"root"===e?r[n]=t:o?r[n].push(t):r[n][a]=t,t}}),{dataDeepLevel:n,nestedData:r}}var o={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function s(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 c(e,t,n){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const a="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),a?n[t](e):function(e,t){if(null==t)return null;const n=r(o)(e),a=o;return n.forEach(((e,r)=>{if(e.includes(a.TG_PRX)){const o=e.replace(a.TG_PRX,"").replace(a.TG_SFX,"").trim();t[o]&&(n[r]=t[o])}})),n.join("")}(n[t],e)}function i(n){const{hasError:i,placeholders:l,chop:u,helpers:f,handshake:d}=function(e){const{template:t,helpers:n,handshake:a}=e,{TG_PRX:s,TG_SFX:c,TG_SIZE_P:i,TG_SIZE_S:l}=o,u=[];let f=null;const d=r(o)(t);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+c,"g");if(e.includes(s)){const a=r.exec(e);u.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),u.forEach((e=>{e.action&&e.action.every((e=>"#"===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:u,chop:d,helpers:n,handshake:a}}(n);if(i)return function(){return i};{let r=structuredClone(u);return function(n={},...o){const i=s(n),u=[];if("null"===i)return r.join("");if("string"==typeof n)switch(n){case"raw":return r.join("");case"demo":if(!d)return"Error: No handshake data.";n=d;break;case"handshake":return d?structuredClone(d):"Error: No handshake data.";case"placeholders":return l.map((e=>r[e.index])).join(", ");default:return`Error: Wrong command "${n}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==i&&(n=[n]),n.forEach((n=>{l.forEach((o=>{const{index:i,data:l,action:u}=o;if(!u&&l){const d=n[l];switch(s(d)){case"function":return void(r[i]=d());case"primitive":return void(r[i]=d);case"array":return void("primitive"===s(d[0])&&(r[i]=d[0]));case"object":return void(d.text&&(r[i]=d.text))}}else{const{dataDeepLevel:p,nestedData:h}=a("@all"===l||null===l||"@root"===l?n:n[l]),b=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=0,a=[...e],o=0;do{r[o]=[],o++}while(o<=t);return a.every((e=>"#"===e?(n++,!(n>t)):e.startsWith("?")?(r[n].push({type:"route",name:e.replace("?",""),level:n}),!0):e.startsWith("+")?(r[n].push({type:"extendedRender",name:e.replace("+",""),level:n}),!0):e.startsWith("[]")?(r[n].push({type:"mix",name:e.replace("[]",""),level:n}),!0):e.startsWith(">")?(r[n].push({type:"data",name:e.replace(">",""),level:n}),!0):(""===e||r[n].push({type:"render",name:e,level:n}),!0))),r}(u,p),p);for(let E of b){let{type:v,name:k,level:j}=E,x=h[j],g=s(x);switch(v){case"route":switch(g){case"array":x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=f[k](e);null!=n&&("object"===r?x[t].text=c(e,n,f):x[t]=c(e,n,f))}));break;case"object":x.text=c(x,routeName,f);break;case"primitive":h[j]=c(x,routeName,f)}break;case"data":switch(g){case"array":x.forEach(((e,t)=>x[t]=e instanceof Function?f[k](e()):f[k](e)));break;case"object":case"primitive":h[j]=f[k](x);break;case"function":h[j]=f[k](x())}break;case"render":const _="function"==typeof f[k];switch(g){case"array":_?x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=f[k](e);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n})):x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=c(e,k,f);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n}));break;case"primitive":h[j]=c(x,k,f);break;case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>t.text=c(t,k,f))):x.text=c(x,k,f)}break;case"extendedRender":"function"==typeof f[k]&&(h[j]=f[k](h[0]));break;case"mix":if(""===k)switch(g){case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>h[j][e]=t.text)):h[j]=x.text;for(let T=j-1;T>=0;T--)h[T]=e({data:h[T],objectCallback:w});function w({value:e,breadcrumbs:t}){return h[j][t]?h[j][t]:e}break;case"array":h[j]=x.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else h[j]=f[k](x)}}let m=s(h[0]),y=h[0];switch(m){case"primitive":if(null==y)return;r[i]=y;break;case"object":if(null==y.text)return;r[i]=y.text;break;case"array":r[i]=y.join("")}}})),u.push(r.join(""))})),"array"===i?u:o?o.reduce(((e,t)=>t(e)),u.join("")):u.join("")}}}const l={default:{}};const u={build:i,get:function(e,t="default"){return l[t]?l[t][e]?l[t][e]:function(){return`Error: Template "${e}" does not exist in storage "${t}".`}:function(){return`Error: Storage "${t}" does not exist.`}},add:function(e,t,r="default"){let n=t;l[r]||(l[r]={}),"function"!=typeof t&&(n=i(t)),"success"===n.name?l[r][e]=n:console.error(`Error: Template "${e}" looks broken and is not added to storage.`)},list:function(e="default"){return l[e]?Object.keys(l[e]):[]},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e,t="default"){return l[t]?l[t][e]?void delete l[t][e]:`Error: Template "${e}" does not exist in storage "${t}".`:`Error: Storage "${t}" does not exist.`}};module.exports=u;
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:c}=e;let i,l,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),l=r.indexOf(o),-1==l)return n("missingClosing");if(l<i)return n("closedBeforeOpened");if(l+=c,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(i,l));let e=t(r.slice(l));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."}}function a(t){const r={};let n=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]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:a}){let o=!1;return n=a.split("/").length-1,isNaN(e)||(o=!0),r[n]||(r[n]=o?[]:{}),"root"===e?r[n]=t:o?r[n].push(t):r[n][a]=t,t}}),{dataDeepLevel:n,nestedData:r}}var o={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function s(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 c(e,t,n){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const a="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),a?n[t](e):function(e,t){if(null==t)return null;const n=r(o)(e),a=o;return n.forEach(((e,r)=>{if(e.includes(a.TG_PRX)){const o=e.replace(a.TG_PRX,"").replace(a.TG_SFX,"").trim();t[o]&&(n[r]=t[o])}})),n.join("")}(n[t],e)}function i(n,i=!1){const{hasError:l,placeholders:u,chop:f,helpers:d,handshake:p}=function(e){const{template:t,helpers:n,handshake:a}=e,{TG_PRX:s,TG_SFX:c,TG_SIZE_P:i,TG_SIZE_S:l}=o,u=[];let f=null;const d=r(o)(t);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+c,"g");if(e.includes(s)){const a=r.exec(e);u.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),u.forEach((e=>{e.action&&e.action.every((e=>"#"===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:u,chop:d,helpers:n,handshake:a}}(n);if(l){function h(){return l}return i?[!1,h]:h}{let b=structuredClone(f);function m(r={},...n){const o=s(r),i=[];if("null"===o)return b.join("");if("string"==typeof r)switch(r){case"raw":return b.join("");case"demo":if(!p)return"Error: No handshake data.";r=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>b[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==o&&(r=[r]),r.forEach((r=>{u.forEach((n=>{const{index:o,data:i,action:l}=n;if(!l&&i){const u=r[i];switch(s(u)){case"function":return void(b[o]=u());case"primitive":return void(b[o]=u);case"array":return void("primitive"===s(u[0])&&(b[o]=u[0]));case"object":return void(u.text&&(b[o]=u.text))}}else{const{dataDeepLevel:f,nestedData:p}=a("@all"===i||null===i||"@root"===i?r:r[i]),h=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=0,a=[...e],o=0;do{r[o]=[],o++}while(o<=t);return a.every((e=>"#"===e?(n++,!(n>t)):e.startsWith("?")?(r[n].push({type:"route",name:e.replace("?",""),level:n}),!0):e.startsWith("+")?(r[n].push({type:"extendedRender",name:e.replace("+",""),level:n}),!0):e.startsWith("[]")?(r[n].push({type:"mix",name:e.replace("[]",""),level:n}),!0):e.startsWith(">")?(r[n].push({type:"data",name:e.replace(">",""),level:n}),!0):(""===e||r[n].push({type:"render",name:e,level:n}),!0))),r}(l,f),f);for(let E of h){let{type:v,name:k,level:j}=E,x=p[j],g=s(x);switch(v){case"route":switch(g){case"array":x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=d[k](e);null!=n&&("object"===r?x[t].text=c(e,n,d):x[t]=c(e,n,d))}));break;case"object":x.text=c(x,routeName,d);break;case"primitive":p[j]=c(x,routeName,d)}break;case"data":switch(g){case"array":x.forEach(((e,t)=>x[t]=e instanceof Function?d[k](e()):d[k](e)));break;case"object":case"primitive":p[j]=d[k](x);break;case"function":p[j]=d[k](x())}break;case"render":const _="function"==typeof d[k];switch(g){case"array":_?x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=d[k](e);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n})):x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=c(e,k,d);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n}));break;case"primitive":p[j]=c(x,k,d);break;case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>t.text=c(t,k,d))):x.text=c(x,k,d)}break;case"extendedRender":"function"==typeof d[k]&&(p[j]=d[k](p[0]));break;case"mix":if(""===k)switch(g){case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>p[j][e]=t.text)):p[j]=x.text;for(let T=j-1;T>=0;T--)p[T]=e({data:p[T],objectCallback:w});function w({value:e,breadcrumbs:t}){return p[j][t]?p[j][t]:e}break;case"array":p[j]=x.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else p[j]=d[k](x)}}let m=s(p[0]),y=p[0];switch(m){case"primitive":if(null==y)return;b[o]=y;break;case"object":if(null==y.text)return;b[o]=y.text;break;case"array":b[o]=y.join("")}}})),i.push(b.join(""))})),"array"===o?i:n?n.reduce(((e,t)=>t(e)),i.join("")):i.join("")}return i?[!0,m]:m}}const l={default:{}};const u={build:i,get:function(e,t="default"){return l[t]?l[t][e]?l[t][e]:function(){return`Error: Template "${e}" does not exist in storage "${t}".`}:function(){return`Error: Storage "${t}" does not exist.`}},add:function(e,t,r="default"){let n=t,a=!0;l[r]||(l[r]={}),"function"!=typeof t&&([a,n]=i(t,!0)),a?l[r][e]=n:console.error(`Error: Template "${e}" looks broken and is not added to storage.`)},list:function(e="default"){return l[e]?Object.keys(l[e]):[]},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e,t="default"){return l[t]?l[t][e]?void delete l[t][e]:`Error: Template "${e}" does not exist in storage "${t}".`:`Error: Storage "${t}" does not exist.`}};module.exports=u;
@@ -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:c}=e;let i,l,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),l=r.indexOf(o),-1==l)return n("missingClosing");if(l<i)return n("closedBeforeOpened");if(l+=c,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(i,l));let e=t(r.slice(l));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."}}function a(t){const r={};let n=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]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:a}){let o=!1;return n=a.split("/").length-1,isNaN(e)||(o=!0),r[n]||(r[n]=o?[]:{}),"root"===e?r[n]=t:o?r[n].push(t):r[n][a]=t,t}}),{dataDeepLevel:n,nestedData:r}}var o={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function s(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 c(e,t,n){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const a="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),a?n[t](e):function(e,t){if(null==t)return null;const n=r(o)(e),a=o;return n.forEach(((e,r)=>{if(e.includes(a.TG_PRX)){const o=e.replace(a.TG_PRX,"").replace(a.TG_SFX,"").trim();t[o]&&(n[r]=t[o])}})),n.join("")}(n[t],e)}function i(n){const{hasError:i,placeholders:l,chop:u,helpers:f,handshake:d}=function(e){const{template:t,helpers:n,handshake:a}=e,{TG_PRX:s,TG_SFX:c,TG_SIZE_P:i,TG_SIZE_S:l}=o,u=[];let f=null;const d=r(o)(t);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+c,"g");if(e.includes(s)){const a=r.exec(e);u.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),u.forEach((e=>{e.action&&e.action.every((e=>"#"===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:u,chop:d,helpers:n,handshake:a}}(n);if(i)return function(){return i};{let r=structuredClone(u);return function(n={},...o){const i=s(n),u=[];if("null"===i)return r.join("");if("string"==typeof n)switch(n){case"raw":return r.join("");case"demo":if(!d)return"Error: No handshake data.";n=d;break;case"handshake":return d?structuredClone(d):"Error: No handshake data.";case"placeholders":return l.map((e=>r[e.index])).join(", ");default:return`Error: Wrong command "${n}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==i&&(n=[n]),n.forEach((n=>{l.forEach((o=>{const{index:i,data:l,action:u}=o;if(!u&&l){const d=n[l];switch(s(d)){case"function":return void(r[i]=d());case"primitive":return void(r[i]=d);case"array":return void("primitive"===s(d[0])&&(r[i]=d[0]));case"object":return void(d.text&&(r[i]=d.text))}}else{const{dataDeepLevel:p,nestedData:h}=a("@all"===l||null===l||"@root"===l?n:n[l]),b=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=0,a=[...e],o=0;do{r[o]=[],o++}while(o<=t);return a.every((e=>"#"===e?(n++,!(n>t)):e.startsWith("?")?(r[n].push({type:"route",name:e.replace("?",""),level:n}),!0):e.startsWith("+")?(r[n].push({type:"extendedRender",name:e.replace("+",""),level:n}),!0):e.startsWith("[]")?(r[n].push({type:"mix",name:e.replace("[]",""),level:n}),!0):e.startsWith(">")?(r[n].push({type:"data",name:e.replace(">",""),level:n}),!0):(""===e||r[n].push({type:"render",name:e,level:n}),!0))),r}(u,p),p);for(let E of b){let{type:v,name:k,level:j}=E,x=h[j],g=s(x);switch(v){case"route":switch(g){case"array":x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=f[k](e);null!=n&&("object"===r?x[t].text=c(e,n,f):x[t]=c(e,n,f))}));break;case"object":x.text=c(x,routeName,f);break;case"primitive":h[j]=c(x,routeName,f)}break;case"data":switch(g){case"array":x.forEach(((e,t)=>x[t]=e instanceof Function?f[k](e()):f[k](e)));break;case"object":case"primitive":h[j]=f[k](x);break;case"function":h[j]=f[k](x())}break;case"render":const _="function"==typeof f[k];switch(g){case"array":_?x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=f[k](e);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n})):x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=c(e,k,f);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n}));break;case"primitive":h[j]=c(x,k,f);break;case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>t.text=c(t,k,f))):x.text=c(x,k,f)}break;case"extendedRender":"function"==typeof f[k]&&(h[j]=f[k](h[0]));break;case"mix":if(""===k)switch(g){case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>h[j][e]=t.text)):h[j]=x.text;for(let T=j-1;T>=0;T--)h[T]=e({data:h[T],objectCallback:w});function w({value:e,breadcrumbs:t}){return h[j][t]?h[j][t]:e}break;case"array":h[j]=x.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else h[j]=f[k](x)}}let m=s(h[0]),y=h[0];switch(m){case"primitive":if(null==y)return;r[i]=y;break;case"object":if(null==y.text)return;r[i]=y.text;break;case"array":r[i]=y.join("")}}})),u.push(r.join(""))})),"array"===i?u:o?o.reduce(((e,t)=>t(e)),u.join("")):u.join("")}}}const l={default:{}};const u={build:i,get:function(e,t="default"){return l[t]?l[t][e]?l[t][e]:function(){return`Error: Template "${e}" does not exist in storage "${t}".`}:function(){return`Error: Storage "${t}" does not exist.`}},add:function(e,t,r="default"){let n=t;l[r]||(l[r]={}),"function"!=typeof t&&(n=i(t)),"success"===n.name?l[r][e]=n:console.error(`Error: Template "${e}" looks broken and is not added to storage.`)},list:function(e="default"){return l[e]?Object.keys(l[e]):[]},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e,t="default"){return l[t]?l[t][e]?void delete l[t][e]:`Error: Template "${e}" does not exist in storage "${t}".`:`Error: Storage "${t}" does not exist.`}};export{u 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:c}=e;let i,l,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),l=r.indexOf(o),-1==l)return n("missingClosing");if(l<i)return n("closedBeforeOpened");if(l+=c,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(i,l));let e=t(r.slice(l));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."}}function a(t){const r={};let n=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]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:a}){let o=!1;return n=a.split("/").length-1,isNaN(e)||(o=!0),r[n]||(r[n]=o?[]:{}),"root"===e?r[n]=t:o?r[n].push(t):r[n][a]=t,t}}),{dataDeepLevel:n,nestedData:r}}var o={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function s(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 c(e,t,n){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const a="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),a?n[t](e):function(e,t){if(null==t)return null;const n=r(o)(e),a=o;return n.forEach(((e,r)=>{if(e.includes(a.TG_PRX)){const o=e.replace(a.TG_PRX,"").replace(a.TG_SFX,"").trim();t[o]&&(n[r]=t[o])}})),n.join("")}(n[t],e)}function i(n,i=!1){const{hasError:l,placeholders:u,chop:f,helpers:d,handshake:p}=function(e){const{template:t,helpers:n,handshake:a}=e,{TG_PRX:s,TG_SFX:c,TG_SIZE_P:i,TG_SIZE_S:l}=o,u=[];let f=null;const d=r(o)(t);return"string"==typeof d?f=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+c,"g");if(e.includes(s)){const a=r.exec(e);u.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),u.forEach((e=>{e.action&&e.action.every((e=>"#"===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:u,chop:d,helpers:n,handshake:a}}(n);if(l){function h(){return l}return i?[!1,h]:h}{let b=structuredClone(f);function m(r={},...n){const o=s(r),i=[];if("null"===o)return b.join("");if("string"==typeof r)switch(r){case"raw":return b.join("");case"demo":if(!p)return"Error: No handshake data.";r=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>b[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==o&&(r=[r]),r.forEach((r=>{u.forEach((n=>{const{index:o,data:i,action:l}=n;if(!l&&i){const u=r[i];switch(s(u)){case"function":return void(b[o]=u());case"primitive":return void(b[o]=u);case"array":return void("primitive"===s(u[0])&&(b[o]=u[0]));case"object":return void(u.text&&(b[o]=u.text))}}else{const{dataDeepLevel:f,nestedData:p}=a("@all"===i||null===i||"@root"===i?r:r[i]),h=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=0,a=[...e],o=0;do{r[o]=[],o++}while(o<=t);return a.every((e=>"#"===e?(n++,!(n>t)):e.startsWith("?")?(r[n].push({type:"route",name:e.replace("?",""),level:n}),!0):e.startsWith("+")?(r[n].push({type:"extendedRender",name:e.replace("+",""),level:n}),!0):e.startsWith("[]")?(r[n].push({type:"mix",name:e.replace("[]",""),level:n}),!0):e.startsWith(">")?(r[n].push({type:"data",name:e.replace(">",""),level:n}),!0):(""===e||r[n].push({type:"render",name:e,level:n}),!0))),r}(l,f),f);for(let E of h){let{type:v,name:k,level:j}=E,x=p[j],g=s(x);switch(v){case"route":switch(g){case"array":x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=d[k](e);null!=n&&("object"===r?x[t].text=c(e,n,d):x[t]=c(e,n,d))}));break;case"object":x.text=c(x,routeName,d);break;case"primitive":p[j]=c(x,routeName,d)}break;case"data":switch(g){case"array":x.forEach(((e,t)=>x[t]=e instanceof Function?d[k](e()):d[k](e)));break;case"object":case"primitive":p[j]=d[k](x);break;case"function":p[j]=d[k](x())}break;case"render":const _="function"==typeof d[k];switch(g){case"array":_?x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=d[k](e);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n})):x.forEach(((e,t)=>{if(null==e)return;const r=s(e),n=c(e,k,d);null==n&&(x[t]=null),"object"===r?e.text=n:x[t]=n}));break;case"primitive":p[j]=c(x,k,d);break;case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>t.text=c(t,k,d))):x.text=c(x,k,d)}break;case"extendedRender":"function"==typeof d[k]&&(p[j]=d[k](p[0]));break;case"mix":if(""===k)switch(g){case"object":Object.keys(x).find((e=>e.includes("/")))?Object.entries(x).forEach((([e,t])=>p[j][e]=t.text)):p[j]=x.text;for(let T=j-1;T>=0;T--)p[T]=e({data:p[T],objectCallback:w});function w({value:e,breadcrumbs:t}){return p[j][t]?p[j][t]:e}break;case"array":p[j]=x.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else p[j]=d[k](x)}}let m=s(p[0]),y=p[0];switch(m){case"primitive":if(null==y)return;b[o]=y;break;case"object":if(null==y.text)return;b[o]=y.text;break;case"array":b[o]=y.join("")}}})),i.push(b.join(""))})),"array"===o?i:n?n.reduce(((e,t)=>t(e)),i.join("")):i.join("")}return i?[!0,m]:m}}const l={default:{}};const u={build:i,get:function(e,t="default"){return l[t]?l[t][e]?l[t][e]:function(){return`Error: Template "${e}" does not exist in storage "${t}".`}:function(){return`Error: Storage "${t}" does not exist.`}},add:function(e,t,r="default"){let n=t,a=!0;l[r]||(l[r]={}),"function"!=typeof t&&([a,n]=i(t,!0)),a?l[r][e]=n:console.error(`Error: Template "${e}" looks broken and is not added to storage.`)},list:function(e="default"){return l[e]?Object.keys(l[e]):[]},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e,t="default"){return l[t]?l[t][e]?void delete l[t][e]:`Error: Template "${e}" does not exist in storage "${t}".`:`Error: Storage "${t}" does not exist.`}};export{u 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:o,TG_SFX:a,TG_SIZE_P:i,TG_SIZE_S:s}=e;let l,c,u,f=[];if("string"!=typeof n)return r("notAString");if(0==n.length)return[];if(l=n.indexOf(o),0<l&&f.push(n.slice(0,l)),-1==l)return f.push(n),f;{if(u=n.indexOf(o,l+i),c=n.indexOf(a),-1==c)return r("missingClosing");if(c<l)return r("closedBeforeOpened");if(c+=s,-1!=u&&u<c)return r("newBeforeClosed");f.push(n.slice(l,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(t){const r={};let n=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]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:o}){let a=!1;return n=o.split("/").length-1,isNaN(e)||(a=!0),r[n]||(r[n]=a?[]:{}),"root"===e?r[n]=t:a?r[n].push(t):r[n][o]=t,t}}),{dataDeepLevel:n,nestedData:r}}function*o(e,t){let r=function(e={}){let t=[],{type:r,limit:n,onLimit:o}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),a="LIFO"===r.toUpperCase(),i=!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 l(e=1,r=0){let n=[],o=t.length-r;return e>1&&Array.from({length:e}).map((()=>{null!=t[o-1]&&n.push(t[o-1]),o--})),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:l,peekReverse:function(e=1,t=0){const r=l(e,t);return r instanceof Array?r.reverse():r},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=a?function(e){const r=e instanceof Array,a=r?e.length:1;let l=!1;if("full"!==o||!i){if(n&&r&&a>n&&(e=e.slice(0,-a+n)),n){const a=(r?e.length:1)+t.length;a>=n&&"full"===o&&(e=e.slice(0,-(a-n))),a>=n&&"update"===o&&(l=s(a-n))}return t=e instanceof Array?t.concat(e):t.concat([e]),i=!!n&&t.length===n,l||void 0}}:function(e){const r=e instanceof Array,a=r?e.length:1;let l=!1;if("full"!==o||!i){if(n&&r&&a>n&&(e=e.slice(a-n)),n){const a=(r?e.length:1)+t.length;a>=n&&"full"===o&&(e=e.slice(0,-(a-n))),a>=n&&"update"===o&&(l=s(a-n))}return t=r?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),i=!!n&&t.length===n,l||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 i(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,r,n){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const o="function"==typeof n[r];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),o?n[r](e):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[a]&&(n[t]=r[a])}})),n.join("")}(n[r],e)}function l(r){const{hasError:l,placeholders:c,chop:u,helpers:f,handshake:p}=function(e){const{template:r,helpers:n,handshake:o}=e,{TG_PRX:i,TG_SFX:s,TG_SIZE_P:l,TG_SIZE_S:c}=a,u=[];let f=null;const p=t(a)(r);return"string"==typeof p?f=p:p.forEach(((e,t)=>{const r=RegExp(i+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(i)){const o=r.exec(e);u.push({index:t,data:(n=o[1],n||null),action:o[2]?o[2].split(",").map((e=>e.trim())):null})}var n})),u.forEach((e=>{e.action&&e.action.every((e=>"#"===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:u,chop:p,helpers:n,handshake:o}}(r);if(l)return function(){return l};{let t=structuredClone(u);return function(r={},...a){const l=i(r),u=[];if("null"===l)return t.join("");if("string"==typeof r)switch(r){case"raw":return t.join("");case"demo":if(!p)return"Error: No handshake data.";r=p;break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return c.map((e=>t[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==l&&(r=[r]),r.forEach((r=>{c.forEach((a=>{const{index:l,data:c,action:u}=a;if(!u&&c){const p=r[c];switch(i(p)){case"function":return void(t[l]=p());case"primitive":return void(t[l]=p);case"array":return void("primitive"===i(p[0])&&(t[l]=p[0]));case"object":return void(p.text&&(t[l]=p.text))}}else{const{dataDeepLevel:d,nestedData:h}=n("@all"===c||null===c||"@root"===c?r:r[c]),m=o(function(e,t=10){let r={},n=0,o=[...e],a=0;do{r[a]=[],a++}while(a<=t);return o.every((e=>"#"===e?(n++,!(n>t)):e.startsWith("?")?(r[n].push({type:"route",name:e.replace("?",""),level:n}),!0):e.startsWith("+")?(r[n].push({type:"extendedRender",name:e.replace("+",""),level:n}),!0):e.startsWith("[]")?(r[n].push({type:"mix",name:e.replace("[]",""),level:n}),!0):e.startsWith(">")?(r[n].push({type:"data",name:e.replace(">",""),level:n}),!0):(""===e||r[n].push({type:"render",name:e,level:n}),!0))),r}(u,d),d);for(let g of m){let{type:v,name:E,level:k}=g,j=h[k],x=i(j);switch(v){case"route":switch(x){case"array":j.forEach(((e,t)=>{if(null==e)return;const r=i(e),n=f[E](e);null!=n&&("object"===r?j[t].text=s(e,n,f):j[t]=s(e,n,f))}));break;case"object":j.text=s(j,routeName,f);break;case"primitive":h[k]=s(j,routeName,f)}break;case"data":switch(x){case"array":j.forEach(((e,t)=>j[t]=e instanceof Function?f[E](e()):f[E](e)));break;case"object":case"primitive":h[k]=f[E](j);break;case"function":h[k]=f[E](j())}break;case"render":const w="function"==typeof f[E];switch(x){case"array":w?j.forEach(((e,t)=>{if(null==e)return;const r=i(e),n=f[E](e);null==n&&(j[t]=null),"object"===r?e.text=n:j[t]=n})):j.forEach(((e,t)=>{if(null==e)return;const r=i(e),n=s(e,E,f);null==n&&(j[t]=null),"object"===r?e.text=n:j[t]=n}));break;case"primitive":h[k]=s(j,E,f);break;case"object":Object.keys(j).find((e=>e.includes("/")))?Object.entries(j).forEach((([e,t])=>t.text=s(t,E,f))):j.text=s(j,E,f)}break;case"extendedRender":"function"==typeof f[E]&&(h[k]=f[E](h[0]));break;case"mix":if(""===E)switch(x){case"object":Object.keys(j).find((e=>e.includes("/")))?Object.entries(j).forEach((([e,t])=>h[k][e]=t.text)):h[k]=j.text;for(let _=k-1;_>=0;_--)h[_]=e({data:h[_],objectCallback:T});function T({value:e,breadcrumbs:t}){return h[k][t]?h[k][t]:e}break;case"array":h[k]=j.map((e=>"object"===i(e)?e.text:e)).filter((e=>null!=e)).join("")}else h[k]=f[E](j)}}let y=i(h[0]),b=h[0];switch(y){case"primitive":if(null==b)return;t[l]=b;break;case"object":if(null==b.text)return;t[l]=b.text;break;case"array":t[l]=b.join("")}}})),u.push(t.join(""))})),"array"===l?u:a?a.reduce(((e,t)=>t(e)),u.join("")):u.join("")}}}const c={default:{}};return{build:l,get:function(e,t="default"){return c[t]?c[t][e]?c[t][e]:function(){return`Error: Template "${e}" does not exist in storage "${t}".`}:function(){return`Error: Storage "${t}" does not exist.`}},add:function(e,t,r="default"){let n=t;c[r]||(c[r]={}),"function"!=typeof t&&(n=l(t)),"success"===n.name?c[r][e]=n:console.error(`Error: Template "${e}" looks broken and is not added to storage.`)},list:function(e="default"){return c[e]?Object.keys(c[e]):[]},clear:function(){Object.keys(c).forEach((e=>{"default"!=e?delete c[e]:c.default={}}))},remove:function(e,t="default"){return c[t]?c[t][e]?void delete c[t][e]:`Error: Template "${e}" does not exist in storage "${t}".`:`Error: Storage "${t}" 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:o,TG_SFX:a,TG_SIZE_P:i,TG_SIZE_S:l}=e;let s,c,u,f=[];if("string"!=typeof n)return r("notAString");if(0==n.length)return[];if(s=n.indexOf(o),0<s&&f.push(n.slice(0,s)),-1==s)return f.push(n),f;{if(u=n.indexOf(o,s+i),c=n.indexOf(a),-1==c)return r("missingClosing");if(c<s)return r("closedBeforeOpened");if(c+=l,-1!=u&&u<c)return r("newBeforeClosed");f.push(n.slice(s,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(t){const r={};let n=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]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:o}){let a=!1;return n=o.split("/").length-1,isNaN(e)||(a=!0),r[n]||(r[n]=a?[]:{}),"root"===e?r[n]=t:a?r[n].push(t):r[n][o]=t,t}}),{dataDeepLevel:n,nestedData:r}}function*o(e,t){let r=function(e={}){let t=[],{type:r,limit:n,onLimit:o}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),a="LIFO"===r.toUpperCase(),i=!1;function l(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 s(e=1,r=0){let n=[],o=t.length-r;return e>1&&Array.from({length:e}).map((()=>{null!=t[o-1]&&n.push(t[o-1]),o--})),1==e?t[t.length-1]:n}function c(){}return c.prototype={pull:l,pullReverse:function(e=1,t=0){let r=l(e,t);return r instanceof Array?r.reverse():r},peek:s,peekReverse:function(e=1,t=0){const r=s(e,t);return r instanceof Array?r.reverse():r},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=a?function(e){const r=e instanceof Array,a=r?e.length:1;let s=!1;if("full"!==o||!i){if(n&&r&&a>n&&(e=e.slice(0,-a+n)),n){const a=(r?e.length:1)+t.length;a>=n&&"full"===o&&(e=e.slice(0,-(a-n))),a>=n&&"update"===o&&(s=l(a-n))}return t=e instanceof Array?t.concat(e):t.concat([e]),i=!!n&&t.length===n,s||void 0}}:function(e){const r=e instanceof Array,a=r?e.length:1;let s=!1;if("full"!==o||!i){if(n&&r&&a>n&&(e=e.slice(a-n)),n){const a=(r?e.length:1)+t.length;a>=n&&"full"===o&&(e=e.slice(0,-(a-n))),a>=n&&"update"===o&&(s=l(a-n))}return t=r?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),i=!!n&&t.length===n,s||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 i(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){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const o="function"==typeof n[r];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),o?n[r](e):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[a]&&(n[t]=r[a])}})),n.join("")}(n[r],e)}function s(r,s=!1){const{hasError:c,placeholders:u,chop:f,helpers:p,handshake:d}=function(e){const{template:r,helpers:n,handshake:o}=e,{TG_PRX:i,TG_SFX:l,TG_SIZE_P:s,TG_SIZE_S:c}=a,u=[];let f=null;const p=t(a)(r);return"string"==typeof p?f=p:p.forEach(((e,t)=>{const r=RegExp(i+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(i)){const o=r.exec(e);u.push({index:t,data:(n=o[1],n||null),action:o[2]?o[2].split(",").map((e=>e.trim())):null})}var n})),u.forEach((e=>{e.action&&e.action.every((e=>"#"===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:u,chop:p,helpers:n,handshake:o}}(r);if(c){function h(){return c}return s?[!1,h]:h}{let y=structuredClone(f);function m(t={},...r){const a=i(t),s=[];if("null"===a)return y.join("");if("string"==typeof t)switch(t){case"raw":return y.join("");case"demo":if(!d)return"Error: No handshake data.";t=d;break;case"handshake":return d?structuredClone(d):"Error: No handshake data.";case"placeholders":return u.map((e=>y[e.index])).join(", ");default:return`Error: Wrong command "${t}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==a&&(t=[t]),t.forEach((t=>{u.forEach((r=>{const{index:a,data:s,action:c}=r;if(!c&&s){const u=t[s];switch(i(u)){case"function":return void(y[a]=u());case"primitive":return void(y[a]=u);case"array":return void("primitive"===i(u[0])&&(y[a]=u[0]));case"object":return void(u.text&&(y[a]=u.text))}}else{const{dataDeepLevel:f,nestedData:d}=n("@all"===s||null===s||"@root"===s?t:t[s]),h=o(function(e,t=10){let r={},n=0,o=[...e],a=0;do{r[a]=[],a++}while(a<=t);return o.every((e=>"#"===e?(n++,!(n>t)):e.startsWith("?")?(r[n].push({type:"route",name:e.replace("?",""),level:n}),!0):e.startsWith("+")?(r[n].push({type:"extendedRender",name:e.replace("+",""),level:n}),!0):e.startsWith("[]")?(r[n].push({type:"mix",name:e.replace("[]",""),level:n}),!0):e.startsWith(">")?(r[n].push({type:"data",name:e.replace(">",""),level:n}),!0):(""===e||r[n].push({type:"render",name:e,level:n}),!0))),r}(c,f),f);for(let g of h){let{type:v,name:E,level:k}=g,j=d[k],x=i(j);switch(v){case"route":switch(x){case"array":j.forEach(((e,t)=>{if(null==e)return;const r=i(e),n=p[E](e);null!=n&&("object"===r?j[t].text=l(e,n,p):j[t]=l(e,n,p))}));break;case"object":j.text=l(j,routeName,p);break;case"primitive":d[k]=l(j,routeName,p)}break;case"data":switch(x){case"array":j.forEach(((e,t)=>j[t]=e instanceof Function?p[E](e()):p[E](e)));break;case"object":case"primitive":d[k]=p[E](j);break;case"function":d[k]=p[E](j())}break;case"render":const w="function"==typeof p[E];switch(x){case"array":w?j.forEach(((e,t)=>{if(null==e)return;const r=i(e),n=p[E](e);null==n&&(j[t]=null),"object"===r?e.text=n:j[t]=n})):j.forEach(((e,t)=>{if(null==e)return;const r=i(e),n=l(e,E,p);null==n&&(j[t]=null),"object"===r?e.text=n:j[t]=n}));break;case"primitive":d[k]=l(j,E,p);break;case"object":Object.keys(j).find((e=>e.includes("/")))?Object.entries(j).forEach((([e,t])=>t.text=l(t,E,p))):j.text=l(j,E,p)}break;case"extendedRender":"function"==typeof p[E]&&(d[k]=p[E](d[0]));break;case"mix":if(""===E)switch(x){case"object":Object.keys(j).find((e=>e.includes("/")))?Object.entries(j).forEach((([e,t])=>d[k][e]=t.text)):d[k]=j.text;for(let _=k-1;_>=0;_--)d[_]=e({data:d[_],objectCallback:T});function T({value:e,breadcrumbs:t}){return d[k][t]?d[k][t]:e}break;case"array":d[k]=j.map((e=>"object"===i(e)?e.text:e)).filter((e=>null!=e)).join("")}else d[k]=p[E](j)}}let m=i(d[0]),b=d[0];switch(m){case"primitive":if(null==b)return;y[a]=b;break;case"object":if(null==b.text)return;y[a]=b.text;break;case"array":y[a]=b.join("")}}})),s.push(y.join(""))})),"array"===a?s:r?r.reduce(((e,t)=>t(e)),s.join("")):s.join("")}return s?[!0,m]:m}}const c={default:{}};return{build:s,get:function(e,t="default"){return c[t]?c[t][e]?c[t][e]:function(){return`Error: Template "${e}" does not exist in storage "${t}".`}:function(){return`Error: Storage "${t}" does not exist.`}},add:function(e,t,r="default"){let n=t,o=!0;c[r]||(c[r]={}),"function"!=typeof t&&([o,n]=s(t,!0)),o?c[r][e]=n:console.error(`Error: Template "${e}" looks broken and is not added to storage.`)},list:function(e="default"){return c[e]?Object.keys(c[e]):[]},clear:function(){Object.keys(c).forEach((e=>{"default"!=e?delete c[e]:c.default={}}))},remove:function(e,t="default"){return c[t]?c[t][e]?void delete c[t][e]:`Error: Template "${e}" does not exist in storage "${t}".`:`Error: Storage "${t}" 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": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "license": "MIT",
6
6
  "author": "Peter Naydenov",
7
7
  "main": "./dist/morph.umd.js",
@@ -22,9 +22,9 @@
22
22
  "build": "rollup -c"
23
23
  },
24
24
  "dependencies": {
25
- "@peter.naydenov/stack": "^3.0.0",
26
- "@peter.naydenov/walk": "^5.0.0"
27
- },
25
+ "@peter.naydenov/stack": "^3.0.0",
26
+ "@peter.naydenov/walk": "^5.0.0"
27
+ },
28
28
  "devDependencies": {
29
29
  "@rollup/plugin-commonjs": "^28.0.0",
30
30
  "@rollup/plugin-node-resolve": "^15.3.0",
@@ -32,7 +32,7 @@
32
32
  "c8": "^10.1.2",
33
33
  "chai": "5.1.2",
34
34
  "mocha": "10.8.2",
35
- "rollup": "^4.27.4"
35
+ "rollup": "^4.28.0"
36
36
  },
37
37
  "repository": {
38
38
  "type": "git",
@@ -57,5 +57,4 @@
57
57
  "string",
58
58
  "function"
59
59
  ]
60
-
61
60
  }
package/src/main.js CHANGED
@@ -37,10 +37,12 @@ function get ( prop, strName='default' ) {
37
37
 
38
38
  function add ( name, tplfn, strName='default' ) {
39
39
  let fn = tplfn;
40
+ let successBuild = true;
40
41
  if( !storage[strName] ) storage[strName] = {}
41
- if ( typeof tplfn !== 'function' ) fn = build ( tplfn );
42
- if ( fn.name === 'success' ) storage[strName][name] = fn
43
- else console.error ( `Error: Template "${name}" looks broken and is not added to storage.` )
42
+ if ( typeof tplfn !== 'function' ) [ successBuild, fn ] = build ( tplfn, true )
43
+
44
+ if ( successBuild ) storage[strName][name] = fn
45
+ else console.error ( `Error: Template "${name}" looks broken and is not added to storage.` )
44
46
  } // add func.
45
47
 
46
48
 
@@ -12,18 +12,17 @@ import walk from '@peter.naydenov/walk'
12
12
 
13
13
 
14
14
 
15
- function build ( tpl ) {
15
+ function build ( tpl, extra=false ) {
16
16
  const { hasError, placeholders, chop, helpers, handshake } = _readTemplate ( tpl );
17
17
  if ( hasError ) {
18
- return function fail () {
19
- return hasError
20
- }
18
+ function fail () { return hasError }
19
+ return extra ? [ false, fail ] : fail
21
20
  }
22
- else { // If no errors:
21
+ else { // If NO Error:
23
22
  let cuts = structuredClone ( chop );
24
23
  // *** Template recognition complete. Start building the rendering function -->
25
24
 
26
- return function success ( d={}, ...args ) {
25
+ function success ( d={}, ...args ) {
27
26
  const
28
27
  topLevelType = _defineDataType ( d )
29
28
  , endData = []
@@ -229,7 +228,9 @@ import walk from '@peter.naydenov/walk'
229
228
  else return endData.join ( '' )
230
229
 
231
230
  } // success func.
231
+ return extra ? [ true, success ] : success
232
232
  }
233
+
233
234
  } // build func.
234
235
 
235
236