@peter.naydenov/morph 1.1.2 → 1.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
@@ -2,7 +2,12 @@
2
2
 
3
3
 
4
4
 
5
- ### 1.1.1 ( 2025-01-20 )
5
+ ### 1.1.3 (2025-01-23)
6
+ - [x] Improve: Error messages when using method "add" with template -> null;
7
+
8
+
9
+
10
+ ### 1.1.2 ( 2025-01-20 )
6
11
  - [x] Improve: Error messages when using method "set" with string;
7
12
 
8
13
 
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,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]};if(!r.includes("#"))return{dataDeepLevel:0,nestedData:[t]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:r}){let o=!1;return a=r.split("/").length-1,isNaN(e)||(o=!0),n[a]||(n[a]=o?[]:{}),"root"===e?n[a]=t:o?n[a].push(t):n[a][r]=t,t}}),{dataDeepLevel:a,nestedData:n}}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,...a){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...a):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,l={}){const{hasError:u,placeholders:f,chop:d,helpers:p,handshake:h}=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=[],f="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let d=null;const p=r(o)(f);return"string"==typeof p?d=p:p.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]||(d=`Error: Missing helper: ${e}`,!1))))})),{hasError:d,placeholders:u,chop:p,helpers:n,handshake:a}}(n);if(u){function b(){return u}return i?[!1,b]:b}{let m=structuredClone(d);function y(r={},n={},...o){const i=s(r),u=[];if(r=e({data:r}),"null"===i)return m.join("");if("string"==typeof r)switch(r){case"raw":return m.join("");case"demo":if(!h)return"Error: No handshake data.";r=h;break;case"handshake":return h?structuredClone(h):"Error: No handshake data.";case"placeholders":return f.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==i&&(r=[r]),r.forEach((r=>{f.forEach((o=>{const{index:i,data:u,action:f}=o;if(!f&&u){const d=r[u];switch(s(d)){case"function":return void(m[i]=d());case"primitive":return void(m[i]=d);case"array":return void("primitive"===s(d[0])&&(m[i]=d[0]));case"object":return void(d.text&&(m[i]=d.text))}}else{const{dataDeepLevel:h,nestedData:b}=a("@all"===u||null===u||"@root"===u?r:r[u],f),y=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}(f,h),h);for(let v of y){let{type:j,name:k,level:x}=v,_=b[x],w=s(_);switch(j){case"route":switch(w){case"array":_.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e);null!=a&&("object"===r?_[t].text=c(e,a,p,{...l,...n}):_[t]=c(e,a,p,{...l,...n}))}));break;case"object":_.text=c(_,k,p,{...l,...n});break;case"primitive":b[x]=c(_,k,p,{...l,...n})}break;case"data":switch(w){case"array":_.forEach(((e,t)=>_[t]=e instanceof Function?p[k](e()):p[k](e)));break;case"object":case"primitive":b[x]=p[k](_);break;case"function":b[x]=p[k](_())}break;case"render":const S="function"==typeof p[k];switch(w){case"array":S?_.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e,{...l,...n});null==a&&(_[t]=null),"object"===r?e.text=a:_[t]=a})):_.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=c(e,k,p,{...l,...n});null==a&&(_[t]=null),"object"===r?e.text=a:_[t]=a}));break;case"function":b[x]=p[k](_(),{...l,...n});break;case"primitive":b[x]=c(_,k,p,{...l,...n});break;case"object":if(S)b[x].text=p[k](_,{...l,...n});else{Object.keys(_).find((e=>e.includes("/")))?Object.entries(_).forEach((([e,t])=>t.text=c(t,k,p,{...l,...n}))):_.text=c(_,k,p,{...l,...n})}}break;case"extendedRender":"function"==typeof p[k]&&(b[x]=p[k](b[0]));break;case"mix":if(""===k)switch(w){case"object":Object.keys(_).find((e=>e.includes("/")))?Object.entries(_).forEach((([e,t])=>b[x][e]=t.text)):b[x]=_.text;for(let O=x-1;O>=0;O--)b[O]=e({data:b[O],objectCallback:T});function T({value:e,breadcrumbs:t}){return b[x][t]?b[x][t]:e}break;case"array":b[x]=_.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else b[x]=p[k](_)}}let E=s(b[0]),g=b[0];switch(E){case"primitive":if(null==g)return;m[i]=g;break;case"object":if(null==g.text)return;m[i]=g.text;break;case"array":const G=s(g[0]);m[i]="object"===G?g.map((e=>e.text)).join(""):g.join("")}}})),u.push(m.join(""))})),"array"===i?u:o?o.reduce(((e,t)=>"function"!=typeof t?e:t(e,{...l,...n})),u.join("")):u.join("")}return i?[!0,y]:y}}const l={default:{}};const u={build:i,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 l[r]?l[r][t]?l[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;let o=t,s=!0;if(l[a]||(l[a]={}),"function"!=typeof t){let e=i(t,!0,...r);s=e[0],o=e[1]}s?l[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>l[e]?Object.keys(l[e]):[])).flat()},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e){const[t,r="default"]=e;return l[r]?l[r][t]?void delete l[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" 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:i}=e;let c,l,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(c=r.indexOf(a),0<c&&f.push(r.slice(0,c)),-1==c)return f.push(r),f;{if(u=r.indexOf(a,c+s),l=r.indexOf(o),-1==l)return n("missingClosing");if(l<c)return n("closedBeforeOpened");if(l+=i,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(c,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,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]};if(!r.includes("#"))return{dataDeepLevel:0,nestedData:[t]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:r}){let o=!1;return a=r.split("/").length-1,isNaN(e)||(o=!0),n[a]||(n[a]=o?[]:{}),"root"===e?n[a]=t:o?n[a].push(t):n[a][r]=t,t}}),{dataDeepLevel:a,nestedData:n}}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 i(e,t,n,...a){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...a):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 c(n,c=!1,l={}){const{hasError:u,placeholders:f,chop:d,helpers:p,handshake:h}=function(e){const{template:t,helpers:n={},handshake:a}=e,{TG_PRX:s,TG_SFX:i,TG_SIZE_P:c,TG_SIZE_S:l}=o,u=[],f="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let d=null;const p=r(o)(f);return"string"==typeof p?d=p:p.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+i,"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]||(d=`Error: Missing helper: ${e}`,!1))))})),{hasError:d,placeholders:u,chop:p,helpers:n,handshake:a}}(n);if(u){function b(){return u}return c?[!1,b]:b}{let m=structuredClone(d);function y(r={},n={},...o){const c=s(r),u=[];if(r=e({data:r}),"null"===c)return m.join("");if("string"==typeof r)switch(r){case"raw":return m.join("");case"demo":if(!h)return"Error: No handshake data.";r=h;break;case"handshake":return h?structuredClone(h):"Error: No handshake data.";case"placeholders":return f.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==c&&(r=[r]),r.forEach((r=>{f.forEach((o=>{const{index:c,data:u,action:f}=o;if(!f&&u){const d=r[u];switch(s(d)){case"function":return void(m[c]=d());case"primitive":return void(m[c]=d);case"array":return void("primitive"===s(d[0])&&(m[c]=d[0]));case"object":return void(d.text&&(m[c]=d.text))}}else{const{dataDeepLevel:h,nestedData:b}=a("@all"===u||null===u||"@root"===u?r:r[u],f),y=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}(f,h),h);for(let v of y){let{type:j,name:k,level:x}=v,T=b[x],_=s(T);switch(j){case"route":switch(_){case"array":T.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e);null!=a&&("object"===r?T[t].text=i(e,a,p,{...l,...n}):T[t]=i(e,a,p,{...l,...n}))}));break;case"object":T.text=i(T,k,p,{...l,...n});break;case"primitive":b[x]=i(T,k,p,{...l,...n})}break;case"data":switch(_){case"array":T.forEach(((e,t)=>T[t]=e instanceof Function?p[k](e()):p[k](e)));break;case"object":case"primitive":b[x]=p[k](T);break;case"function":b[x]=p[k](T())}break;case"render":const w="function"==typeof p[k];switch(_){case"array":w?T.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e,{...l,...n});null==a&&(T[t]=null),"object"===r?e.text=a:T[t]=a})):T.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=i(e,k,p,{...l,...n});null==a&&(T[t]=null),"object"===r?e.text=a:T[t]=a}));break;case"function":b[x]=p[k](T(),{...l,...n});break;case"primitive":b[x]=i(T,k,p,{...l,...n});break;case"object":if(w)b[x].text=p[k](T,{...l,...n});else{Object.keys(T).find((e=>e.includes("/")))?Object.entries(T).forEach((([e,t])=>t.text=i(t,k,p,{...l,...n}))):T.text=i(T,k,p,{...l,...n})}}break;case"extendedRender":"function"==typeof p[k]&&(b[x]=p[k](b[0]));break;case"mix":if(""===k)switch(_){case"object":Object.keys(T).find((e=>e.includes("/")))?Object.entries(T).forEach((([e,t])=>b[x][e]=t.text)):b[x]=T.text;for(let O=x-1;O>=0;O--)b[O]=e({data:b[O],objectCallback:S});function S({value:e,breadcrumbs:t}){return b[x][t]?b[x][t]:e}break;case"array":b[x]=T.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else b[x]=p[k](T)}}let g=s(b[0]),E=b[0];switch(g){case"primitive":if(null==E)return;m[c]=E;break;case"object":if(null==E.text)return;m[c]=E.text;break;case"array":const G=s(E[0]);m[c]="object"===G?E.map((e=>e.text)).join(""):E.join("")}}})),u.push(m.join(""))})),"array"===c?u:o?o.reduce(((e,t)=>"function"!=typeof t?e:t(e,{...l,...n})),u.join("")):u.join("")}return c?[!0,y]:y}}const l={default:{}};const u={build:c,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 l[r]?l[r][t]?l[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(l[a]||(l[a]={}),"function"!=typeof t){let e=c(t,!0,...r);s=e[0],o=e[1]}s?l[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>l[e]?Object.keys(l[e]):[])).flat()},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e){const[t,r="default"]=e;return l[r]?l[r][t]?void delete l[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" 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,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]};if(!r.includes("#"))return{dataDeepLevel:0,nestedData:[t]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:r}){let o=!1;return a=r.split("/").length-1,isNaN(e)||(o=!0),n[a]||(n[a]=o?[]:{}),"root"===e?n[a]=t:o?n[a].push(t):n[a][r]=t,t}}),{dataDeepLevel:a,nestedData:n}}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,...a){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...a):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,l={}){const{hasError:u,placeholders:f,chop:d,helpers:p,handshake:h}=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=[],f="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let d=null;const p=r(o)(f);return"string"==typeof p?d=p:p.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]||(d=`Error: Missing helper: ${e}`,!1))))})),{hasError:d,placeholders:u,chop:p,helpers:n,handshake:a}}(n);if(u){function b(){return u}return i?[!1,b]:b}{let m=structuredClone(d);function y(r={},n={},...o){const i=s(r),u=[];if(r=e({data:r}),"null"===i)return m.join("");if("string"==typeof r)switch(r){case"raw":return m.join("");case"demo":if(!h)return"Error: No handshake data.";r=h;break;case"handshake":return h?structuredClone(h):"Error: No handshake data.";case"placeholders":return f.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==i&&(r=[r]),r.forEach((r=>{f.forEach((o=>{const{index:i,data:u,action:f}=o;if(!f&&u){const d=r[u];switch(s(d)){case"function":return void(m[i]=d());case"primitive":return void(m[i]=d);case"array":return void("primitive"===s(d[0])&&(m[i]=d[0]));case"object":return void(d.text&&(m[i]=d.text))}}else{const{dataDeepLevel:h,nestedData:b}=a("@all"===u||null===u||"@root"===u?r:r[u],f),y=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}(f,h),h);for(let v of y){let{type:j,name:k,level:x}=v,_=b[x],w=s(_);switch(j){case"route":switch(w){case"array":_.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e);null!=a&&("object"===r?_[t].text=c(e,a,p,{...l,...n}):_[t]=c(e,a,p,{...l,...n}))}));break;case"object":_.text=c(_,k,p,{...l,...n});break;case"primitive":b[x]=c(_,k,p,{...l,...n})}break;case"data":switch(w){case"array":_.forEach(((e,t)=>_[t]=e instanceof Function?p[k](e()):p[k](e)));break;case"object":case"primitive":b[x]=p[k](_);break;case"function":b[x]=p[k](_())}break;case"render":const S="function"==typeof p[k];switch(w){case"array":S?_.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e,{...l,...n});null==a&&(_[t]=null),"object"===r?e.text=a:_[t]=a})):_.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=c(e,k,p,{...l,...n});null==a&&(_[t]=null),"object"===r?e.text=a:_[t]=a}));break;case"function":b[x]=p[k](_(),{...l,...n});break;case"primitive":b[x]=c(_,k,p,{...l,...n});break;case"object":if(S)b[x].text=p[k](_,{...l,...n});else{Object.keys(_).find((e=>e.includes("/")))?Object.entries(_).forEach((([e,t])=>t.text=c(t,k,p,{...l,...n}))):_.text=c(_,k,p,{...l,...n})}}break;case"extendedRender":"function"==typeof p[k]&&(b[x]=p[k](b[0]));break;case"mix":if(""===k)switch(w){case"object":Object.keys(_).find((e=>e.includes("/")))?Object.entries(_).forEach((([e,t])=>b[x][e]=t.text)):b[x]=_.text;for(let O=x-1;O>=0;O--)b[O]=e({data:b[O],objectCallback:T});function T({value:e,breadcrumbs:t}){return b[x][t]?b[x][t]:e}break;case"array":b[x]=_.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else b[x]=p[k](_)}}let E=s(b[0]),g=b[0];switch(E){case"primitive":if(null==g)return;m[i]=g;break;case"object":if(null==g.text)return;m[i]=g.text;break;case"array":const G=s(g[0]);m[i]="object"===G?g.map((e=>e.text)).join(""):g.join("")}}})),u.push(m.join(""))})),"array"===i?u:o?o.reduce(((e,t)=>"function"!=typeof t?e:t(e,{...l,...n})),u.join("")):u.join("")}return i?[!0,y]:y}}const l={default:{}};const u={build:i,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 l[r]?l[r][t]?l[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;let o=t,s=!0;if(l[a]||(l[a]={}),"function"!=typeof t){let e=i(t,!0,...r);s=e[0],o=e[1]}s?l[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>l[e]?Object.keys(l[e]):[])).flat()},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e){const[t,r="default"]=e;return l[r]?l[r][t]?void delete l[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" 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:i}=e;let c,l,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(c=r.indexOf(a),0<c&&f.push(r.slice(0,c)),-1==c)return f.push(r),f;{if(u=r.indexOf(a,c+s),l=r.indexOf(o),-1==l)return n("missingClosing");if(l<c)return n("closedBeforeOpened");if(l+=i,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(c,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,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]};if(!r.includes("#"))return{dataDeepLevel:0,nestedData:[t]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:r}){let o=!1;return a=r.split("/").length-1,isNaN(e)||(o=!0),n[a]||(n[a]=o?[]:{}),"root"===e?n[a]=t:o?n[a].push(t):n[a][r]=t,t}}),{dataDeepLevel:a,nestedData:n}}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 i(e,t,n,...a){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text)}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...a):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 c(n,c=!1,l={}){const{hasError:u,placeholders:f,chop:d,helpers:p,handshake:h}=function(e){const{template:t,helpers:n={},handshake:a}=e,{TG_PRX:s,TG_SFX:i,TG_SIZE_P:c,TG_SIZE_S:l}=o,u=[],f="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let d=null;const p=r(o)(f);return"string"==typeof p?d=p:p.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+i,"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]||(d=`Error: Missing helper: ${e}`,!1))))})),{hasError:d,placeholders:u,chop:p,helpers:n,handshake:a}}(n);if(u){function b(){return u}return c?[!1,b]:b}{let m=structuredClone(d);function y(r={},n={},...o){const c=s(r),u=[];if(r=e({data:r}),"null"===c)return m.join("");if("string"==typeof r)switch(r){case"raw":return m.join("");case"demo":if(!h)return"Error: No handshake data.";r=h;break;case"handshake":return h?structuredClone(h):"Error: No handshake data.";case"placeholders":return f.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==c&&(r=[r]),r.forEach((r=>{f.forEach((o=>{const{index:c,data:u,action:f}=o;if(!f&&u){const d=r[u];switch(s(d)){case"function":return void(m[c]=d());case"primitive":return void(m[c]=d);case"array":return void("primitive"===s(d[0])&&(m[c]=d[0]));case"object":return void(d.text&&(m[c]=d.text))}}else{const{dataDeepLevel:h,nestedData:b}=a("@all"===u||null===u||"@root"===u?r:r[u],f),y=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}(f,h),h);for(let v of y){let{type:j,name:k,level:x}=v,T=b[x],_=s(T);switch(j){case"route":switch(_){case"array":T.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e);null!=a&&("object"===r?T[t].text=i(e,a,p,{...l,...n}):T[t]=i(e,a,p,{...l,...n}))}));break;case"object":T.text=i(T,k,p,{...l,...n});break;case"primitive":b[x]=i(T,k,p,{...l,...n})}break;case"data":switch(_){case"array":T.forEach(((e,t)=>T[t]=e instanceof Function?p[k](e()):p[k](e)));break;case"object":case"primitive":b[x]=p[k](T);break;case"function":b[x]=p[k](T())}break;case"render":const w="function"==typeof p[k];switch(_){case"array":w?T.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=p[k](e,{...l,...n});null==a&&(T[t]=null),"object"===r?e.text=a:T[t]=a})):T.forEach(((e,t)=>{if(null==e)return;const r=s(e),a=i(e,k,p,{...l,...n});null==a&&(T[t]=null),"object"===r?e.text=a:T[t]=a}));break;case"function":b[x]=p[k](T(),{...l,...n});break;case"primitive":b[x]=i(T,k,p,{...l,...n});break;case"object":if(w)b[x].text=p[k](T,{...l,...n});else{Object.keys(T).find((e=>e.includes("/")))?Object.entries(T).forEach((([e,t])=>t.text=i(t,k,p,{...l,...n}))):T.text=i(T,k,p,{...l,...n})}}break;case"extendedRender":"function"==typeof p[k]&&(b[x]=p[k](b[0]));break;case"mix":if(""===k)switch(_){case"object":Object.keys(T).find((e=>e.includes("/")))?Object.entries(T).forEach((([e,t])=>b[x][e]=t.text)):b[x]=T.text;for(let O=x-1;O>=0;O--)b[O]=e({data:b[O],objectCallback:S});function S({value:e,breadcrumbs:t}){return b[x][t]?b[x][t]:e}break;case"array":b[x]=T.map((e=>"object"===s(e)?e.text:e)).filter((e=>null!=e)).join("")}else b[x]=p[k](T)}}let g=s(b[0]),E=b[0];switch(g){case"primitive":if(null==E)return;m[c]=E;break;case"object":if(null==E.text)return;m[c]=E.text;break;case"array":const G=s(E[0]);m[c]="object"===G?E.map((e=>e.text)).join(""):E.join("")}}})),u.push(m.join(""))})),"array"===c?u:o?o.reduce(((e,t)=>"function"!=typeof t?e:t(e,{...l,...n})),u.join("")):u.join("")}return c?[!0,y]:y}}const l={default:{}};const u={build:c,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 l[r]?l[r][t]?l[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(l[a]||(l[a]={}),"function"!=typeof t){let e=c(t,!0,...r);s=e[0],o=e[1]}s?l[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>l[e]?Object.keys(l[e]):[])).flat()},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e){const[t,r="default"]=e;return l[r]?l[r][t]?void delete l[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" 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(r){const{TG_PRX:o,TG_SFX:a,TG_SIZE_P:i,TG_SIZE_S:s}=e;let c,l,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(c=r.indexOf(o),0<c&&f.push(r.slice(0,c)),-1==c)return f.push(r),f;{if(u=r.indexOf(o,c+i),l=r.indexOf(a),-1==l)return n("missingClosing");if(l<c)return n("closedBeforeOpened");if(l+=s,-1!=u&&u<l)return n("newBeforeClosed");f.push(r.slice(c,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 r(t,n){const r={};let o=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]};if(!n.includes("#"))return{dataDeepLevel:0,nestedData:[t]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:n}){let a=!1;return o=n.split("/").length-1,isNaN(e)||(a=!0),r[o]||(r[o]=a?[]:{}),"root"===e?r[o]=t:a?r[o].push(t):r[o][n]=t,t}}),{dataDeepLevel:o,nestedData:r}}function*o(e,t){let n=function(e={}){let t=[],{type:n,limit:r,onLimit:o}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),a="LIFO"===n.toUpperCase(),i=!1;function s(e=1,n=0){let r=[];return n>0&&Array.from({length:n}).map((()=>t.pop())),1==e?t.pop():(Array.from({length:e}).map((()=>{let e=t.pop();null!=e&&r.push(e)})),r)}function c(e=1,n=0){let r=[],o=t.length-n;return e>1&&Array.from({length:e}).map((()=>{null!=t[o-1]&&r.push(t[o-1]),o--})),1==e?t[t.length-1]:r}function l(){}return l.prototype={pull:s,pullReverse:function(e=1,t=0){let n=s(e,t);return n instanceof Array?n.reverse():n},peek:c,peekReverse:function(e=1,t=0){const n=c(e,t);return n instanceof Array?n.reverse():n},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},l.prototype.push=a?function(e){const n=e instanceof Array,a=n?e.length:1;let c=!1;if("full"!==o||!i){if(r&&n&&a>r&&(e=e.slice(0,-a+r)),r){const a=(n?e.length:1)+t.length;a>=r&&"full"===o&&(e=e.slice(0,-(a-r))),a>=r&&"update"===o&&(c=s(a-r))}return t=e instanceof Array?t.concat(e):t.concat([e]),i=!!r&&t.length===r,c||void 0}}:function(e){const n=e instanceof Array,a=n?e.length:1;let c=!1;if("full"!==o||!i){if(r&&n&&a>r&&(e=e.slice(a-r)),r){const a=(n?e.length:1)+t.length;a>=r&&"full"===o&&(e=e.slice(0,-(a-r))),a>=r&&"update"===o&&(c=s(a-r))}return t=n?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),i=!!r&&t.length===r,c||void 0}},new l}({type:"LIFO"});for(let r=0;r<=t;r++)n.push(e[r]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.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,n,r,...o){e instanceof Object&&Object.entries(e).forEach((([t,n])=>{n instanceof Object&&(e[t]=n.text)}));const i="function"==typeof r[n];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),i?r[n](e,...o):function(e,n){if(null==n)return null;const r=t(a)(e),o=a;return r.forEach(((e,t)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();n[a]&&(r[t]=n[a])}})),r.join("")}(r[n],e)}function c(n,c=!1,l={}){const{hasError:u,placeholders:f,chop:p,helpers:d,handshake:h}=function(e){const{template:n,helpers:r={},handshake:o}=e,{TG_PRX:i,TG_SFX:s,TG_SIZE_P:c,TG_SIZE_S:l}=a,u=[],f="string"==typeof n?n.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):n;let p=null;const d=t(a)(f);return"string"==typeof d?p=d:d.forEach(((e,t)=>{const n=RegExp(i+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(i)){const o=n.exec(e);u.push({index:t,data:(r=o[1],r||null),action:o[2]?o[2].split(",").map((e=>e.trim())):null})}var r})),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||!!r[e]||(p=`Error: Missing helper: ${e}`,!1))))})),{hasError:p,placeholders:u,chop:d,helpers:r,handshake:o}}(n);if(u){function y(){return u}return c?[!1,y]:y}{let m=structuredClone(p);function g(t={},n={},...a){const c=i(t),u=[];if(t=e({data:t}),"null"===c)return m.join("");if("string"==typeof t)switch(t){case"raw":return m.join("");case"demo":if(!h)return"Error: No handshake data.";t=h;break;case"handshake":return h?structuredClone(h):"Error: No handshake data.";case"placeholders":return f.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${t}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==c&&(t=[t]),t.forEach((t=>{f.forEach((a=>{const{index:c,data:u,action:f}=a;if(!f&&u){const p=t[u];switch(i(p)){case"function":return void(m[c]=p());case"primitive":return void(m[c]=p);case"array":return void("primitive"===i(p[0])&&(m[c]=p[0]));case"object":return void(p.text&&(m[c]=p.text))}}else{const{dataDeepLevel:h,nestedData:y}=r("@all"===u||null===u||"@root"===u?t:t[u],f),g=o(function(e,t=10){let n={},r=0,o=[...e],a=0;do{n[a]=[],a++}while(a<=t);return o.every((e=>"#"===e?(r++,!(r>t)):e.startsWith("?")?(n[r].push({type:"route",name:e.replace("?",""),level:r}),!0):e.startsWith("+")?(n[r].push({type:"extendedRender",name:e.replace("+",""),level:r}),!0):e.startsWith("[]")?(n[r].push({type:"mix",name:e.replace("[]",""),level:r}),!0):e.startsWith(">")?(n[r].push({type:"data",name:e.replace(">",""),level:r}),!0):(""===e||n[r].push({type:"render",name:e,level:r}),!0))),n}(f,h),h);for(let E of g){let{type:k,name:j,level:x}=E,w=y[x],T=i(w);switch(k){case"route":switch(T){case"array":w.forEach(((e,t)=>{if(null==e)return;const r=i(e),o=d[j](e);null!=o&&("object"===r?w[t].text=s(e,o,d,{...l,...n}):w[t]=s(e,o,d,{...l,...n}))}));break;case"object":w.text=s(w,j,d,{...l,...n});break;case"primitive":y[x]=s(w,j,d,{...l,...n})}break;case"data":switch(T){case"array":w.forEach(((e,t)=>w[t]=e instanceof Function?d[j](e()):d[j](e)));break;case"object":case"primitive":y[x]=d[j](w);break;case"function":y[x]=d[j](w())}break;case"render":const _="function"==typeof d[j];switch(T){case"array":_?w.forEach(((e,t)=>{if(null==e)return;const r=i(e),o=d[j](e,{...l,...n});null==o&&(w[t]=null),"object"===r?e.text=o:w[t]=o})):w.forEach(((e,t)=>{if(null==e)return;const r=i(e),o=s(e,j,d,{...l,...n});null==o&&(w[t]=null),"object"===r?e.text=o:w[t]=o}));break;case"function":y[x]=d[j](w(),{...l,...n});break;case"primitive":y[x]=s(w,j,d,{...l,...n});break;case"object":if(_)y[x].text=d[j](w,{...l,...n});else{Object.keys(w).find((e=>e.includes("/")))?Object.entries(w).forEach((([e,t])=>t.text=s(t,j,d,{...l,...n}))):w.text=s(w,j,d,{...l,...n})}}break;case"extendedRender":"function"==typeof d[j]&&(y[x]=d[j](y[0]));break;case"mix":if(""===j)switch(T){case"object":Object.keys(w).find((e=>e.includes("/")))?Object.entries(w).forEach((([e,t])=>y[x][e]=t.text)):y[x]=w.text;for(let O=x-1;O>=0;O--)y[O]=e({data:y[O],objectCallback:S});function S({value:e,breadcrumbs:t}){return y[x][t]?y[x][t]:e}break;case"array":y[x]=w.map((e=>"object"===i(e)?e.text:e)).filter((e=>null!=e)).join("")}else y[x]=d[j](w)}}let b=i(y[0]),v=y[0];switch(b){case"primitive":if(null==v)return;m[c]=v;break;case"object":if(null==v.text)return;m[c]=v.text;break;case"array":const G=i(v[0]);m[c]="object"===G?v.map((e=>e.text)).join(""):v.join("")}}})),u.push(m.join(""))})),"array"===c?u:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,{...l,...n})),u.join("")):u.join("")}return c?[!0,g]:g}}const l={default:{}};return{build:c,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,n="default"]=e;return l[n]?l[n][t]?l[n][t]:function(){return`Error: Template "${t}" does not exist in storage "${n}".`}:function(){return`Error: Storage "${n}" does not exist.`}},add:function(e,t,...n){const[r,o="default"]=e;let a=t,i=!0;if(l[o]||(l[o]={}),"function"!=typeof t){let e=c(t,!0,...n);i=e[0],a=e[1]}i?l[o][r]=a:console.error(`Error: Template "${r}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>l[e]?Object.keys(l[e]):[])).flat()},clear:function(){Object.keys(l).forEach((e=>{"default"!=e?delete l[e]:l.default={}}))},remove:function(e){const[t,n="default"]=e;return l[n]?l[n][t]?void delete l[n][t]:`Error: Template "${t}" does not exist in storage "${n}".`:`Error: Storage "${n}" 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(r){const{TG_PRX:o,TG_SFX:a,TG_SIZE_P:i,TG_SIZE_S:s}=e;let l,c,u,f=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(l=r.indexOf(o),0<l&&f.push(r.slice(0,l)),-1==l)return f.push(r),f;{if(u=r.indexOf(o,l+i),c=r.indexOf(a),-1==c)return n("missingClosing");if(c<l)return n("closedBeforeOpened");if(c+=s,-1!=u&&u<c)return n("newBeforeClosed");f.push(r.slice(l,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."}}function r(t,n){const r={};let o=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]};if(!n.includes("#"))return{dataDeepLevel:0,nestedData:[t]};return e({data:t,objectCallback:function({key:e,value:t,breadcrumbs:n}){let a=!1;return o=n.split("/").length-1,isNaN(e)||(a=!0),r[o]||(r[o]=a?[]:{}),"root"===e?r[o]=t:a?r[o].push(t):r[o][n]=t,t}}),{dataDeepLevel:o,nestedData:r}}function*o(e,t){let n=function(e={}){let t=[],{type:n,limit:r,onLimit:o}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),a="LIFO"===n.toUpperCase(),i=!1;function s(e=1,n=0){let r=[];return n>0&&Array.from({length:n}).map((()=>t.pop())),1==e?t.pop():(Array.from({length:e}).map((()=>{let e=t.pop();null!=e&&r.push(e)})),r)}function l(e=1,n=0){let r=[],o=t.length-n;return e>1&&Array.from({length:e}).map((()=>{null!=t[o-1]&&r.push(t[o-1]),o--})),1==e?t[t.length-1]:r}function c(){}return c.prototype={pull:s,pullReverse:function(e=1,t=0){let n=s(e,t);return n instanceof Array?n.reverse():n},peek:l,peekReverse:function(e=1,t=0){const n=l(e,t);return n instanceof Array?n.reverse():n},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=a?function(e){const n=e instanceof Array,a=n?e.length:1;let l=!1;if("full"!==o||!i){if(r&&n&&a>r&&(e=e.slice(0,-a+r)),r){const a=(n?e.length:1)+t.length;a>=r&&"full"===o&&(e=e.slice(0,-(a-r))),a>=r&&"update"===o&&(l=s(a-r))}return t=e instanceof Array?t.concat(e):t.concat([e]),i=!!r&&t.length===r,l||void 0}}:function(e){const n=e instanceof Array,a=n?e.length:1;let l=!1;if("full"!==o||!i){if(r&&n&&a>r&&(e=e.slice(a-r)),r){const a=(n?e.length:1)+t.length;a>=r&&"full"===o&&(e=e.slice(0,-(a-r))),a>=r&&"update"===o&&(l=s(a-r))}return t=n?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),i=!!r&&t.length===r,l||void 0}},new c}({type:"LIFO"});for(let r=0;r<=t;r++)n.push(e[r]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.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,n,r,...o){e instanceof Object&&Object.entries(e).forEach((([t,n])=>{n instanceof Object&&(e[t]=n.text)}));const i="function"==typeof r[n];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),i?r[n](e,...o):function(e,n){if(null==n)return null;const r=t(a)(e),o=a;return r.forEach(((e,t)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();n[a]&&(r[t]=n[a])}})),r.join("")}(r[n],e)}function l(n,l=!1,c={}){const{hasError:u,placeholders:f,chop:p,helpers:d,handshake:h}=function(e){const{template:n,helpers:r={},handshake:o}=e,{TG_PRX:i,TG_SFX:s,TG_SIZE_P:l,TG_SIZE_S:c}=a,u=[],f="string"==typeof n?n.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):n;let p=null;const d=t(a)(f);return"string"==typeof d?p=d:d.forEach(((e,t)=>{const n=RegExp(i+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(i)){const o=n.exec(e);u.push({index:t,data:(r=o[1],r||null),action:o[2]?o[2].split(",").map((e=>e.trim())):null})}var r})),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||!!r[e]||(p=`Error: Missing helper: ${e}`,!1))))})),{hasError:p,placeholders:u,chop:d,helpers:r,handshake:o}}(n);if(u){function m(){return u}return l?[!1,m]:m}{let y=structuredClone(p);function g(t={},n={},...a){const l=i(t),u=[];if(t=e({data:t}),"null"===l)return y.join("");if("string"==typeof t)switch(t){case"raw":return y.join("");case"demo":if(!h)return"Error: No handshake data.";t=h;break;case"handshake":return h?structuredClone(h):"Error: No handshake data.";case"placeholders":return f.map((e=>y[e.index])).join(", ");default:return`Error: Wrong command "${t}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==l&&(t=[t]),t.forEach((t=>{f.forEach((a=>{const{index:l,data:u,action:f}=a;if(!f&&u){const p=t[u];switch(i(p)){case"function":return void(y[l]=p());case"primitive":return void(y[l]=p);case"array":return void("primitive"===i(p[0])&&(y[l]=p[0]));case"object":return void(p.text&&(y[l]=p.text))}}else{const{dataDeepLevel:h,nestedData:m}=r("@all"===u||null===u||"@root"===u?t:t[u],f),g=o(function(e,t=10){let n={},r=0,o=[...e],a=0;do{n[a]=[],a++}while(a<=t);return o.every((e=>"#"===e?(r++,!(r>t)):e.startsWith("?")?(n[r].push({type:"route",name:e.replace("?",""),level:r}),!0):e.startsWith("+")?(n[r].push({type:"extendedRender",name:e.replace("+",""),level:r}),!0):e.startsWith("[]")?(n[r].push({type:"mix",name:e.replace("[]",""),level:r}),!0):e.startsWith(">")?(n[r].push({type:"data",name:e.replace(">",""),level:r}),!0):(""===e||n[r].push({type:"render",name:e,level:r}),!0))),n}(f,h),h);for(let E of g){let{type:k,name:j,level:x}=E,w=m[x],T=i(w);switch(k){case"route":switch(T){case"array":w.forEach(((e,t)=>{if(null==e)return;const r=i(e),o=d[j](e);null!=o&&("object"===r?w[t].text=s(e,o,d,{...c,...n}):w[t]=s(e,o,d,{...c,...n}))}));break;case"object":w.text=s(w,j,d,{...c,...n});break;case"primitive":m[x]=s(w,j,d,{...c,...n})}break;case"data":switch(T){case"array":w.forEach(((e,t)=>w[t]=e instanceof Function?d[j](e()):d[j](e)));break;case"object":case"primitive":m[x]=d[j](w);break;case"function":m[x]=d[j](w())}break;case"render":const _="function"==typeof d[j];switch(T){case"array":_?w.forEach(((e,t)=>{if(null==e)return;const r=i(e),o=d[j](e,{...c,...n});null==o&&(w[t]=null),"object"===r?e.text=o:w[t]=o})):w.forEach(((e,t)=>{if(null==e)return;const r=i(e),o=s(e,j,d,{...c,...n});null==o&&(w[t]=null),"object"===r?e.text=o:w[t]=o}));break;case"function":m[x]=d[j](w(),{...c,...n});break;case"primitive":m[x]=s(w,j,d,{...c,...n});break;case"object":if(_)m[x].text=d[j](w,{...c,...n});else{Object.keys(w).find((e=>e.includes("/")))?Object.entries(w).forEach((([e,t])=>t.text=s(t,j,d,{...c,...n}))):w.text=s(w,j,d,{...c,...n})}}break;case"extendedRender":"function"==typeof d[j]&&(m[x]=d[j](m[0]));break;case"mix":if(""===j)switch(T){case"object":Object.keys(w).find((e=>e.includes("/")))?Object.entries(w).forEach((([e,t])=>m[x][e]=t.text)):m[x]=w.text;for(let O=x-1;O>=0;O--)m[O]=e({data:m[O],objectCallback:S});function S({value:e,breadcrumbs:t}){return m[x][t]?m[x][t]:e}break;case"array":m[x]=w.map((e=>"object"===i(e)?e.text:e)).filter((e=>null!=e)).join("")}else m[x]=d[j](w)}}let b=i(m[0]),v=m[0];switch(b){case"primitive":if(null==v)return;y[l]=v;break;case"object":if(null==v.text)return;y[l]=v.text;break;case"array":const G=i(v[0]);y[l]="object"===G?v.map((e=>e.text)).join(""):v.join("")}}})),u.push(y.join(""))})),"array"===l?u:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,{...c,...n})),u.join("")):u.join("")}return l?[!0,g]:g}}const c={default:{}};return{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,n="default"]=e;return c[n]?c[n][t]?c[n][t]:function(){return`Error: Template "${t}" does not exist in storage "${n}".`}:function(){return`Error: Storage "${n}" does not exist.`}},add:function(e,t,...n){const[r,o="default"]=e;if(null==t)return void console.warn(`Warning: Template ${o}/${r} is not added to storage. The template is null.`);let a=t,i=!0;if(c[o]||(c[o]={}),"function"!=typeof t){let e=l(t,!0,...n);i=e[0],a=e[1]}i?c[o][r]=a:console.error(`Error: Template "${r}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>c[e]?Object.keys(c[e]):[])).flat()},clear:function(){Object.keys(c).forEach((e=>{"default"!=e?delete c[e]:c.default={}}))},remove:function(e){const[t,n="default"]=e;return c[n]?c[n][t]?void delete c[n][t]:`Error: Template "${t}" does not exist in storage "${n}".`:`Error: Storage "${n}" 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": "1.1.2",
4
+ "version": "1.1.3",
5
5
  "license": "MIT",
6
6
  "author": "Peter Naydenov",
7
7
  "main": ".src/main.js",
package/src/main.js CHANGED
@@ -72,15 +72,20 @@ function get ( location ) {
72
72
  * @param {string[]} location - The location to add the template to. Array of two elements.
73
73
  * The first element is the name of the template. The second element
74
74
  * is optional and is the name of the storage. Defaults to 'default'.
75
- * @param {object|function} tplfn - The template description or the already built template function.
75
+ * @param {object|function|null} tplfn - The template description or the already built template function.
76
76
  * @param {...any} args - Additional arguments to be passed to the build function.
77
77
  * Only used if the first argument is a template description.
78
78
  */
79
79
  function add ( location, tplfn, ...args ) {
80
80
  const [ name, strName='default'] = location
81
+ if ( tplfn == null ) {
82
+ console.warn ( `Warning: Template ${strName}/${name} is not added to storage. The template is null.` )
83
+ return
84
+ }
81
85
  let fn = tplfn;
82
86
  let successBuild = true;
83
87
  if( !storage[strName] ) storage[strName] = {}
88
+
84
89
  if ( typeof tplfn !== 'function' ) {
85
90
  let r = build ( tplfn, true, ...args )
86
91
  successBuild = r[0]