@peter.naydenov/morph 2.1.3 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Changelog.md +5 -0
- package/README.md +46 -2
- package/dist/morph.cjs +1 -1
- package/dist/morph.esm.mjs +1 -1
- package/dist/morph.umd.js +1 -1
- package/package.json +3 -3
- package/src/methods/build.js +9 -4
package/Changelog.md
CHANGED
package/README.md
CHANGED
|
@@ -103,14 +103,14 @@ Let's see a more complex example before we go into details:
|
|
|
103
103
|
const myTemplateDescription = {
|
|
104
104
|
template: `Hello, {{ person : a, >getReady }}! Your age is {{ person : >getAge}}.`
|
|
105
105
|
, helpers: {
|
|
106
|
-
getReady: (person) => {
|
|
106
|
+
getReady: ({data:person}) => {
|
|
107
107
|
return {
|
|
108
108
|
text: person.name
|
|
109
109
|
, href: person.web
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
, a: `<a href="{{href}}">{{text}}</a>`
|
|
113
|
-
, getAge: (
|
|
113
|
+
, getAge: ({data}) => data.age
|
|
114
114
|
}
|
|
115
115
|
, handshake: {
|
|
116
116
|
// ... demo data here
|
|
@@ -205,6 +205,9 @@ Template placeholders can contain data-source and actions separated by ':'. Data
|
|
|
205
205
|
`{{ :someAction}}` // no data, but the result of the action will fill the placeholder
|
|
206
206
|
`{{ @all : someAction }}` // provide all the data to the action 'someAction'
|
|
207
207
|
```
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
208
211
|
### Deep data-sources ( after version 2.1.0 )
|
|
209
212
|
|
|
210
213
|
Setup a deep data-source by using breadcrumbs.
|
|
@@ -278,6 +281,47 @@ Helpers are templates and functions that are used by actions to decorate the dat
|
|
|
278
281
|
|
|
279
282
|
## Good Practices and Examples
|
|
280
283
|
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
### Setup a development environment
|
|
287
|
+
Setting up your development environment for working with the Morph components will help you to get the most out of it. First, what you need to do is to start showing HTML markup inside JavaScript. For editor 'Video Studio Code', you can use an extension [es6-string-html](https://marketplace.visualstudio.com/items?itemName=Tobermory.es6-string-html). Now simple comment will turn your HTML string into colored HTML markup.
|
|
288
|
+
|
|
289
|
+
Second - make sure that `emmet` works for your javascript files. For editor 'Video Studio Code', go to settings, write 'emmet' in search box. Find a section 'Emmet: Include Languages' and press 'Add item'. Set item 'javascript' and value 'html'. Save settings. Now you can write emmet snippets to speed up your markup writing.
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
### VS Code Snippet for Morph Components
|
|
293
|
+
To speed up the creation of Morph components in Visual Studio Code, you can use the following snippet. Add it to your VS Code snippets configuration:
|
|
294
|
+
|
|
295
|
+
```json
|
|
296
|
+
"morph":{
|
|
297
|
+
"prefix": "mc",
|
|
298
|
+
"body": [
|
|
299
|
+
"const ${1:componentName} = {",
|
|
300
|
+
" template: /*template*/`",
|
|
301
|
+
" $2",
|
|
302
|
+
" `",
|
|
303
|
+
", helpers: {}",
|
|
304
|
+
", handshake: {}",
|
|
305
|
+
"}",
|
|
306
|
+
],
|
|
307
|
+
"description": "Morphs component snippet"
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
### Use the handshake property
|
|
314
|
+
Handshake is a non-required property inside Morph components but is a good practice to have it. Handshake shows how the component looks as the data object. Programmersn who work with Morph components don't have to understand the template rules, they just can look at the handshake object. It's their data-model.
|
|
315
|
+
|
|
316
|
+
The Handshake property acts as a placeholder for dummy data, allowing you to visualize the appearance of the component. It is particularly useful during the design phase of a project.
|
|
317
|
+
```js
|
|
318
|
+
const codeSnippet = morph.get(['templateThatHaveHandshake'])('demo')
|
|
319
|
+
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
281
325
|
### Modify root data before start rendering
|
|
282
326
|
Sometimes we need to modify data and modification should be valid for all placeholders. Add in the begining of the template a placeholder like `{{ : blank, ^^, >myModification }}`, where myModification is a helper function that will modify the data, `^^` is overwrite action and `blank` is a render helper function that will return an empty string (placeholder disapears ). Look at the example here:
|
|
283
327
|
|
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 l,i,u,d=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(l=r.indexOf(a),0<l&&d.push(r.slice(0,l)),-1==l)return d.push(r),d;{if(u=r.indexOf(a,l+s),i=r.indexOf(o),-1==i)return n("missingClosing");if(i<l)return n("closedBeforeOpened");if(i+=c,-1!=u&&u<i)return n("newBeforeClosed");d.push(r.slice(l,i));let e=t(r.slice(i));return d.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...o):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function c(n,c=!1,l={}){const{hasError:i,placeholders:u,chop:d,helpers:f,handshake:p}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:c}=a,l=[],i="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let u=null;const d=r(a)(i);return"string"==typeof d?u=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+c,"g");if(e.includes(s)){const a=r.exec(e);l.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),l.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(u=`Error: Missing helper: ${e}`,!1))))})),{hasError:u,placeholders:l,chop:d,helpers:n,handshake:o}}(n);if(i){function h(){return i}return c?[!1,h]:h}{let b=structuredClone(d);function m(r={},n={},...a){const c=[],i={};let d=o(r),h={...l,...n};if(r=e({data:r}),"null"===d)return b.join("");if("string"==typeof r)switch(r){case"raw":return b.join("");case"demo":if(!p)return"Error: No handshake data.";d=o(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"!==d&&(r=[r]),"null"===d?b.join(""):(r.forEach((r=>{u.forEach((n=>{const{index:a,data:c,action:l}=n,u=!l&&c,d=structuredClone(i),p={dependencies:h,memory:d};let m=r;if(c&&c.includes("/")?c.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===c||null===c||"@root"===c?m=r:c&&(m=m[c]),u){switch(o(m)){case"function":return void(b[a]=m());case"primitive":return void(b[a]=m);case"array":return void("primitive"===o(m[0])&&(b[a]=m[0]));case"object":return void(m.text&&(b[a]=m.text))}}else{let{dataDeepLevel:n,nestedData:c}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,l),u=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(l,n),n);for(let t of u){let{type:n,name:a,level:l}=t;(c[l]||[]).forEach(((t,u)=>{let d=o(t);switch(n){case"route":switch(d){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),c=f[a]({data:e,...p});null!=c&&("object"===n?t[r].text=s(e,c,f,h):t[r]=s(e,c,f,h))}));break;case"object":t.text=s(t,a,f,h)}break;case"save":i[a]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(d){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?f[a]({data:e(),...p}):f[a]({data:e,...p})));break;case"object":c[l]=[f[a]({data:t,...p})];break;case"function":c[l]=[f[a]({data:t(),...p})];break;case"primitive":c[l]=f[a]({data:t,...p})}break;case"render":const b="function"==typeof f[a];switch(d){case"array":b?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),s=f[a]({data:e,...p});null==s&&(t[r]=null),"object"===n?e.text=s:t[r]=s})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),c=s(e,a,f,h);null==c?t[r]=null:"object"===n?e.text=c:t[r]=c}));break;case"function":c[l]=f[a]({data:t(),...p});break;case"primitive":c[l]=b?f[a]({data:t,...p}):s(t,a,f,h);break;case"object":b?c[l][u].text=f[a]({data:t,...p}):t.text=s(t,a,f,h)}break;case"extendedRender":"function"==typeof f[a]&&c[0].forEach(((e,t)=>c[0][t]=f[a]({data:e,...p})));break;case"mix":if(""===a)switch(d){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>c[l][e]=t.text)):c[l]=t.text;for(let y=l-1;y>=0;y--)c[y]=e({data:c[y],objectCallback:m});function m({value:e,breadcrumbs:t}){return c[l][t]?c[l][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let v=f[a]({data:t,...p}),g=o(v);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,g){case"primitive":t[0]=v;break;case"array":t.push(...v)}}}}))}if(c instanceof Array&&1===c.length&&c[0]instanceof Array&&(c=c[0]),null==c[0])return;let d=o(c[0]),y=c[0];switch(d){case"primitive":if(null==y)return;b[a]=y;break;case"object":if(null==y.text)return;b[a]=y.text;break;case"array":const e=o(y[0]);b[a]="object"===e?y.map((e=>e.text)).join(""):y.join("")}}})),c.push(b.join(""))})),"array"===d?c:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,h)),c.join("")):c.join(""))}return c?[!0,m]:m}}const l={default:{}};const i={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=i;
|
|
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 l,i,u,d=[];if("string"!=typeof r)return n("notAString");if(0==r.length)return[];if(l=r.indexOf(a),0<l&&d.push(r.slice(0,l)),-1==l)return d.push(r),d;{if(u=r.indexOf(a,l+s),i=r.indexOf(o),-1==i)return n("missingClosing");if(i<l)return n("closedBeforeOpened");if(i+=c,-1!=u&&u<i)return n("newBeforeClosed");d.push(r.slice(l,i));let e=t(r.slice(i));return d.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...o):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function c(n,c=!1,l={}){const{hasError:i,placeholders:u,chop:d,helpers:f,handshake:p}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:c}=a,l=[],i="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let u=null;const d=r(a)(i);return"string"==typeof d?u=d:d.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+c,"g");if(e.includes(s)){const a=r.exec(e);l.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),l.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(u=`Error: Missing helper: ${e}`,!1))))})),{hasError:u,placeholders:l,chop:d,helpers:n,handshake:o}}(n);if(i){function h(){return i}return c?[!1,h]:h}{let b=structuredClone(d);function m(r={},n={},...a){const c=[],i={};let d=o(r),h={...l,...n};if(r=e({data:r}),"null"===d)return b.join("");if("string"==typeof r)switch(r){case"raw":return b.join("");case"demo":if(!p)return"Error: No handshake data.";d=o(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"!==d&&(r=[r]),"null"===d?b.join(""):(r.forEach((r=>{u.forEach((n=>{const{index:a,data:c,action:l}=n,u=!l&&c,d=structuredClone(i),p={dependencies:h,memory:d};let m=r;if(c&&c.includes("/")?m.hasOwnProperty(c)?m=m[c]:c.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===c||null===c||"@root"===c?m=r:c&&(m=m[c]),u){switch(o(m)){case"function":return void(b[a]=m());case"primitive":return void(b[a]=m);case"array":return void("primitive"===o(m[0])&&(b[a]=m[0]));case"object":return void(m.text&&(b[a]=m.text))}}else{let{dataDeepLevel:n,nestedData:c}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,l),u=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(l,n),n);for(let t of u){let{type:n,name:a,level:l}=t;(c[l]||[]).forEach(((t,u)=>{let d=o(t);switch(n){case"route":switch(d){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),c=f[a]({data:e,...p});null!=c&&("object"===n?t[r].text=s(e,c,f,h):t[r]=s(e,c,f,h))}));break;case"object":t.text=s(t,a,f,h)}break;case"save":i[a]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(d){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?f[a]({data:e(),...p}):f[a]({data:e,...p})));break;case"object":c[l]=[f[a]({data:t,...p})];break;case"function":c[l]=[f[a]({data:t(),...p})];break;case"primitive":c[l]=f[a]({data:t,...p})}break;case"render":const b="function"==typeof f[a];switch(d){case"array":b?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),s=f[a]({data:e,...p});null==s&&(t[r]=null),"object"===n?e.text=s:t[r]=s})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),c=s(e,a,f,h);null==c?t[r]=null:"object"===n?e.text=c:t[r]=c}));break;case"function":c[l]=f[a]({data:t(),...p});break;case"primitive":c[l]=b?f[a]({data:t,...p}):s(t,a,f,h);break;case"object":b?c[l][u].text=f[a]({data:t,...p}):t.text=s(t,a,f,h)}break;case"extendedRender":"function"==typeof f[a]&&c[0].forEach(((e,t)=>c[0][t]=f[a]({data:e,...p})));break;case"mix":if(""===a)switch(d){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>c[l][e]=t.text)):c[l]=t.text;for(let y=l-1;y>=0;y--)c[y]=e({data:c[y],objectCallback:m});function m({value:e,breadcrumbs:t}){return c[l][t]?c[l][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let v=f[a]({data:t,...p}),g=o(v);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,g){case"primitive":t[0]=v;break;case"array":t.push(...v)}}}}))}if(c instanceof Array&&1===c.length&&c[0]instanceof Array&&(c=c[0]),null==c[0])return;let d=o(c[0]),y=c[0];switch(d){case"primitive":if(null==y)return;b[a]=y;break;case"object":if(null==y.text)return;b[a]=y.text;break;case"array":const e=o(y[0]);b[a]="object"===e?y.map((e=>e.text)).join(""):y.join("")}}})),c.push(b.join(""))})),"array"===d?c:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,h)),c.join("")):c.join(""))}return c?[!0,m]:m}}const l={default:{}};const i={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=i;
|
package/dist/morph.esm.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"@peter.naydenov/walk";import t from"@peter.naydenov/stack";function r(e){return function t(r){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:s,TG_SIZE_S:l}=e;let c,i,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),i=r.indexOf(o),-1==i)return n("missingClosing");if(i<c)return n("closedBeforeOpened");if(i+=l,-1!=u&&u<i)return n("newBeforeClosed");f.push(r.slice(c,i));let e=t(r.slice(i));return f.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...o):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function l(n,l=!1,c={}){const{hasError:i,placeholders:u,chop:f,helpers:d,handshake:p}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:l}=a,c=[],i="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let u=null;const f=r(a)(i);return"string"==typeof f?u=f:f.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(s)){const a=r.exec(e);c.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),c.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(u=`Error: Missing helper: ${e}`,!1))))})),{hasError:u,placeholders:c,chop:f,helpers:n,handshake:o}}(n);if(i){function h(){return i}return l?[!1,h]:h}{let m=structuredClone(f);function b(r={},n={},...a){const l=[],i={};let f=o(r),h={...c,...n};if(r=e({data:r}),"null"===f)return m.join("");if("string"==typeof r)switch(r){case"raw":return m.join("");case"demo":if(!p)return"Error: No handshake data.";f=o(r=p);break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==f&&(r=[r]),"null"===f?m.join(""):(r.forEach((r=>{u.forEach((n=>{const{index:a,data:l,action:c}=n,u=!c&&l,f=structuredClone(i),p={dependencies:h,memory:f};let b=r;if(l&&l.includes("/")?l.split("/").forEach((e=>{b=b.hasOwnProperty(e)?b[e]:[]})):"@all"===l||null===l||"@root"===l?b=r:l&&(b=b[l]),u){switch(o(b)){case"function":return void(m[a]=b());case"primitive":return void(m[a]=b);case"array":return void("primitive"===o(b[0])&&(m[a]=b[0]));case"object":return void(b.text&&(m[a]=b.text))}}else{let{dataDeepLevel:n,nestedData:l}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(b,c),u=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(c,n),n);for(let t of u){let{type:n,name:a,level:c}=t;(l[c]||[]).forEach(((t,u)=>{let f=o(t);switch(n){case"route":switch(f){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),l=d[a]({data:e,...p});null!=l&&("object"===n?t[r].text=s(e,l,d,h):t[r]=s(e,l,d,h))}));break;case"object":t.text=s(t,a,d,h)}break;case"save":i[a]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(f){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?d[a]({data:e(),...p}):d[a]({data:e,...p})));break;case"object":l[c]=[d[a]({data:t,...p})];break;case"function":l[c]=[d[a]({data:t(),...p})];break;case"primitive":l[c]=d[a]({data:t,...p})}break;case"render":const m="function"==typeof d[a];switch(f){case"array":m?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),s=d[a]({data:e,...p});null==s&&(t[r]=null),"object"===n?e.text=s:t[r]=s})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),l=s(e,a,d,h);null==l?t[r]=null:"object"===n?e.text=l:t[r]=l}));break;case"function":l[c]=d[a]({data:t(),...p});break;case"primitive":l[c]=m?d[a]({data:t,...p}):s(t,a,d,h);break;case"object":m?l[c][u].text=d[a]({data:t,...p}):t.text=s(t,a,d,h)}break;case"extendedRender":"function"==typeof d[a]&&l[0].forEach(((e,t)=>l[0][t]=d[a]({data:e,...p})));break;case"mix":if(""===a)switch(f){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>l[c][e]=t.text)):l[c]=t.text;for(let y=c-1;y>=0;y--)l[y]=e({data:l[y],objectCallback:b});function b({value:e,breadcrumbs:t}){return l[c][t]?l[c][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let v=d[a]({data:t,...p}),g=o(v);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,g){case"primitive":t[0]=v;break;case"array":t.push(...v)}}}}))}if(l instanceof Array&&1===l.length&&l[0]instanceof Array&&(l=l[0]),null==l[0])return;let f=o(l[0]),y=l[0];switch(f){case"primitive":if(null==y)return;m[a]=y;break;case"object":if(null==y.text)return;m[a]=y.text;break;case"array":const e=o(y[0]);m[a]="object"===e?y.map((e=>e.text)).join(""):y.join("")}}})),l.push(m.join(""))})),"array"===f?l:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,h)),l.join("")):l.join(""))}return l?[!0,b]:b}}const c={default:{}};const i={build:l,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return c[r]?c[r][t]?c[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(c[a]||(c[a]={}),"function"!=typeof t){let e=l(t,!0,...r);s=e[0],o=e[1]}s?c[a][n]=o:console.error(`Error: Template "${n}" 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,r="default"]=e;return c[r]?c[r][t]?void delete c[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}};export{i as default};
|
|
1
|
+
import e from"@peter.naydenov/walk";import t from"@peter.naydenov/stack";function r(e){return function t(r){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:s,TG_SIZE_S:l}=e;let c,i,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),i=r.indexOf(o),-1==i)return n("missingClosing");if(i<c)return n("closedBeforeOpened");if(i+=l,-1!=u&&u<i)return n("newBeforeClosed");f.push(r.slice(c,i));let e=t(r.slice(i));return f.concat(e)}}}function n(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function s(e,t,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const s="function"==typeof n[t];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),s?n[t](e,...o):function(e,t){if(null==t)return null;const n=r(a)(e),o=a;return n.forEach(((e,r)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();t.hasOwnProperty(a)&&null!=t[a]&&(n[r]=t[a])}})),n.join("")}(n[t],e)}function l(n,l=!1,c={}){const{hasError:i,placeholders:u,chop:f,helpers:d,handshake:p}=function(e){const{template:t,helpers:n={},handshake:o}=e,{TG_PRX:s,TG_SFX:l}=a,c=[],i="string"==typeof t?t.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):t;let u=null;const f=r(a)(i);return"string"==typeof f?u=f:f.forEach(((e,t)=>{const r=RegExp(s+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+l,"g");if(e.includes(s)){const a=r.exec(e);c.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),c.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(u=`Error: Missing helper: ${e}`,!1))))})),{hasError:u,placeholders:c,chop:f,helpers:n,handshake:o}}(n);if(i){function h(){return i}return l?[!1,h]:h}{let m=structuredClone(f);function b(r={},n={},...a){const l=[],i={};let f=o(r),h={...c,...n};if(r=e({data:r}),"null"===f)return m.join("");if("string"==typeof r)switch(r){case"raw":return m.join("");case"demo":if(!p)return"Error: No handshake data.";f=o(r=p);break;case"handshake":return p?structuredClone(p):"Error: No handshake data.";case"placeholders":return u.map((e=>m[e.index])).join(", ");default:return`Error: Wrong command "${r}". Available commands: raw, demo, handshake, placeholders.`}return"array"!==f&&(r=[r]),"null"===f?m.join(""):(r.forEach((r=>{u.forEach((n=>{const{index:a,data:l,action:c}=n,u=!c&&l,f=structuredClone(i),p={dependencies:h,memory:f};let b=r;if(l&&l.includes("/")?b.hasOwnProperty(l)?b=b[l]:l.split("/").forEach((e=>{b=b.hasOwnProperty(e)?b[e]:[]})):"@all"===l||null===l||"@root"===l?b=r:l&&(b=b[l]),u){switch(o(b)){case"function":return void(m[a]=b());case"primitive":return void(m[a]=b);case"array":return void("primitive"===o(b[0])&&(m[a]=b[0]));case"object":return void(b.text&&(m[a]=b.text))}}else{let{dataDeepLevel:n,nestedData:l}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(b,c),u=function*(e,r){let n=t({type:"LIFO"});for(let t=0;t<=r;t++)n.push(e[t]);for(;n&&!n.isEmpty();){let e=yield n.pull();e&&n.push(e)}}(function(e,t=10){let r={},n=[...e],a=0,o=0,s=0;n.forEach((e=>{"#"===e&&s++})),s<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${s} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(c,n),n);for(let t of u){let{type:n,name:a,level:c}=t;(l[c]||[]).forEach(((t,u)=>{let f=o(t);switch(n){case"route":switch(f){case"array":t.forEach(((e,r)=>{if(null==e)return;const n=o(e),l=d[a]({data:e,...p});null!=l&&("object"===n?t[r].text=s(e,l,d,h):t[r]=s(e,l,d,h))}));break;case"object":t.text=s(t,a,d,h)}break;case"save":i[a]=structuredClone(t);break;case"overwrite":r=structuredClone(t);break;case"data":switch(f){case"array":t.forEach(((e,r)=>t[r]=e instanceof Function?d[a]({data:e(),...p}):d[a]({data:e,...p})));break;case"object":l[c]=[d[a]({data:t,...p})];break;case"function":l[c]=[d[a]({data:t(),...p})];break;case"primitive":l[c]=d[a]({data:t,...p})}break;case"render":const m="function"==typeof d[a];switch(f){case"array":m?t.forEach(((e,r)=>{if(null==e)return;const n=o(e),s=d[a]({data:e,...p});null==s&&(t[r]=null),"object"===n?e.text=s:t[r]=s})):t.forEach(((e,r)=>{if(null==e)return;const n=o(e),l=s(e,a,d,h);null==l?t[r]=null:"object"===n?e.text=l:t[r]=l}));break;case"function":l[c]=d[a]({data:t(),...p});break;case"primitive":l[c]=m?d[a]({data:t,...p}):s(t,a,d,h);break;case"object":m?l[c][u].text=d[a]({data:t,...p}):t.text=s(t,a,d,h)}break;case"extendedRender":"function"==typeof d[a]&&l[0].forEach(((e,t)=>l[0][t]=d[a]({data:e,...p})));break;case"mix":if(""===a)switch(f){case"object":Object.keys(t).find((e=>e.includes("/")))?Object.entries(t).forEach((([e,t])=>l[c][e]=t.text)):l[c]=t.text;for(let y=c-1;y>=0;y--)l[y]=e({data:l[y],objectCallback:b});function b({value:e,breadcrumbs:t}){return l[c][t]?l[c][t]:e}break;case"array":t.forEach(((e,r)=>{if(r>0){let n=o(e);if(null==e)return;t[0]+="object"===n?`${e.text}`:`${e}`,t.toSpliced(r,1)}else{let r=o(e);if(t[0]="",null===r)return;t[0]="object"===r?`${e.text}`:`${e}`}})),t.length=1}else{let v=d[a]({data:t,...p}),g=o(v);switch(t.forEach(((e,r)=>t.splice(r,1))),t.length=0,g){case"primitive":t[0]=v;break;case"array":t.push(...v)}}}}))}if(l instanceof Array&&1===l.length&&l[0]instanceof Array&&(l=l[0]),null==l[0])return;let f=o(l[0]),y=l[0];switch(f){case"primitive":if(null==y)return;m[a]=y;break;case"object":if(null==y.text)return;m[a]=y.text;break;case"array":const e=o(y[0]);m[a]="object"===e?y.map((e=>e.text)).join(""):y.join("")}}})),l.push(m.join(""))})),"array"===f?l:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,h)),l.join("")):l.join(""))}return l?[!0,b]:b}}const c={default:{}};const i={build:l,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return c[r]?c[r][t]?c[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(c[a]||(c[a]={}),"function"!=typeof t){let e=l(t,!0,...r);s=e[0],o=e[1]}s?c[a][n]=o:console.error(`Error: Template "${n}" 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,r="default"]=e;return c[r]?c[r][t]?void delete c[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}};export{i as default};
|
package/dist/morph.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@peter.naydenov/walk")):"function"==typeof define&&define.amd?define(["@peter.naydenov/walk"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).morph=t(e.walk)}(this,(function(e){"use strict";function t(e){return function t(n){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:l,TG_SIZE_S:s}=e;let i,c,u,f=[];if("string"!=typeof n)return r("notAString");if(0==n.length)return[];if(i=n.indexOf(a),0<i&&f.push(n.slice(0,i)),-1==i)return f.push(n),f;{if(u=n.indexOf(a,i+l),c=n.indexOf(o),-1==c)return r("missingClosing");if(c<i)return r("closedBeforeOpened");if(c+=s,-1!=u&&u<c)return r("newBeforeClosed");f.push(n.slice(i,c));let e=t(n.slice(c));return f.concat(e)}}}function r(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}function*n(e,t){let r=function(e={}){let t=[],{type:r,limit:n,onLimit:a}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),o="LIFO"===r.toUpperCase(),l=!1;function s(e=1,r=0){let n=[];return r>0&&Array.from({length:r}).map((()=>t.pop())),1==e?t.pop():(Array.from({length:e}).map((()=>{let e=t.pop();null!=e&&n.push(e)})),n)}function i(e=1,r=0){let n=[],a=t.length-r;return e>1&&Array.from({length:e}).map((()=>{null!=t[a-1]&&n.push(t[a-1]),a--})),1==e?t[t.length-1]:n}function c(){}return c.prototype={pull:s,pullReverse:function(e=1,t=0){let r=s(e,t);return r instanceof Array?r.reverse():r},peek:i,peekReverse:function(e=1,t=0){const r=i(e,t);return r instanceof Array?r.reverse():r},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=o?function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(0,-o+n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=e instanceof Array?t.concat(e):t.concat([e]),l=!!n&&t.length===n,i||void 0}}:function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(o-n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=r?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),l=!!n&&t.length===n,i||void 0}},new c}({type:"LIFO"});for(let n=0;n<=t;n++)r.push(e[n]);for(;r&&!r.isEmpty();){let e=yield r.pull();e&&r.push(e)}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function l(e,r,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const l="function"==typeof n[r];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),l?n[r](e,...o):function(e,r){if(null==r)return null;const n=t(a)(e),o=a;return n.forEach(((e,t)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();r.hasOwnProperty(a)&&null!=r[a]&&(n[t]=r[a])}})),n.join("")}(n[r],e)}function s(r,s=!1,i={}){const{hasError:c,placeholders:u,chop:f,helpers:d,handshake:p}=function(e){const{template:r,helpers:n={},handshake:o}=e,{TG_PRX:l,TG_SFX:s}=a,i=[],c="string"==typeof r?r.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):r;let u=null;const f=t(a)(c);return"string"==typeof f?u=f:f.forEach(((e,t)=>{const r=RegExp(l+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(l)){const a=r.exec(e);i.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(u=`Error: Missing helper: ${e}`,!1))))})),{hasError:u,placeholders:i,chop:f,helpers:n,handshake:o}}(r);if(c){function h(){return c}return s?[!1,h]:h}{let y=structuredClone(f);function m(t={},r={},...a){const s=[],c={};let f=o(t),h={...i,...r};if(t=e({data:t}),"null"===f)return y.join("");if("string"==typeof t)switch(t){case"raw":return y.join("");case"demo":if(!p)return"Error: No handshake data.";f=o(t=p);break;case"handshake":return p?structuredClone(p):"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"!==f&&(t=[t]),"null"===f?y.join(""):(t.forEach((t=>{u.forEach((r=>{const{index:a,data:s,action:i}=r,u=!i&&s,f=structuredClone(c),p={dependencies:h,memory:f};let m=t;if(s&&s.includes("/")?s.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===s||null===s||"@root"===s?m=t:s&&(m=m[s]),u){switch(o(m)){case"function":return void(y[a]=m());case"primitive":return void(y[a]=m);case"array":return void("primitive"===o(m[0])&&(y[a]=m[0]));case"object":return void(m.text&&(y[a]=m.text))}}else{let{dataDeepLevel:r,nestedData:s}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,i),u=n(function(e,t=10){let r={},n=[...e],a=0,o=0,l=0;n.forEach((e=>{"#"===e&&l++})),l<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${l} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(i,r),r);for(let r of u){let{type:n,name:a,level:i}=r;(s[i]||[]).forEach(((r,u)=>{let f=o(r);switch(n){case"route":switch(f){case"array":r.forEach(((e,t)=>{if(null==e)return;const n=o(e),s=d[a]({data:e,...p});null!=s&&("object"===n?r[t].text=l(e,s,d,h):r[t]=l(e,s,d,h))}));break;case"object":r.text=l(r,a,d,h)}break;case"save":c[a]=structuredClone(r);break;case"overwrite":t=structuredClone(r);break;case"data":switch(f){case"array":r.forEach(((e,t)=>r[t]=e instanceof Function?d[a]({data:e(),...p}):d[a]({data:e,...p})));break;case"object":s[i]=[d[a]({data:r,...p})];break;case"function":s[i]=[d[a]({data:r(),...p})];break;case"primitive":s[i]=d[a]({data:r,...p})}break;case"render":const y="function"==typeof d[a];switch(f){case"array":y?r.forEach(((e,t)=>{if(null==e)return;const n=o(e),l=d[a]({data:e,...p});null==l&&(r[t]=null),"object"===n?e.text=l:r[t]=l})):r.forEach(((e,t)=>{if(null==e)return;const n=o(e),s=l(e,a,d,h);null==s?r[t]=null:"object"===n?e.text=s:r[t]=s}));break;case"function":s[i]=d[a]({data:r(),...p});break;case"primitive":s[i]=y?d[a]({data:r,...p}):l(r,a,d,h);break;case"object":y?s[i][u].text=d[a]({data:r,...p}):r.text=l(r,a,d,h)}break;case"extendedRender":"function"==typeof d[a]&&s[0].forEach(((e,t)=>s[0][t]=d[a]({data:e,...p})));break;case"mix":if(""===a)switch(f){case"object":Object.keys(r).find((e=>e.includes("/")))?Object.entries(r).forEach((([e,t])=>s[i][e]=t.text)):s[i]=r.text;for(let g=i-1;g>=0;g--)s[g]=e({data:s[g],objectCallback:m});function m({value:e,breadcrumbs:t}){return s[i][t]?s[i][t]:e}break;case"array":r.forEach(((e,t)=>{if(t>0){let n=o(e);if(null==e)return;r[0]+="object"===n?`${e.text}`:`${e}`,r.toSpliced(t,1)}else{let t=o(e);if(r[0]="",null===t)return;r[0]="object"===t?`${e.text}`:`${e}`}})),r.length=1}else{let b=d[a]({data:r,...p}),v=o(b);switch(r.forEach(((e,t)=>r.splice(t,1))),r.length=0,v){case"primitive":r[0]=b;break;case"array":r.push(...b)}}}}))}if(s instanceof Array&&1===s.length&&s[0]instanceof Array&&(s=s[0]),null==s[0])return;let f=o(s[0]),g=s[0];switch(f){case"primitive":if(null==g)return;y[a]=g;break;case"object":if(null==g.text)return;y[a]=g.text;break;case"array":const e=o(g[0]);y[a]="object"===e?g.map((e=>e.text)).join(""):g.join("")}}})),s.push(y.join(""))})),"array"===f?s:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,h)),s.join("")):s.join(""))}return s?[!0,m]:m}}const i={default:{}};return{build:s,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,l=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=s(t,!0,...r);l=e[0],o=e[1]}l?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}}}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@peter.naydenov/walk")):"function"==typeof define&&define.amd?define(["@peter.naydenov/walk"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).morph=t(e.walk)}(this,(function(e){"use strict";function t(e){return function t(n){const{TG_PRX:a,TG_SFX:o,TG_SIZE_P:l,TG_SIZE_S:s}=e;let i,c,u,f=[];if("string"!=typeof n)return r("notAString");if(0==n.length)return[];if(i=n.indexOf(a),0<i&&f.push(n.slice(0,i)),-1==i)return f.push(n),f;{if(u=n.indexOf(a,i+l),c=n.indexOf(o),-1==c)return r("missingClosing");if(c<i)return r("closedBeforeOpened");if(c+=s,-1!=u&&u<c)return r("newBeforeClosed");f.push(n.slice(i,c));let e=t(n.slice(c));return f.concat(e)}}}function r(e){switch(e){case"notAString":return"Error: Template is not a string.";case"missingClosing":return"Error: Placeholder with missing closing tag.";case"closedBeforeOpened":return"Error: Placeholder closing tag without starting one.";case"newBeforeClosed":return"Error: Nested placeholders. Close placeholder before open new one.";default:return"Error: Unknown template error."}}function*n(e,t){let r=function(e={}){let t=[],{type:r,limit:n,onLimit:a}=Object.assign({},{type:"FIFO",limit:!1,onLimit:"update"},e),o="LIFO"===r.toUpperCase(),l=!1;function s(e=1,r=0){let n=[];return r>0&&Array.from({length:r}).map((()=>t.pop())),1==e?t.pop():(Array.from({length:e}).map((()=>{let e=t.pop();null!=e&&n.push(e)})),n)}function i(e=1,r=0){let n=[],a=t.length-r;return e>1&&Array.from({length:e}).map((()=>{null!=t[a-1]&&n.push(t[a-1]),a--})),1==e?t[t.length-1]:n}function c(){}return c.prototype={pull:s,pullReverse:function(e=1,t=0){let r=s(e,t);return r instanceof Array?r.reverse():r},peek:i,peekReverse:function(e=1,t=0){const r=i(e,t);return r instanceof Array?r.reverse():r},getSize:()=>t.length,isEmpty:()=>0==t.length,reset:()=>(t=[],!0),debug:()=>[...t]},c.prototype.push=o?function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(0,-o+n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=e instanceof Array?t.concat(e):t.concat([e]),l=!!n&&t.length===n,i||void 0}}:function(e){const r=e instanceof Array,o=r?e.length:1;let i=!1;if("full"!==a||!l){if(n&&r&&o>n&&(e=e.slice(o-n)),n){const o=(r?e.length:1)+t.length;o>=n&&"full"===a&&(e=e.slice(0,-(o-n))),o>=n&&"update"===a&&(i=s(o-n))}return t=r?e.reduce(((e,t)=>[t,...e]),t):[e].reduce(((e,t)=>[t,...e]),t),l=!!n&&t.length===n,i||void 0}},new c}({type:"LIFO"});for(let n=0;n<=t;n++)r.push(e[n]);for(;r&&!r.isEmpty();){let e=yield r.pull();e&&r.push(e)}}var a={TG_PRX:"{{",TG_SFX:"}}",TG_SIZE_P:2,TG_SIZE_S:2};function o(e){return null==e?"null":"string"==typeof e||"number"==typeof e||"boolean"==typeof e?"primitive":"function"==typeof e?"function":e instanceof Array?"array":e instanceof Object?"object":void 0}function l(e,r,n,...o){e instanceof Object&&Object.entries(e).forEach((([t,r])=>{r instanceof Object&&(e[t]=r.text),r instanceof Array&&(e[t]=r[0])}));const l="function"==typeof n[r];return e=function(e={}){return"string"==typeof e?{text:e}:e}(e),l?n[r](e,...o):function(e,r){if(null==r)return null;const n=t(a)(e),o=a;return n.forEach(((e,t)=>{if(e.includes(o.TG_PRX)){const a=e.replace(o.TG_PRX,"").replace(o.TG_SFX,"").trim();r.hasOwnProperty(a)&&null!=r[a]&&(n[t]=r[a])}})),n.join("")}(n[r],e)}function s(r,s=!1,i={}){const{hasError:c,placeholders:u,chop:f,helpers:d,handshake:p}=function(e){const{template:r,helpers:n={},handshake:o}=e,{TG_PRX:l,TG_SFX:s}=a,i=[],c="string"==typeof r?r.replace(/<!--[\s\S]*?-->/g,"").replace(/\s{2,}/g," "):r;let u=null;const f=t(a)(c);return"string"==typeof f?u=f:f.forEach(((e,t)=>{const r=RegExp(l+"\\s*(.*?)\\s*(?::\\s*(.*?)\\s*)?"+s,"g");if(e.includes(l)){const a=r.exec(e);i.push({index:t,data:(n=a[1],n||null),action:a[2]?a[2].split(",").map((e=>e.trim())):null})}var n})),i.forEach((e=>{e.action&&e.action.every((e=>"#"===e||"^^"===e||!(!e.startsWith("^")||"^^"===e)||(e.startsWith("?")&&(e=e.replace("?","")),e.startsWith("+")&&(e=e.replace("+","")),e.startsWith("[]")&&(e=e.replace("[]","")),e.startsWith(">")&&(e=e.replace(">","")),""===e||!!n[e]||(u=`Error: Missing helper: ${e}`,!1))))})),{hasError:u,placeholders:i,chop:f,helpers:n,handshake:o}}(r);if(c){function h(){return c}return s?[!1,h]:h}{let y=structuredClone(f);function m(t={},r={},...a){const s=[],c={};let f=o(t),h={...i,...r};if(t=e({data:t}),"null"===f)return y.join("");if("string"==typeof t)switch(t){case"raw":return y.join("");case"demo":if(!p)return"Error: No handshake data.";f=o(t=p);break;case"handshake":return p?structuredClone(p):"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"!==f&&(t=[t]),"null"===f?y.join(""):(t.forEach((t=>{u.forEach((r=>{const{index:a,data:s,action:i}=r,u=!i&&s,f=structuredClone(c),p={dependencies:h,memory:f};let m=t;if(s&&s.includes("/")?m.hasOwnProperty(s)?m=m[s]:s.split("/").forEach((e=>{m=m.hasOwnProperty(e)?m[e]:[]})):"@all"===s||null===s||"@root"===s?m=t:s&&(m=m[s]),u){switch(o(m)){case"function":return void(y[a]=m());case"primitive":return void(y[a]=m);case"array":return void("primitive"===o(m[0])&&(y[a]=m[0]));case"object":return void(m.text&&(y[a]=m.text))}}else{let{dataDeepLevel:r,nestedData:s}=function(t,r){const n=[];let a=0;if(t instanceof Function)return{dataDeepLevel:0,nestedData:[[t()]]};if(null==t)return{dataDeepLevel:0,nestedData:[null]};if("string"==typeof t)return{dataDeepLevel:0,nestedData:[[t]]};const o=structuredClone(t);return r.includes("#")?(e({data:o,objectCallback:function({key:e,value:t,breadcrumbs:r}){return e===r?(n[0]=[t],t):(a=r.split("/").length-1,n[a]||(n[a]=[]),n[a].push(t),t)}}),{dataDeepLevel:a,nestedData:n}):(n.push([o]),{dataDeepLevel:0,nestedData:n})}(m,i),u=n(function(e,t=10){let r={},n=[...e],a=0,o=0,l=0;n.forEach((e=>{"#"===e&&l++})),l<t&&console.error(`Error: Not enough level markers (#) for data with depth level ${t}. Found ${l} level markers in actions: ${e.join(", ")}`);do{r[o]=[],o++}while(o<=t);return n.every((e=>"#"===e?(a++,!(a>t)):e.startsWith("?")?(r[a].push({type:"route",name:e.replace("?",""),level:a}),!0):e.startsWith("^")&&"^^"!==e?(r[a].push({type:"save",name:e.replace("^",""),level:a}),!0):"^^"===e?(r[a].push({type:"overwrite",name:"none",level:a}),!0):e.startsWith("+")?(r[a].push({type:"extendedRender",name:e.replace("+",""),level:a}),!0):e.startsWith("[]")?(r[a].push({type:"mix",name:e.replace("[]",""),level:a}),!0):e.startsWith(">")?(r[a].push({type:"data",name:e.replace(">",""),level:a}),!0):(""===e||r[a].push({type:"render",name:e,level:a}),!0))),r}(i,r),r);for(let r of u){let{type:n,name:a,level:i}=r;(s[i]||[]).forEach(((r,u)=>{let f=o(r);switch(n){case"route":switch(f){case"array":r.forEach(((e,t)=>{if(null==e)return;const n=o(e),s=d[a]({data:e,...p});null!=s&&("object"===n?r[t].text=l(e,s,d,h):r[t]=l(e,s,d,h))}));break;case"object":r.text=l(r,a,d,h)}break;case"save":c[a]=structuredClone(r);break;case"overwrite":t=structuredClone(r);break;case"data":switch(f){case"array":r.forEach(((e,t)=>r[t]=e instanceof Function?d[a]({data:e(),...p}):d[a]({data:e,...p})));break;case"object":s[i]=[d[a]({data:r,...p})];break;case"function":s[i]=[d[a]({data:r(),...p})];break;case"primitive":s[i]=d[a]({data:r,...p})}break;case"render":const y="function"==typeof d[a];switch(f){case"array":y?r.forEach(((e,t)=>{if(null==e)return;const n=o(e),l=d[a]({data:e,...p});null==l&&(r[t]=null),"object"===n?e.text=l:r[t]=l})):r.forEach(((e,t)=>{if(null==e)return;const n=o(e),s=l(e,a,d,h);null==s?r[t]=null:"object"===n?e.text=s:r[t]=s}));break;case"function":s[i]=d[a]({data:r(),...p});break;case"primitive":s[i]=y?d[a]({data:r,...p}):l(r,a,d,h);break;case"object":y?s[i][u].text=d[a]({data:r,...p}):r.text=l(r,a,d,h)}break;case"extendedRender":"function"==typeof d[a]&&s[0].forEach(((e,t)=>s[0][t]=d[a]({data:e,...p})));break;case"mix":if(""===a)switch(f){case"object":Object.keys(r).find((e=>e.includes("/")))?Object.entries(r).forEach((([e,t])=>s[i][e]=t.text)):s[i]=r.text;for(let g=i-1;g>=0;g--)s[g]=e({data:s[g],objectCallback:m});function m({value:e,breadcrumbs:t}){return s[i][t]?s[i][t]:e}break;case"array":r.forEach(((e,t)=>{if(t>0){let n=o(e);if(null==e)return;r[0]+="object"===n?`${e.text}`:`${e}`,r.toSpliced(t,1)}else{let t=o(e);if(r[0]="",null===t)return;r[0]="object"===t?`${e.text}`:`${e}`}})),r.length=1}else{let b=d[a]({data:r,...p}),v=o(b);switch(r.forEach(((e,t)=>r.splice(t,1))),r.length=0,v){case"primitive":r[0]=b;break;case"array":r.push(...b)}}}}))}if(s instanceof Array&&1===s.length&&s[0]instanceof Array&&(s=s[0]),null==s[0])return;let f=o(s[0]),g=s[0];switch(f){case"primitive":if(null==g)return;y[a]=g;break;case"object":if(null==g.text)return;y[a]=g.text;break;case"array":const e=o(g[0]);y[a]="object"===e?g.map((e=>e.text)).join(""):g.join("")}}})),s.push(y.join(""))})),"array"===f?s:a?a.reduce(((e,t)=>"function"!=typeof t?e:t(e,h)),s.join("")):s.join(""))}return s?[!0,m]:m}}const i={default:{}};return{build:s,get:function(e){if(!(e instanceof Array))return function(){return'Error: Argument "location" is a string. Should be an array. E.g. ["templateName", "storageName"].'};const[t,r="default"]=e;return i[r]?i[r][t]?i[r][t]:function(){return`Error: Template "${t}" does not exist in storage "${r}".`}:function(){return`Error: Storage "${r}" does not exist.`}},add:function(e,t,...r){const[n,a="default"]=e;if(null==t)return void console.warn(`Warning: Template ${a}/${n} is not added to storage. The template is null.`);let o=t,l=!0;if(i[a]||(i[a]={}),"function"!=typeof t){let e=s(t,!0,...r);l=e[0],o=e[1]}l?i[a][n]=o:console.error(`Error: Template "${n}" looks broken and is not added to storage.`)},list:function(e=["default"]){return e.map((e=>i[e]?Object.keys(i[e]):[])).flat()},clear:function(){Object.keys(i).forEach((e=>{"default"!=e?delete i[e]:i.default={}}))},remove:function(e){const[t,r="default"]=e;return i[r]?i[r][t]?void delete i[r][t]:`Error: Template "${t}" does not exist in storage "${r}".`:`Error: Storage "${r}" does not exist.`}}}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter.naydenov/morph",
|
|
3
3
|
"description": "Template engine",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Peter Naydenov",
|
|
7
7
|
"main": "./src/main.js",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"@rollup/plugin-terser": "^0.4.4",
|
|
33
33
|
"c8": "^10.1.3",
|
|
34
34
|
"chai": "5.2.0",
|
|
35
|
-
"mocha": "11.
|
|
36
|
-
"rollup": "^4.
|
|
35
|
+
"mocha": "11.5.0",
|
|
36
|
+
"rollup": "^4.41.2"
|
|
37
37
|
},
|
|
38
38
|
"repository": {
|
|
39
39
|
"type": "git",
|
package/src/methods/build.js
CHANGED
|
@@ -105,10 +105,15 @@ function build ( tpl, extra=false, buildDependencies={} ) {
|
|
|
105
105
|
|
|
106
106
|
|
|
107
107
|
if ( data && data.includes('/') ) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
108
|
+
if ( info.hasOwnProperty ( data )) {
|
|
109
|
+
info = info[data]
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
data.split('/').forEach ( d => {
|
|
113
|
+
if ( info.hasOwnProperty(d) ) info = info[d]
|
|
114
|
+
else info = []
|
|
115
|
+
})
|
|
116
|
+
}
|
|
112
117
|
} // If data contains '/'
|
|
113
118
|
else if ( data==='@all' || data===null || data==='@root' ) info = dElement
|
|
114
119
|
else if ( data ) info = info[data]
|