neobiz 1.2.4 → 1.3.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/fromString.v.js +27 -0
- package/example/interact_withDOM.v.js +16 -0
- package/example/temp.html +1 -0
- package/index.js +19 -11
- package/package.json +8 -6
- package/readme.md +113 -14
- package/str.h.js +78 -0
- package/minify.js +0 -11
- package/neobiz.min.js +0 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
import { outFile, fromStr } from "../index.js";
|
|
4
|
+
|
|
5
|
+
const temp = `
|
|
6
|
+
<div id="first">
|
|
7
|
+
<ul id="outer">
|
|
8
|
+
<li>1</li>
|
|
9
|
+
<li>2</li>
|
|
10
|
+
</ul>
|
|
11
|
+
<div id="inner-div">
|
|
12
|
+
<ul id="inner-ul">
|
|
13
|
+
<li>3</li>
|
|
14
|
+
<li>4</li>
|
|
15
|
+
</ul>
|
|
16
|
+
</div>
|
|
17
|
+
</div>
|
|
18
|
+
`;
|
|
19
|
+
|
|
20
|
+
const { div } = fromStr(temp);
|
|
21
|
+
|
|
22
|
+
div.ul.li.text = "data-1";
|
|
23
|
+
div.div.ul.class = "inner";
|
|
24
|
+
|
|
25
|
+
outFile("./temp.html", { div });
|
|
26
|
+
|
|
27
|
+
console.dir({ div }, { depth: null });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
import { outFile } from "../index.js";
|
|
4
|
+
|
|
5
|
+
outFile("./temp.html", {
|
|
6
|
+
button: { onclick: "sayHi(this)", id: "example" },
|
|
7
|
+
script: {
|
|
8
|
+
html: function () {
|
|
9
|
+
document.getElementById("example").innerText = "click me!";
|
|
10
|
+
|
|
11
|
+
function sayHi(el) {
|
|
12
|
+
el.innerText = "hi!";
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<div id='first'><ul id='outer'><li>data-1</li><li>2</li></ul><div id='inner-div'><ul id='inner-ul' class='inner'><li>3</li><li>4</li></ul></div></div>
|
package/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/** @format */
|
|
2
2
|
"use strict";
|
|
3
3
|
const path = require("path");
|
|
4
|
-
|
|
5
4
|
function render(val = {}) {
|
|
6
5
|
let result = "";
|
|
7
6
|
Object.entries(val).forEach((e) => {
|
|
@@ -10,23 +9,31 @@ function render(val = {}) {
|
|
|
10
9
|
result += render({ [e[0]]: g });
|
|
11
10
|
});
|
|
12
11
|
} else if (typeof e[1] == "object") {
|
|
13
|
-
e[0] = e[0].replace(
|
|
12
|
+
e[0] = e[0].replace(/\s/g, "");
|
|
14
13
|
result += `<${e[0]}`;
|
|
15
14
|
const temp = [];
|
|
16
15
|
let inn = "";
|
|
17
16
|
Object.entries(e[1]).forEach((k) => {
|
|
18
17
|
if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html")
|
|
19
18
|
result += ` ${k[0]}='${k[1]}'`;
|
|
20
|
-
// else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`;
|
|
21
19
|
else if (k[0] == "text")
|
|
22
|
-
inn = `${
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
inn = `${
|
|
21
|
+
typeof k[1] == "string"
|
|
22
|
+
? k[1]
|
|
23
|
+
.replace(/&/g, "&")
|
|
24
|
+
.replace(/</g, "<")
|
|
25
|
+
.replace(/>/g, ">")
|
|
26
|
+
.replace(/"/g, """)
|
|
27
|
+
.replace(/'/g, "'")
|
|
28
|
+
: k[1].toString()
|
|
29
|
+
}`;
|
|
30
|
+
else if (k[0] == "html") {
|
|
31
|
+
inn = `${
|
|
32
|
+
typeof k[1] != "function" ? k[1] : (k[1] + "").replace(/.*{/, "")
|
|
33
|
+
}`;
|
|
34
|
+
inn =
|
|
35
|
+
typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
|
|
36
|
+
} else temp.push(k);
|
|
30
37
|
});
|
|
31
38
|
const stat = !["input", "link", "br", "hr"].includes(e[0]);
|
|
32
39
|
if (stat) result += ">";
|
|
@@ -59,6 +66,7 @@ module.exports = {
|
|
|
59
66
|
render,
|
|
60
67
|
range,
|
|
61
68
|
outFile,
|
|
69
|
+
fromStr: require("./str.h.js"),
|
|
62
70
|
partial: async (src) => {
|
|
63
71
|
const { default: i } = await import(
|
|
64
72
|
path.join("file:\\\\", process.cwd(), src)
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neobiz",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "simple view engine
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "A simple view engine powered by JS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [
|
|
10
|
-
"view
|
|
11
|
-
"template
|
|
12
|
-
"
|
|
10
|
+
"view",
|
|
11
|
+
"template",
|
|
12
|
+
"dom",
|
|
13
|
+
"html",
|
|
14
|
+
"express"
|
|
13
15
|
],
|
|
14
|
-
"license": "
|
|
16
|
+
"license": "MIT"
|
|
15
17
|
}
|
package/readme.md
CHANGED
|
@@ -4,28 +4,29 @@
|
|
|
4
4
|
|
|
5
5
|
> A simple view engine that has 100% JS features
|
|
6
6
|
|
|
7
|
-
[Github](https://
|
|
7
|
+
[[Github]](https://github.com/harakun24/neobiz.js)
|
|
8
8
|
Neobiz.js is pure in JS lang, transform Object into HTML
|
|
9
9
|
|
|
10
10
|
`read /example dir to understanding`
|
|
11
11
|
|
|
12
|
-
[
|
|
12
|
+
[link to usage in express.js](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
---
|
|
15
15
|
|
|
16
|
-
| function | params
|
|
17
|
-
| -------- |
|
|
18
|
-
| render | content
|
|
19
|
-
| outFile | destination
|
|
20
|
-
| range | length
|
|
21
|
-
| partial | source
|
|
16
|
+
| function | params | return-type | description |
|
|
17
|
+
| -------- | ------------------------------------ | ----------- | -------------------------------- |
|
|
18
|
+
| render | content[object] | string | rendering to HTML |
|
|
19
|
+
| outFile | destination[string], content[object] | void | generate static HTML file |
|
|
20
|
+
| range | length[int], start[int] | Array | shortcut for looping in a range |
|
|
21
|
+
| partial | source[string] | AsyncFn | including other file |
|
|
22
|
+
| fromStr | str[string] | Object | generate object from HTML string |
|
|
22
23
|
|
|
23
24
|
#### Why use this?
|
|
24
25
|
|
|
25
|
-
-
|
|
26
|
-
- **directly**
|
|
26
|
+
- Integrating back-end to view
|
|
27
|
+
- **directly** manipulation data or Model with a view
|
|
27
28
|
- Generate static html file
|
|
28
|
-
- Full control of
|
|
29
|
+
- Full control of view. Such as filtering, encrypting the data before serve, or just like conditional and loop case
|
|
29
30
|
- Almost no need to learn anything. Just need to learn how to render
|
|
30
31
|
- Limitless potential, because it's just js. Almost all library should be compatible
|
|
31
32
|
|
|
@@ -35,7 +36,7 @@ It's just an Object!
|
|
|
35
36
|
|
|
36
37
|
```js
|
|
37
38
|
{
|
|
38
|
-
// text property is same as
|
|
39
|
+
// text property is same as innerText
|
|
39
40
|
h1: {
|
|
40
41
|
text: "Hello World!",
|
|
41
42
|
}
|
|
@@ -77,6 +78,17 @@ nested dom:
|
|
|
77
78
|
}
|
|
78
79
|
```
|
|
79
80
|
|
|
81
|
+
expression & operator
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
{
|
|
85
|
+
span: [
|
|
86
|
+
{ text: 24 + 12 / 6 },
|
|
87
|
+
{ text: !false },
|
|
88
|
+
],
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
80
92
|
siblings dom:
|
|
81
93
|
|
|
82
94
|
There is a case like this:
|
|
@@ -86,7 +98,7 @@ There is a case like this:
|
|
|
86
98
|
<li>banana</li>
|
|
87
99
|
```
|
|
88
100
|
|
|
89
|
-
Object can not use 2 keys with the same name
|
|
101
|
+
Object can not use 2 keys with the same name
|
|
90
102
|
|
|
91
103
|
```js
|
|
92
104
|
// Don't do this
|
|
@@ -127,6 +139,93 @@ Can't use array? just use this:
|
|
|
127
139
|
}
|
|
128
140
|
```
|
|
129
141
|
|
|
142
|
+
### Components
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import header from "./header.v.js"; //function type
|
|
146
|
+
import footer from "./footer.v.js"; //object type
|
|
147
|
+
|
|
148
|
+
render({
|
|
149
|
+
div: header({ user: "dim24" }),
|
|
150
|
+
p: { text: "this is text" },
|
|
151
|
+
div: footer,
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
header.v.js
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
export default ({ user }) => ({ h1: { text: `Hi, ${user}` } });
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
footer.v.js
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
export default {
|
|
165
|
+
script: { src: "/assets/js/main.js" },
|
|
166
|
+
span: { text: "this is footer" },
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Interacting with DOM
|
|
171
|
+
|
|
172
|
+
use `script` to interact with browser with **html** property
|
|
173
|
+
|
|
174
|
+
wrap the script with a function, or use string
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
{
|
|
178
|
+
button:{onclick:"sayHi(this)",id:"hi-button"},
|
|
179
|
+
script:{
|
|
180
|
+
html:()=>{
|
|
181
|
+
document.getElementById("hi-button").innerText="Click me!";
|
|
182
|
+
|
|
183
|
+
function sayHi(el){
|
|
184
|
+
el.innerText="Hi!";
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## fromStr
|
|
193
|
+
|
|
194
|
+
If you already have an html file and want to modify it, or make it as a template, just use `fromStr` function
|
|
195
|
+
|
|
196
|
+
> this function is still has some issues, but for simple html fromStr is work fine!
|
|
197
|
+
|
|
198
|
+
- wrap all tag into single parent
|
|
199
|
+
- use external script and css
|
|
200
|
+
- repetitive nested could caused weird behavior (help me, please!)
|
|
201
|
+
|
|
202
|
+
I still try to improve this feature. **if you can provide some help** just tell me please... :)
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
const { fromStr, render } = require("./index");
|
|
206
|
+
|
|
207
|
+
const { div } = fromStr(`
|
|
208
|
+
<div>
|
|
209
|
+
<h1>Hello dims!</h1>
|
|
210
|
+
</div>
|
|
211
|
+
`);
|
|
212
|
+
|
|
213
|
+
div.id = "main";
|
|
214
|
+
div.h1.class = "heading";
|
|
215
|
+
div.p = { text: "this is a whole text" };
|
|
216
|
+
|
|
217
|
+
console.log(render({ div }));
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
result
|
|
221
|
+
|
|
222
|
+
```html
|
|
223
|
+
<div id="main">
|
|
224
|
+
<h1 class="heading">Hello dims!</h1>
|
|
225
|
+
<p>this is a whole text</p>
|
|
226
|
+
</div>
|
|
227
|
+
```
|
|
228
|
+
|
|
130
229
|
## Pass Value
|
|
131
230
|
|
|
132
231
|
use function
|
package/str.h.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
function fromStr(letter, prefix = 0) {
|
|
4
|
+
if (letter == "") return;
|
|
5
|
+
letter = letter.replace(/[\r\n\t]/gm, "").replace(/>\s{2,}</gm, "><");
|
|
6
|
+
const result = {};
|
|
7
|
+
|
|
8
|
+
result.tag = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)[1];
|
|
9
|
+
|
|
10
|
+
result.tag = result.tag + Array(prefix).fill(" ").join("");
|
|
11
|
+
|
|
12
|
+
result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
|
|
13
|
+
|
|
14
|
+
result.props = result.head.match(/[a-zA-Z0-9]{1,}=(["']).*?\1/g);
|
|
15
|
+
|
|
16
|
+
result.inner = letter
|
|
17
|
+
.replace(result.head, "")
|
|
18
|
+
.split("")
|
|
19
|
+
.reverse()
|
|
20
|
+
.join("")
|
|
21
|
+
.replace(
|
|
22
|
+
`</${result.tag.replace(Array(prefix).fill(" ").join(""), "")}>`
|
|
23
|
+
.split("")
|
|
24
|
+
.reverse()
|
|
25
|
+
.join(""),
|
|
26
|
+
""
|
|
27
|
+
)
|
|
28
|
+
.split("")
|
|
29
|
+
.reverse()
|
|
30
|
+
.join("");
|
|
31
|
+
let temp = { [result.tag]: {} };
|
|
32
|
+
if (result.props)
|
|
33
|
+
result.attr = result.props.forEach((e) => {
|
|
34
|
+
let y = e.replace(/.*=(['"])/, "");
|
|
35
|
+
y = y.substring(0, y.length - 1);
|
|
36
|
+
let w = e.substring(0, e.indexOf("="));
|
|
37
|
+
temp[result.tag][w] = y;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
let s = [];
|
|
41
|
+
let ttexr = result.inner;
|
|
42
|
+
|
|
43
|
+
while (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)) {
|
|
44
|
+
let ss = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)[0];
|
|
45
|
+
const h = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(ss)[1];
|
|
46
|
+
const sss = ttexr.slice(ttexr.indexOf(ss));
|
|
47
|
+
const f = {
|
|
48
|
+
a: sss.replace("<" + h, "").indexOf(`</${h}>`),
|
|
49
|
+
b: sss.replace("<" + h, "").indexOf(`<${h}`),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
ss = f.a > f.b ? sss : ss;
|
|
53
|
+
ttexr = ttexr.replace(ss, "");
|
|
54
|
+
s.push(ss);
|
|
55
|
+
}
|
|
56
|
+
result.status = s;
|
|
57
|
+
result.puretext = ttexr ? ttexr : ttexr + " ";
|
|
58
|
+
// result.temp = temp;
|
|
59
|
+
if (result.puretext != " ") temp[result.tag].text = result.puretext;
|
|
60
|
+
if (result.status) {
|
|
61
|
+
const ss = new Set();
|
|
62
|
+
const current = {};
|
|
63
|
+
s.forEach((e) => {
|
|
64
|
+
const hh = /<[a-zA-Z0-9]{1,}\s?/.exec(e)[0].substring(1);
|
|
65
|
+
if (ss.has(hh)) {
|
|
66
|
+
current[hh] = current[hh] + 1;
|
|
67
|
+
} else {
|
|
68
|
+
current[hh] = 0;
|
|
69
|
+
ss.add(hh);
|
|
70
|
+
}
|
|
71
|
+
temp = {
|
|
72
|
+
[result.tag]: { ...temp[result.tag], ...fromStr(e, current[hh]) },
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return temp;
|
|
77
|
+
}
|
|
78
|
+
module.exports = fromStr;
|
package/minify.js
DELETED
package/neobiz.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
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; },};
|