neobiz 1.2.5 → 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/temp.html +1 -0
- package/index.js +2 -3
- package/package.json +5 -6
- package/readme.md +45 -7
- package/str.h.js +78 -0
|
@@ -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 @@
|
|
|
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,14 +9,13 @@ 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
20
|
inn = `${
|
|
23
21
|
typeof k[1] == "string"
|
|
@@ -68,6 +66,7 @@ module.exports = {
|
|
|
68
66
|
render,
|
|
69
67
|
range,
|
|
70
68
|
outFile,
|
|
69
|
+
fromStr: require("./str.h.js"),
|
|
71
70
|
partial: async (src) => {
|
|
72
71
|
const { default: i } = await import(
|
|
73
72
|
path.join("file:\\\\", process.cwd(), src)
|
package/package.json
CHANGED
|
@@ -1,18 +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
|
|
10
|
+
"view",
|
|
11
11
|
"template",
|
|
12
12
|
"dom",
|
|
13
13
|
"html",
|
|
14
|
-
""
|
|
15
|
-
"view"
|
|
14
|
+
"express"
|
|
16
15
|
],
|
|
17
|
-
"license": "
|
|
16
|
+
"license": "MIT"
|
|
18
17
|
}
|
package/readme.md
CHANGED
|
@@ -11,14 +11,15 @@ Neobiz.js is pure in JS lang, transform Object into HTML
|
|
|
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
|
|
|
@@ -188,6 +189,43 @@ wrap the script with a function, or use string
|
|
|
188
189
|
|
|
189
190
|
```
|
|
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
|
+
|
|
191
229
|
## Pass Value
|
|
192
230
|
|
|
193
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;
|