@peter.naydenov/morph 0.0.1
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 +7 -0
- package/LICENSE +21 -0
- package/Migration.guide.md +3 -0
- package/README.md +207 -0
- package/dist/morph.cjs +1 -0
- package/dist/morph.esm.mjs +1 -0
- package/dist/morph.umd.js +1 -0
- package/package.json +61 -0
- package/rollup.config.js +42 -0
- package/src/main.js +100 -0
- package/src/methods/_actionSupply.js +19 -0
- package/src/methods/_chopTemplates.js +60 -0
- package/src/methods/_defineData.js +32 -0
- package/src/methods/_defineType.js +16 -0
- package/src/methods/_readTemplate.js +78 -0
- package/src/methods/_renderHolder.js +26 -0
- package/src/methods/_setupActions.js +69 -0
- package/src/methods/build.js +239 -0
- package/src/methods/render.js +26 -0
- package/src/methods/settings.js +6 -0
- package/test/01-chop.test.js +24 -0
- package/test/02-build.test.js +470 -0
- package/test/03-storage.test.js +78 -0
- package/test/04-commands.js +73 -0
- package/test/05-data.test.js +168 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import morph from '../src/main.js'
|
|
2
|
+
import { expect } from 'chai'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
describe ( 'morph: Data', () => {
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
it ( 'Using string literals in helpers', () => {
|
|
11
|
+
const myTpl = {
|
|
12
|
+
template : `My name is {{ person: hello }}.`
|
|
13
|
+
, helpers : {
|
|
14
|
+
hello: ({name, age}) => {
|
|
15
|
+
return `${name}o. Age is ${age}`
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const templateFn = morph.build ( myTpl );
|
|
20
|
+
const result = templateFn({
|
|
21
|
+
person: {
|
|
22
|
+
name: 'Robert'
|
|
23
|
+
, age: 30
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
});
|
|
27
|
+
expect ( result ).to.be.equal ( 'My name is Roberto. Age is 30.' )
|
|
28
|
+
}) // it using string literals in helpers
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
it ( 'Helper returns null', () => {
|
|
33
|
+
const myTpl = {
|
|
34
|
+
template : `My name is {{ person: hello }}.`
|
|
35
|
+
, helpers : {
|
|
36
|
+
hello: ({name, age}) => null
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const templateFn = morph.build ( myTpl );
|
|
40
|
+
const result = templateFn({
|
|
41
|
+
person: {
|
|
42
|
+
name: 'Robert'
|
|
43
|
+
, age: 30
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
});
|
|
47
|
+
expect ( result ).to.be.equal ( 'My name is {{ person: hello }}.' )
|
|
48
|
+
}) // it helper returns null
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
it ( 'Conditional rendering with string literals', () => {
|
|
53
|
+
// Conditional rendering mean that helper rendering could be called
|
|
54
|
+
// if data coresponds to some condition.
|
|
55
|
+
// Returning null means: do not render this data
|
|
56
|
+
const myTpl = {
|
|
57
|
+
template : "Hello, I'm {{ persons: []coma, ?web, hello }}"
|
|
58
|
+
, helpers : {
|
|
59
|
+
hello: ( data ) => {
|
|
60
|
+
const { name, age } = data;
|
|
61
|
+
if ( age < 28 ) return null
|
|
62
|
+
else return `${name} - ${age} years old`
|
|
63
|
+
}
|
|
64
|
+
, coma: (res) => res // Coma is a mixing helper
|
|
65
|
+
.filter ( x => x != null) // filter is required to remove cancelled renders
|
|
66
|
+
.map ( x => x.text )
|
|
67
|
+
.join ( ', ' )
|
|
68
|
+
, a : `<a href="{{href}}">{{text}}</a>`
|
|
69
|
+
, web: ( d ) => {
|
|
70
|
+
if ( d.href ) return 'a' // response 'a' means: render this data with helper 'a'
|
|
71
|
+
else return null // response 'null' in conditional render means: do not render
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const templateFn = morph.build ( myTpl );
|
|
76
|
+
const result = templateFn({
|
|
77
|
+
persons: [
|
|
78
|
+
{ name: 'Robert' , age: 30, href: 'http://robert.com' }
|
|
79
|
+
, { name: 'John' , age: 64 }
|
|
80
|
+
, { name: 'Edward' , age: 21, href: 'http://edward.com' }
|
|
81
|
+
, { name: 'Bob' , age: 34 , href: 'http://bob.com' }
|
|
82
|
+
]
|
|
83
|
+
});
|
|
84
|
+
expect ( result ).to.be.equal ( `Hello, I'm <a href="http://robert.com">Robert - 30 years old</a>, John - 64 years old, <a href="http://bob.com">Bob - 34 years old</a>` )
|
|
85
|
+
}) // it conditional rendering with string literals
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
it ( 'Post process functions', () => {
|
|
90
|
+
const myTpl = {
|
|
91
|
+
template : `The template:`
|
|
92
|
+
}
|
|
93
|
+
const
|
|
94
|
+
post1 = ( data ) => `${data} >post1`
|
|
95
|
+
, post2 = ( data ) => `${data} >post2`
|
|
96
|
+
, post3 = ( data ) => `${data} >post3`
|
|
97
|
+
;
|
|
98
|
+
const templateFn = morph.build ( myTpl );
|
|
99
|
+
const result = templateFn ( {}, post1, post2, post3 );
|
|
100
|
+
expect ( result ).to.be.equal ( `The template: >post1 >post2 >post3` )
|
|
101
|
+
}) // it post process functions
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
it ( 'Read data from external state', () => {
|
|
106
|
+
const state = {
|
|
107
|
+
name: 'Robert'
|
|
108
|
+
, age: 30
|
|
109
|
+
, job: 'software developer'
|
|
110
|
+
}
|
|
111
|
+
const myTpl = {
|
|
112
|
+
template : `Hello from {{ :name }}. I'm {{ :age }} years old {{ :job }}`
|
|
113
|
+
, helpers : {
|
|
114
|
+
name: ( data ) => state.name
|
|
115
|
+
, age: ( data ) => state.age
|
|
116
|
+
, job: ( data ) => state.job
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const templateFn = morph.build ( myTpl );
|
|
120
|
+
const result = templateFn();
|
|
121
|
+
expect ( result ).to.be.equal ( `Hello from Robert. I'm 30 years old software developer` )
|
|
122
|
+
}) // it read data from external state
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
it ( 'Function arguments - data only', () => {
|
|
127
|
+
const state = {
|
|
128
|
+
name: 'Robert'
|
|
129
|
+
, age: 30
|
|
130
|
+
, job: 'software developer'
|
|
131
|
+
}
|
|
132
|
+
const myTpl = {
|
|
133
|
+
template : `Hello from {{ name }}. I'm {{ age }} years old {{ job }}`
|
|
134
|
+
}
|
|
135
|
+
const templateFn = morph.build ( myTpl );
|
|
136
|
+
const result = templateFn({
|
|
137
|
+
name: () => state.name
|
|
138
|
+
, age: () => state.age
|
|
139
|
+
, job: () => state.job
|
|
140
|
+
});
|
|
141
|
+
expect ( result ).to.be.equal ( `Hello from Robert. I'm 30 years old software developer` )
|
|
142
|
+
}) // it function arguments - data only
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
it ( 'Function arguments with actions', () => {
|
|
147
|
+
const state = {
|
|
148
|
+
name: 'Robert'
|
|
149
|
+
, age: 30
|
|
150
|
+
, job: 'software developer'
|
|
151
|
+
}
|
|
152
|
+
const myTpl = {
|
|
153
|
+
template : `Hello from {{ name }}. I'm {{ age: extraAge }} years old {{ job }}`
|
|
154
|
+
, helpers : {
|
|
155
|
+
extraAge: ( data ) => data+3
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const templateFn = morph.build ( myTpl );
|
|
159
|
+
const result = templateFn({
|
|
160
|
+
name: () => state.name
|
|
161
|
+
, age: () => state.age
|
|
162
|
+
, job: () => state.job
|
|
163
|
+
});
|
|
164
|
+
expect ( result ).to.be.equal ( `Hello from Robert. I'm 33 years old software developer` )
|
|
165
|
+
}) // it function arguments with actions
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
}) // describe
|