neobiz 1.0.0 → 1.2.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/example/basic.html +1 -0
- package/example/basic_syntax.v.js +18 -0
- package/example/control_flow.v.js +56 -0
- package/example/exported.v.js +3 -0
- package/example/index.js +3 -0
- package/example/loop_and_Array.v.js +53 -0
- package/example/multi_tag.v.js +13 -0
- package/{test → example}/package.json +8 -3
- package/example/partials_usage.v.js +11 -0
- package/example/passing_value.v.js +17 -0
- package/example/templating.v.js +20 -0
- package/index.js +59 -1
- package/minify.js +2 -2
- package/neobiz.min.js +1 -0
- package/package.json +1 -1
- package/readme.md +165 -197
- package/simple.js +0 -63
- package/simple.min.js +0 -1
- package/test/index.js +0 -8
- package/test/node_modules/.package-lock.json +0 -16
- package/test/s.js +0 -3
- package/test/temp.html +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<h1>Hello World!</h1>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
//use object literal
|
|
4
|
+
|
|
5
|
+
import { render, outFile } from "../index.js";
|
|
6
|
+
|
|
7
|
+
console.log(
|
|
8
|
+
render({
|
|
9
|
+
h1: { text: "Hello World!" },
|
|
10
|
+
})
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
//output: <h1>Hello World!</h1>
|
|
14
|
+
|
|
15
|
+
// output static file
|
|
16
|
+
outFile("./basic.html", {
|
|
17
|
+
h1: { text: "Hello World!" },
|
|
18
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
import { render } from "../index.js";
|
|
4
|
+
|
|
5
|
+
const status = false;
|
|
6
|
+
|
|
7
|
+
// ternary operator
|
|
8
|
+
|
|
9
|
+
console.log(
|
|
10
|
+
render({
|
|
11
|
+
h1: status
|
|
12
|
+
? { text: "Hello World!" }
|
|
13
|
+
: {
|
|
14
|
+
a: { href: "#", text: "You are offline!" },
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
//<h1><a href='#'>You are offline!</a></h1
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// use IIFE
|
|
21
|
+
|
|
22
|
+
// IF
|
|
23
|
+
console.log(
|
|
24
|
+
render({
|
|
25
|
+
h1: (() => {
|
|
26
|
+
let result;
|
|
27
|
+
if (status) result = { text: "Hello World!" };
|
|
28
|
+
else
|
|
29
|
+
result = {
|
|
30
|
+
a: { href: "#", text: "You are offline!" },
|
|
31
|
+
};
|
|
32
|
+
return result;
|
|
33
|
+
})(),
|
|
34
|
+
})
|
|
35
|
+
//<h1><a href='#'>You are offline!</a></h1
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
// SWITCH
|
|
39
|
+
console.log(
|
|
40
|
+
render({
|
|
41
|
+
h1: (() => {
|
|
42
|
+
let result;
|
|
43
|
+
switch (status) {
|
|
44
|
+
case true:
|
|
45
|
+
result = { text: "Hello World!" };
|
|
46
|
+
break;
|
|
47
|
+
case false:
|
|
48
|
+
result = {
|
|
49
|
+
a: { href: "#", text: "You are offline!" },
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
})(),
|
|
54
|
+
})
|
|
55
|
+
//<h1><a href='#'>You are offline!</a></h1
|
|
56
|
+
);
|
package/example/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
import { render, range } from "../index.js";
|
|
4
|
+
|
|
5
|
+
// use range @param= length, startFrom
|
|
6
|
+
|
|
7
|
+
console.log(
|
|
8
|
+
render({
|
|
9
|
+
ul: {
|
|
10
|
+
li: range(8, 1).map((item) => ({ text: "data-" + item })),
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
//<ul><li>data-1</li><li>data-2</li><li>data-3</li><li>data-4</li><li>data-5</li><li>data-6</li><li>data-7</li><li>data-8</li></ul>
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
// map
|
|
17
|
+
console.log(
|
|
18
|
+
render({
|
|
19
|
+
ul: {
|
|
20
|
+
li: ["grape", "banana", "apple"].map((text) => ({ text })),
|
|
21
|
+
},
|
|
22
|
+
})
|
|
23
|
+
//<ul><li>grape</li><li>banana</li><li>apple</li></ul>
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// for
|
|
27
|
+
console.log(
|
|
28
|
+
render({
|
|
29
|
+
ul: {
|
|
30
|
+
li: (() => {
|
|
31
|
+
const result = [];
|
|
32
|
+
for (let i = 1; i < 9; i++) result.push({ text: "data-" + i });
|
|
33
|
+
return result;
|
|
34
|
+
})(),
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
//<ul><li>data-1</li><li>data-2</li><li>data-3</li><li>data-4</li><li>data-5</li><li>data-6</li><li>data-7</li><li>data-8</li></ul>
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// while
|
|
41
|
+
console.log(
|
|
42
|
+
render({
|
|
43
|
+
ul: {
|
|
44
|
+
li: (() => {
|
|
45
|
+
const result = [];
|
|
46
|
+
let i = 0;
|
|
47
|
+
while (++i < 9) result.push({ text: "data-" + i });
|
|
48
|
+
return result;
|
|
49
|
+
})(),
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
//<ul><li>data-1</li><li>data-2</li><li>data-3</li><li>data-4</li><li>data-5</li><li>data-6</li><li>data-7</li><li>data-8</li></ul>
|
|
53
|
+
);
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "
|
|
3
|
-
"type": "module",
|
|
2
|
+
"name": "example",
|
|
4
3
|
"version": "1.0.0",
|
|
5
|
-
"
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "example of neobiz.js cases",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"neobiz.js",
|
|
12
|
+
"view",
|
|
13
|
+
"template"
|
|
14
|
+
],
|
|
10
15
|
"author": "",
|
|
11
16
|
"license": "ISC"
|
|
12
17
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
import { render, partial } from "../index.js";
|
|
4
|
+
|
|
5
|
+
console.log(
|
|
6
|
+
render({
|
|
7
|
+
head: (await partial("./exported.v.js"))("example page"),
|
|
8
|
+
h1: { text: "This is example" },
|
|
9
|
+
})
|
|
10
|
+
//<head><title>example page</title></head><h1>This is example</h1>
|
|
11
|
+
);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
//use function, iife recommended :)
|
|
4
|
+
|
|
5
|
+
import { render } from "../index.js";
|
|
6
|
+
|
|
7
|
+
console.log(
|
|
8
|
+
// Ignore this
|
|
9
|
+
render(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins"))
|
|
10
|
+
//<h1>Hello, Vins</h1>
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
// or just use this below
|
|
14
|
+
|
|
15
|
+
const greeting = (user) => ({ h1: `Hello, ${user}` });
|
|
16
|
+
|
|
17
|
+
console.log(render(greeting("Vins")));
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
import { render } from "../index.js";
|
|
4
|
+
|
|
5
|
+
const template = (content) => ({
|
|
6
|
+
div: {
|
|
7
|
+
class: "main wrapper",
|
|
8
|
+
div: content,
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
console.log(
|
|
13
|
+
render(
|
|
14
|
+
template({
|
|
15
|
+
h1: { text: "Hello World!" },
|
|
16
|
+
})
|
|
17
|
+
)
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
//<div class='main wrapper'><div><h1>Hello World!</h1></div></div>
|
package/index.js
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
1
|
/** @format */
|
|
2
|
+
"use strict";
|
|
3
|
+
const path = require("path");
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
function render(val = {}) {
|
|
6
|
+
let result = "";
|
|
7
|
+
Object.entries(val).forEach((e) => {
|
|
8
|
+
if (e[1].length) {
|
|
9
|
+
e[1].forEach((g) => {
|
|
10
|
+
result += render({ [e[0]]: g });
|
|
11
|
+
});
|
|
12
|
+
} else if (typeof e[1] == "object") {
|
|
13
|
+
e[0] = e[0].replace(" ", "");
|
|
14
|
+
result += `<${e[0]}`;
|
|
15
|
+
const temp = [];
|
|
16
|
+
let inn = "";
|
|
17
|
+
Object.entries(e[1]).forEach((k) => {
|
|
18
|
+
if (typeof k[1] == "string" && k[0] != "text")
|
|
19
|
+
result += ` ${k[0]}='${k[1]}'`;
|
|
20
|
+
else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`;
|
|
21
|
+
else if (k[0] == "text") inn = `${k[1]}`;
|
|
22
|
+
else temp.push(k);
|
|
23
|
+
});
|
|
24
|
+
const stat = !["input", "link", "br", "hr"].includes(e[0]);
|
|
25
|
+
if (stat) result += ">";
|
|
26
|
+
result += inn;
|
|
27
|
+
for (let i of temp) result += render({ [i[0]]: i[1] });
|
|
28
|
+
if (stat) result += `</${e[0]}>`;
|
|
29
|
+
else result += "/>";
|
|
30
|
+
} else {
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
result = result.replace(/[\r\n\t]/gm, "");
|
|
34
|
+
result = result.replace(/\s{2,}/gm, " ");
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
function range(length, start = 0) {
|
|
38
|
+
return [...Array(length)].map(() => start++);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function outFile(source, data) {
|
|
42
|
+
const fs = require("fs");
|
|
43
|
+
(function () {
|
|
44
|
+
const dir = path.dirname(path.resolve(source));
|
|
45
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
|
|
46
|
+
fs.writeFileSync(source, render(data));
|
|
47
|
+
console.log(`Output file: ${source}`);
|
|
48
|
+
})();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = {
|
|
52
|
+
render,
|
|
53
|
+
range,
|
|
54
|
+
outFile,
|
|
55
|
+
partial: async (src) => {
|
|
56
|
+
const { default: i } = await import(
|
|
57
|
+
path.join("file:\\\\", process.cwd(), src)
|
|
58
|
+
);
|
|
59
|
+
return i;
|
|
60
|
+
},
|
|
61
|
+
};
|
package/minify.js
CHANGED
package/neobiz.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/** @format */"use strict";const path = require("path");function render(val = {}) { let result = ""; Object.entries(val).forEach((e) => { if (e[1].length) { e[1].forEach((g) => { result += render({ [e[0]]: g }); }); } else if (typeof e[1] == "object") { e[0] = e[0].replace(" ", ""); result += `<${e[0]}`; const temp = []; let inn = ""; Object.entries(e[1]).forEach((k) => { if (typeof k[1] == "string" && k[0] != "text") result += ` ${k[0]}='${k[1]}'`; else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`; else if (k[0] == "text") inn = `${k[1]}`; else temp.push(k); }); const stat = !["input", "link", "br", "hr"].includes(e[0]); if (stat) result += ">"; result += inn; for (let i of temp) result += render({ [i[0]]: i[1] }); if (stat) result += `</${e[0]}>`; else result += "/>"; } else { } }); result = result.replace(/[\r\n\t]/gm, ""); result = result.replace(/\s{2,}/gm, " "); return result;}function range(length, start = 0) { return [...Array(length)].map(() => start++);}function outFile(source, data) { const fs = require("fs"); (function () { const dir = path.dirname(path.resolve(source)); if (!fs.existsSync(dir)) fs.mkdirSync(dir); fs.writeFileSync(source, render(data)); console.log(`Output file: ${source}`); })();}module.exports = { render, range, outFile, partial: async (src) => { console.log(path.join("file:\\\\", process.cwd(), src)); const { default: i } = await import( path.join("file:\\\\", process.cwd(), src) ); return i; },};
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,300 +1,268 @@
|
|
|
1
1
|
<!-- @format -->
|
|
2
2
|
|
|
3
|
-
# Documentation
|
|
3
|
+
# Documentation Neobiz.js
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
> A simple view engine that has 100% JS features
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Neobiz.js is pure JS lang, transpile Object to HTML as string
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
import head from "./partials/head.v.js";
|
|
9
|
+
`read /example dir to understanding`
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
body:{
|
|
16
|
-
h1:{text:"Hello World!"},
|
|
17
|
-
b:range(10,13).filter(i=>i%2).map(i=>{text:`${i}- index`})
|
|
18
|
-
ul:[
|
|
19
|
-
{
|
|
20
|
-
li:[
|
|
21
|
-
{text:"one"},
|
|
22
|
-
{text:"two"},
|
|
23
|
-
{text:"3"},
|
|
24
|
-
]
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
li:["apple","banana","orange"].map(item=>({text:item}))
|
|
28
|
-
}
|
|
29
|
-
]
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
```
|
|
11
|
+
[this link to usage in express.js](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
|
|
12
|
+
|
|
13
|
+
**Features**
|
|
34
14
|
|
|
35
|
-
|
|
15
|
+
| function | params | return-type | description |
|
|
16
|
+
| -------- | ---------------------------------- | ----------- | ------------------------------- |
|
|
17
|
+
| render | content:object | string | rendering to HTML |
|
|
18
|
+
| outFile | destination:string, content:object | | generate static HTML file |
|
|
19
|
+
| range | length:int, start:int | Array | shortcut for looping in a range |
|
|
20
|
+
| partial | source:string | AsyncFn | including other file |
|
|
36
21
|
|
|
37
|
-
|
|
22
|
+
## Basic syntax
|
|
38
23
|
|
|
39
|
-
It's just
|
|
24
|
+
It's just Object in js!
|
|
40
25
|
|
|
41
26
|
```js
|
|
42
27
|
{
|
|
43
|
-
|
|
44
|
-
|
|
28
|
+
// text property is same as innerHTML
|
|
29
|
+
h1: {
|
|
30
|
+
text: "Hello World!";
|
|
45
31
|
}
|
|
46
32
|
}
|
|
33
|
+
// <h1>Hello World!</h1>
|
|
47
34
|
```
|
|
48
35
|
|
|
49
|
-
|
|
36
|
+
properties dom
|
|
50
37
|
|
|
51
38
|
```js
|
|
52
|
-
//use text property as innerHTML
|
|
53
39
|
{
|
|
40
|
+
// text property is same as innerHTML
|
|
54
41
|
h1: {
|
|
42
|
+
class:"heading",
|
|
55
43
|
text: "Hello World!";
|
|
44
|
+
style:"color: #DC1E1E"
|
|
56
45
|
}
|
|
57
46
|
}
|
|
47
|
+
// <h1 class="heading" style="color: #DC1E1E">Hello World!</h1>
|
|
58
48
|
```
|
|
59
49
|
|
|
60
|
-
|
|
61
|
-
<h1>Hello World!</h1>
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
#### 1.2.0 Why use js Object?
|
|
50
|
+
nested dom:
|
|
65
51
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
52
|
+
```js
|
|
53
|
+
{
|
|
54
|
+
body: {
|
|
55
|
+
h1: {
|
|
56
|
+
text: "Hello World!";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
73
61
|
|
|
74
|
-
|
|
62
|
+
siblings dom:
|
|
75
63
|
|
|
76
|
-
|
|
64
|
+
There is a case like this:
|
|
77
65
|
|
|
78
|
-
|
|
79
|
-
|
|
66
|
+
```html
|
|
67
|
+
<li>orange</li>
|
|
68
|
+
<li>banana</li>
|
|
69
|
+
```
|
|
80
70
|
|
|
81
|
-
|
|
71
|
+
Object can not use 2 keys with same name, instead use this:
|
|
82
72
|
|
|
83
73
|
```js
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
74
|
+
// Don't do this
|
|
75
|
+
{
|
|
76
|
+
li:{text:"orange"},
|
|
77
|
+
li:{text:"banana"},
|
|
78
|
+
}
|
|
87
79
|
|
|
88
|
-
|
|
80
|
+
// Do this
|
|
81
|
+
{
|
|
82
|
+
li:[
|
|
83
|
+
{text:"orange"},
|
|
84
|
+
{text:"banana"},
|
|
85
|
+
]
|
|
86
|
+
}
|
|
89
87
|
```
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
outFile([filepath],[object-view])
|
|
89
|
+
What if there is somethings in the middle of list?
|
|
94
90
|
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
```html
|
|
92
|
+
<li>orange</li>
|
|
93
|
+
<p>
|
|
94
|
+
<span>This is in the middle</span>
|
|
95
|
+
</p>
|
|
96
|
+
<li>banana</li>
|
|
99
97
|
```
|
|
100
98
|
|
|
101
|
-
|
|
99
|
+
Can't use array? just use this:
|
|
102
100
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
```js
|
|
102
|
+
{
|
|
103
|
+
li:{text:"orange"},
|
|
104
|
+
p:{span:{text:"This is in the middle"}}
|
|
105
|
+
li:{text:"banana"},
|
|
106
|
+
}
|
|
107
|
+
```
|
|
106
108
|
|
|
107
|
-
|
|
109
|
+
## Pass Value
|
|
108
110
|
|
|
109
|
-
|
|
111
|
+
use function
|
|
110
112
|
|
|
111
113
|
```js
|
|
112
|
-
|
|
113
|
-
const msg = "Welcome page!";
|
|
114
|
+
import { render } from "../index.js";
|
|
114
115
|
|
|
115
|
-
render({ h1: { text:
|
|
116
|
-
render({ h1: { text: `Message: ${msg}` } });
|
|
116
|
+
console.log(render(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins")));
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
#### 4.1.0 props
|
|
119
|
+
or
|
|
122
120
|
|
|
123
121
|
```js
|
|
124
|
-
{h1:{
|
|
125
|
-
|
|
126
|
-
//to
|
|
122
|
+
const greeting = (user) => ({ h1: `Hello, ${user}` });
|
|
127
123
|
|
|
128
|
-
|
|
124
|
+
console.log(render(greeting("Vins")));
|
|
129
125
|
```
|
|
130
126
|
|
|
131
|
-
|
|
127
|
+
## Control Flow
|
|
132
128
|
|
|
133
|
-
|
|
129
|
+
ternary:
|
|
134
130
|
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
131
|
+
```js
|
|
132
|
+
{
|
|
133
|
+
h1: status
|
|
134
|
+
? { text: "Hello World!" }
|
|
135
|
+
: {
|
|
136
|
+
a: { href: "#", text: "You are offline!" },
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
|
|
142
|
-
`the 2nd li will rewrite 1st li`
|
|
141
|
+
IIFE if-else
|
|
143
142
|
|
|
144
143
|
```js
|
|
145
144
|
{
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
h1: (() => {
|
|
146
|
+
let result;
|
|
147
|
+
if (status) result = { text: "Hello World!" };
|
|
148
|
+
else
|
|
149
|
+
result = {
|
|
150
|
+
a: { href: "#", text: "You are offline!" },
|
|
151
|
+
};
|
|
152
|
+
return result;
|
|
153
|
+
})(),
|
|
149
154
|
}
|
|
155
|
+
```
|
|
150
156
|
|
|
151
|
-
|
|
157
|
+
IIFE switch-case
|
|
152
158
|
|
|
159
|
+
```js
|
|
153
160
|
{
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
161
|
+
h1: (() => {
|
|
162
|
+
let result;
|
|
163
|
+
switch (status) {
|
|
164
|
+
case true:
|
|
165
|
+
result = { text: "Hello World!" };
|
|
166
|
+
break;
|
|
167
|
+
case false:
|
|
168
|
+
result = {
|
|
169
|
+
a: { href: "#", text: "You are offline!" },
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
})(),
|
|
157
174
|
}
|
|
158
175
|
```
|
|
159
176
|
|
|
160
|
-
|
|
177
|
+
## Loop & array manipulation
|
|
161
178
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
for example in express.js response:
|
|
165
|
-
|
|
166
|
-
`express server`
|
|
179
|
+
range & map:
|
|
167
180
|
|
|
168
181
|
```js
|
|
169
|
-
import
|
|
170
|
-
import { render } from "neobiz";
|
|
182
|
+
import { render, range } from "../index.js";
|
|
171
183
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
```js
|
|
178
|
-
export default (username) => ({ h1: { text: `Welcome, ${username}` } });
|
|
184
|
+
{
|
|
185
|
+
ul: {
|
|
186
|
+
li: range(8, 1).map((item) => ({ text: "data-" + item })),
|
|
187
|
+
},
|
|
188
|
+
}
|
|
179
189
|
```
|
|
180
190
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
##### 4.4.1 partial
|
|
191
|
+
map
|
|
184
192
|
|
|
185
193
|
```js
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
//contents
|
|
192
|
-
},
|
|
193
|
-
});
|
|
194
|
+
{
|
|
195
|
+
ul: {
|
|
196
|
+
li: ["grape", "banana", "apple"].map((text) => ({ text })),
|
|
197
|
+
},
|
|
198
|
+
}
|
|
194
199
|
```
|
|
195
200
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
`template.v.js`
|
|
201
|
+
for
|
|
199
202
|
|
|
200
203
|
```js
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
div: contents,
|
|
204
|
+
{
|
|
205
|
+
ul: {
|
|
206
|
+
li: (() => {
|
|
207
|
+
const result = [];
|
|
208
|
+
for (let i = 1; i < 9; i++) result.push({ text: "data-" + i });
|
|
209
|
+
return result;
|
|
210
|
+
})(),
|
|
209
211
|
},
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
+
}
|
|
212
213
|
```
|
|
213
214
|
|
|
214
|
-
|
|
215
|
+
while
|
|
215
216
|
|
|
216
217
|
```js
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
218
|
+
{
|
|
219
|
+
ul: {
|
|
220
|
+
li: (() => {
|
|
221
|
+
const result = [];
|
|
222
|
+
let i = 0;
|
|
223
|
+
while (++i < 9) result.push({ text: "data-" + i });
|
|
224
|
+
return result;
|
|
225
|
+
})(),
|
|
224
226
|
},
|
|
225
|
-
|
|
226
|
-
);
|
|
227
|
+
}
|
|
227
228
|
```
|
|
228
229
|
|
|
229
|
-
|
|
230
|
+
## partial & templating
|
|
230
231
|
|
|
231
|
-
|
|
232
|
+
### partial
|
|
232
233
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
`Ternary operator`
|
|
234
|
+
make sure to export as function & default
|
|
236
235
|
|
|
237
236
|
```js
|
|
238
|
-
|
|
237
|
+
import { render, partial } from "../index.js";
|
|
239
238
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
};
|
|
239
|
+
{
|
|
240
|
+
head: (await partial("./exported.v.js"))("example page"),
|
|
241
|
+
h1: { text: "This is example" },
|
|
242
|
+
}
|
|
245
243
|
```
|
|
246
244
|
|
|
247
|
-
`
|
|
245
|
+
`/exported.v.js`
|
|
248
246
|
|
|
249
247
|
```js
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const result = {
|
|
253
|
-
h1: status
|
|
254
|
-
? { text: "already active" }
|
|
255
|
-
: { button: { onclick: "...", text: "activate" } },
|
|
256
|
-
};
|
|
248
|
+
export default (title) => ({ title: { text: title } });
|
|
257
249
|
```
|
|
258
250
|
|
|
259
|
-
|
|
251
|
+
### templating
|
|
260
252
|
|
|
261
|
-
|
|
253
|
+
this example use single file usage. for modular usage leave to you
|
|
262
254
|
|
|
263
255
|
```js
|
|
264
|
-
import {
|
|
256
|
+
import { render } from "../index.js";
|
|
265
257
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
258
|
+
const template = (content) => ({
|
|
259
|
+
div: {
|
|
260
|
+
class: "main wrapper",
|
|
261
|
+
div: content,
|
|
269
262
|
},
|
|
270
|
-
};
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
maps in array
|
|
274
|
-
|
|
275
|
-
```js
|
|
276
|
-
import { range } from "neobiz";
|
|
277
|
-
|
|
278
|
-
return {
|
|
279
|
-
ul: {
|
|
280
|
-
li: ["one", "two"].map((item) => ({ text: item })),
|
|
281
|
-
},
|
|
282
|
-
};
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
IIFE
|
|
263
|
+
});
|
|
286
264
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
li: ((array) => {
|
|
291
|
-
for (let i = 0; i < 10; i++) array[i] = { text: array[i] };
|
|
292
|
-
return array;
|
|
293
|
-
})(),
|
|
294
|
-
},
|
|
295
|
-
};
|
|
265
|
+
template({
|
|
266
|
+
h1: { text: "Hello World!" },
|
|
267
|
+
});
|
|
296
268
|
```
|
|
297
|
-
|
|
298
|
-
##### 4.5.3 while loop
|
|
299
|
-
|
|
300
|
-
just same as `4.5.2` section
|
package/simple.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/** @format */
|
|
2
|
-
"use strict";
|
|
3
|
-
|
|
4
|
-
function render(val = {}) {
|
|
5
|
-
let result = "";
|
|
6
|
-
Object.entries(val).forEach((e) => {
|
|
7
|
-
if (e[1].length) {
|
|
8
|
-
e[1].forEach((g) => {
|
|
9
|
-
result += render({ [e[0]]: g });
|
|
10
|
-
});
|
|
11
|
-
} else if (typeof e[1] == "object") {
|
|
12
|
-
e[0] = e[0].replace(" ", "");
|
|
13
|
-
result += `<${e[0]}`;
|
|
14
|
-
const temp = [];
|
|
15
|
-
let inn = "";
|
|
16
|
-
Object.entries(e[1]).forEach((k) => {
|
|
17
|
-
if (typeof k[1] == "string" && k[0] != "text")
|
|
18
|
-
result += ` ${k[0]}='${k[1]}'`;
|
|
19
|
-
else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`;
|
|
20
|
-
else if (k[0] == "text") inn = `${k[1]}`;
|
|
21
|
-
else temp.push(k);
|
|
22
|
-
});
|
|
23
|
-
const stat = !["input", "link", "br", "hr"].includes(e[0]);
|
|
24
|
-
if (stat) result += ">";
|
|
25
|
-
result += inn;
|
|
26
|
-
for (let i of temp) result += render({ [i[0]]: i[1] });
|
|
27
|
-
if (stat) result += `</${e[0]}>`;
|
|
28
|
-
else result += "/>";
|
|
29
|
-
} else {
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
result = result.replace(/[\r\n\t]/gm, "");
|
|
33
|
-
result = result.replace(/\s{2,}/gm, " ");
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
function range(length, start = 0) {
|
|
37
|
-
return [...Array(length)].map(() => start++);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function outFile(source, data) {
|
|
41
|
-
const fs = require("fs");
|
|
42
|
-
const path = require("path");
|
|
43
|
-
(function () {
|
|
44
|
-
const dir = path.dirname(path.resolve(source));
|
|
45
|
-
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
|
|
46
|
-
fs.writeFileSync(source, render(data));
|
|
47
|
-
console.log(`Output file: ${source}`);
|
|
48
|
-
})();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function readf(source) {
|
|
52
|
-
const fs = require("fs");
|
|
53
|
-
const path = require("path");
|
|
54
|
-
|
|
55
|
-
return fs.readFileSync(path.resolve(source)).toString();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
module.exports = {
|
|
59
|
-
render,
|
|
60
|
-
range,
|
|
61
|
-
outFile,
|
|
62
|
-
readf,
|
|
63
|
-
};
|
package/simple.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/** @format */"use strict";function render(val = {}) { let result = ""; Object.entries(val).forEach((e) => { if (e[1].length) { e[1].forEach((g) => { result += render({ [e[0]]: g }); }); } else if (typeof e[1] == "object") { e[0] = e[0].replace(" ", ""); result += `<${e[0]}`; const temp = []; let inn = ""; Object.entries(e[1]).forEach((k) => { if (typeof k[1] == "string" && k[0] != "text") result += ` ${k[0]}='${k[1]}'`; else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`; else if (k[0] == "text") inn = `${k[1]}`; else temp.push(k); }); const stat = !["input", "link", "br", "hr"].includes(e[0]); if (stat) result += ">"; result += inn; for (let i of temp) result += render({ [i[0]]: i[1] }); if (stat) result += `</${e[0]}>`; else result += "/>"; } else { } }); result = result.replace(/[\r\n\t]/gm, ""); result = result.replace(/\s{2,}/gm, " "); return result;}function range(length, start = 0) { return [...Array(length)].map(() => start++);}function outFile(source, data) { const fs = require("fs"); const path = require("path"); (function () { const dir = path.dirname(path.resolve(source)); if (!fs.existsSync(dir)) fs.mkdirSync(dir); fs.writeFileSync(source, render(data)); console.log(`Output file: ${source}`); })();}function readf(source) { const fs = require("fs"); const path = require("path"); return fs.readFileSync(path.resolve(source)).toString();}module.exports = { render, range, outFile, readf,};
|
package/test/index.js
DELETED
package/test/s.js
DELETED
package/test/temp.html
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<h1>hello Dimas!</h1>
|