neobiz 1.3.6 → 1.4.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/browser.js +179 -0
- package/index.js +43 -13
- package/package.json +1 -1
- package/readme.md +39 -0
package/browser.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/** @format */
|
|
2
|
+
|
|
3
|
+
function range(length, start = 0) {
|
|
4
|
+
return [...Array(length)].map(() => start++);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function fromStr(letter, prefix = 0) {
|
|
8
|
+
if (letter == "") return {};
|
|
9
|
+
|
|
10
|
+
letter = letter.replace(/[\r\n\t]/gm, "").replace(/>\s{2,}</gm, "><");
|
|
11
|
+
const result = {};
|
|
12
|
+
|
|
13
|
+
if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)?.[1]) {
|
|
14
|
+
result.tag = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)[1];
|
|
15
|
+
|
|
16
|
+
result.tag = result.tag + Array(prefix).fill(" ").join("");
|
|
17
|
+
|
|
18
|
+
result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
|
|
19
|
+
|
|
20
|
+
result.inner = letter
|
|
21
|
+
.replace(result.head, "")
|
|
22
|
+
.split("")
|
|
23
|
+
.reverse()
|
|
24
|
+
.join("")
|
|
25
|
+
.replace(
|
|
26
|
+
`</${result.tag.replace(Array(prefix).fill(" ").join(""), "")}>`
|
|
27
|
+
.split("")
|
|
28
|
+
.reverse()
|
|
29
|
+
.join(""),
|
|
30
|
+
""
|
|
31
|
+
)
|
|
32
|
+
.split("")
|
|
33
|
+
.reverse()
|
|
34
|
+
.join("");
|
|
35
|
+
} else {
|
|
36
|
+
result.tag = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(letter)[1];
|
|
37
|
+
|
|
38
|
+
result.tag = result.tag + Array(prefix).fill(" ").join("");
|
|
39
|
+
|
|
40
|
+
result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
|
|
41
|
+
result.inner = "";
|
|
42
|
+
}
|
|
43
|
+
result.props = result.head.match(/[a-zA-Z0-9]{1,}=(["']).*?\1/g);
|
|
44
|
+
|
|
45
|
+
let temp = { [result.tag]: {} };
|
|
46
|
+
if (result.props)
|
|
47
|
+
result.attr = result.props.forEach((e) => {
|
|
48
|
+
let y = e.replace(/.*=(['"])/, "");
|
|
49
|
+
y = y.substring(0, y.length - 1);
|
|
50
|
+
let w = e.substring(0, e.indexOf("="));
|
|
51
|
+
temp[result.tag][w] = y;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
let s = [];
|
|
55
|
+
let ttexr = result.inner;
|
|
56
|
+
while (
|
|
57
|
+
/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr) ||
|
|
58
|
+
/<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)
|
|
59
|
+
) {
|
|
60
|
+
let ss;
|
|
61
|
+
if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)) {
|
|
62
|
+
ss = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)[0];
|
|
63
|
+
const h = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(ss)[1];
|
|
64
|
+
const sss = ttexr.slice(ttexr.indexOf(ss));
|
|
65
|
+
const f = {
|
|
66
|
+
a: sss.replace("<" + h, "").indexOf(`</${h}>`),
|
|
67
|
+
b: sss.replace("<" + h, "").indexOf(`<${h}`),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
ss = f.a > f.b && f.b > -1 ? sss : ss;
|
|
71
|
+
} else {
|
|
72
|
+
ss = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)[0];
|
|
73
|
+
}
|
|
74
|
+
ttexr = ttexr.replace(ss, "");
|
|
75
|
+
s.push(ss);
|
|
76
|
+
}
|
|
77
|
+
s.sort((a, b) => {
|
|
78
|
+
const r = { a: result.inner.indexOf(a), b: result.inner.indexOf(b) };
|
|
79
|
+
return r.a > r.b ? 1 : r.a < r.b ? -1 : 0;
|
|
80
|
+
});
|
|
81
|
+
result.status = s;
|
|
82
|
+
result.puretext = ttexr ? ttexr : ttexr + " ";
|
|
83
|
+
// result.temp = temp;
|
|
84
|
+
if (result.puretext != " ") temp[result.tag].text = result.puretext;
|
|
85
|
+
if (result.status) {
|
|
86
|
+
const ss = new Set();
|
|
87
|
+
const current = {};
|
|
88
|
+
s.forEach((e) => {
|
|
89
|
+
const hh = /<[a-zA-Z0-9]{1,}\s?/.exec(e)[0].substring(1);
|
|
90
|
+
if (ss.has(hh)) {
|
|
91
|
+
current[hh] = current[hh] + 1;
|
|
92
|
+
} else {
|
|
93
|
+
current[hh] = 0;
|
|
94
|
+
ss.add(hh);
|
|
95
|
+
}
|
|
96
|
+
temp = {
|
|
97
|
+
[result.tag]: { ...temp[result.tag], ...fromStr(e, current[hh]) },
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return temp;
|
|
102
|
+
}
|
|
103
|
+
function render(val = {}) {
|
|
104
|
+
let result = "";
|
|
105
|
+
Object.entries(val).forEach((e) => {
|
|
106
|
+
if (e[1].length) {
|
|
107
|
+
e[1].forEach((g) => {
|
|
108
|
+
result += render({ [e[0]]: g });
|
|
109
|
+
});
|
|
110
|
+
} else if (typeof e[1] == "object") {
|
|
111
|
+
e[0] = e[0].replace(/\s/g, "");
|
|
112
|
+
result += `<${e[0]}`;
|
|
113
|
+
const temp = [];
|
|
114
|
+
let inn = "";
|
|
115
|
+
Object.entries(e[1]).forEach((k) => {
|
|
116
|
+
if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html") {
|
|
117
|
+
result += ` ${k[0]}='${k[1].toString()}'`;
|
|
118
|
+
} else if (k[0] == "text")
|
|
119
|
+
inn = `${
|
|
120
|
+
typeof k[1] == "string"
|
|
121
|
+
? k[1]
|
|
122
|
+
.replace(/&/g, "&")
|
|
123
|
+
.replace(/</g, "<")
|
|
124
|
+
.replace(/>/g, ">")
|
|
125
|
+
.replace(/"/g, """)
|
|
126
|
+
.replace(/'/g, "'")
|
|
127
|
+
: k[1].toString()
|
|
128
|
+
}`;
|
|
129
|
+
else if (k[0] == "html") {
|
|
130
|
+
inn = `${
|
|
131
|
+
typeof k[1] != "function"
|
|
132
|
+
? k[1]
|
|
133
|
+
: (k[1] + "")
|
|
134
|
+
.replace(/.*{/, "")
|
|
135
|
+
.split("")
|
|
136
|
+
.reverse()
|
|
137
|
+
.join("")
|
|
138
|
+
.replace(/}/, "")
|
|
139
|
+
.split("")
|
|
140
|
+
.reverse()
|
|
141
|
+
.join("")
|
|
142
|
+
}`;
|
|
143
|
+
// typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
|
|
144
|
+
} else temp.push(k);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const stat = ![
|
|
148
|
+
"input",
|
|
149
|
+
"link",
|
|
150
|
+
"br",
|
|
151
|
+
"hr",
|
|
152
|
+
"area",
|
|
153
|
+
"base",
|
|
154
|
+
"br",
|
|
155
|
+
"col",
|
|
156
|
+
"embed",
|
|
157
|
+
"img",
|
|
158
|
+
"meta",
|
|
159
|
+
"param",
|
|
160
|
+
"source",
|
|
161
|
+
"track",
|
|
162
|
+
"wbr",
|
|
163
|
+
].includes(e[0]);
|
|
164
|
+
if (stat) {
|
|
165
|
+
result += ">";
|
|
166
|
+
result += inn;
|
|
167
|
+
for (let i of temp) {
|
|
168
|
+
result += render({ [i[0]]: i[1] });
|
|
169
|
+
}
|
|
170
|
+
result += `</${e[0]}>`;
|
|
171
|
+
} else {
|
|
172
|
+
if (!stat) result += "/>";
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
result = result.replace(/[\r\n\t]/gm, "");
|
|
177
|
+
result = result.replace(/\s{2,}/gm, " ");
|
|
178
|
+
return result;
|
|
179
|
+
}
|
package/index.js
CHANGED
|
@@ -14,9 +14,9 @@ function render(val = {}) {
|
|
|
14
14
|
const temp = [];
|
|
15
15
|
let inn = "";
|
|
16
16
|
Object.entries(e[1]).forEach((k) => {
|
|
17
|
-
if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html")
|
|
18
|
-
result += ` ${k[0]}='${k[1]}'`;
|
|
19
|
-
else if (k[0] == "text")
|
|
17
|
+
if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html") {
|
|
18
|
+
result += ` ${k[0]}='${k[1].toString()}'`;
|
|
19
|
+
} else if (k[0] == "text")
|
|
20
20
|
inn = `${
|
|
21
21
|
typeof k[1] == "string"
|
|
22
22
|
? k[1]
|
|
@@ -29,19 +29,49 @@ function render(val = {}) {
|
|
|
29
29
|
}`;
|
|
30
30
|
else if (k[0] == "html") {
|
|
31
31
|
inn = `${
|
|
32
|
-
typeof k[1] != "function"
|
|
32
|
+
typeof k[1] != "function"
|
|
33
|
+
? k[1]
|
|
34
|
+
: (k[1] + "")
|
|
35
|
+
.replace(/.*{/, "")
|
|
36
|
+
.split("")
|
|
37
|
+
.reverse()
|
|
38
|
+
.join("")
|
|
39
|
+
.replace(/}/, "")
|
|
40
|
+
.split("")
|
|
41
|
+
.reverse()
|
|
42
|
+
.join("")
|
|
33
43
|
}`;
|
|
34
|
-
inn
|
|
35
|
-
typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
|
|
44
|
+
// typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
|
|
36
45
|
} else temp.push(k);
|
|
37
46
|
});
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
|
|
48
|
+
const stat = ![
|
|
49
|
+
"input",
|
|
50
|
+
"link",
|
|
51
|
+
"br",
|
|
52
|
+
"hr",
|
|
53
|
+
"area",
|
|
54
|
+
"base",
|
|
55
|
+
"br",
|
|
56
|
+
"col",
|
|
57
|
+
"embed",
|
|
58
|
+
"img",
|
|
59
|
+
"meta",
|
|
60
|
+
"param",
|
|
61
|
+
"source",
|
|
62
|
+
"track",
|
|
63
|
+
"wbr",
|
|
64
|
+
].includes(e[0]);
|
|
65
|
+
if (stat) {
|
|
66
|
+
result += ">";
|
|
67
|
+
result += inn;
|
|
68
|
+
for (let i of temp) {
|
|
69
|
+
result += render({ [i[0]]: i[1] });
|
|
70
|
+
}
|
|
71
|
+
result += `</${e[0]}>`;
|
|
72
|
+
} else {
|
|
73
|
+
if (!stat) result += "/>";
|
|
74
|
+
}
|
|
45
75
|
}
|
|
46
76
|
});
|
|
47
77
|
result = result.replace(/[\r\n\t]/gm, "");
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -15,6 +15,45 @@ Neobiz.js is pure in JS lang, transform Object into HTML
|
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
18
|
+
## Example with `vue` in browser
|
|
19
|
+
|
|
20
|
+
page.js
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { outFile } from "neobiz";
|
|
24
|
+
|
|
25
|
+
outFile("./welcome.html", {
|
|
26
|
+
div: {
|
|
27
|
+
script: [
|
|
28
|
+
{ src: "https://unpkg.com/vue@3/dist/vue.global.js" },
|
|
29
|
+
{ src: "https://unpkg.com/neobiz@1.4.0/browser.js" },
|
|
30
|
+
{ src: "./my-component.js" },
|
|
31
|
+
],
|
|
32
|
+
id: "app",
|
|
33
|
+
},
|
|
34
|
+
script: {
|
|
35
|
+
html: () => {
|
|
36
|
+
const { createApp } = Vue;
|
|
37
|
+
|
|
38
|
+
createApp(myComponent).mount("#app");
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
my-component.js
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
const myComponent = {
|
|
48
|
+
data() {
|
|
49
|
+
return { msg: "hi, this is example" };
|
|
50
|
+
},
|
|
51
|
+
template: render({ h1: { text: "{{msg}}" } }),
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
<br>
|
|
56
|
+
|
|
18
57
|
| function | params | return-type | description |
|
|
19
58
|
| -------- | ------------------------------------ | ----------- | -------------------------------- |
|
|
20
59
|
| render | content[object] | string | rendering to HTML |
|