neobiz 1.3.9 → 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.
Files changed (4) hide show
  1. package/browser.js +179 -0
  2. package/index.js +24 -12
  3. package/package.json +1 -1
  4. 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, "&amp;")
123
+ .replace(/</g, "&lt;")
124
+ .replace(/>/g, "&gt;")
125
+ .replace(/"/g, "&quot;")
126
+ .replace(/'/g, "&#39;")
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,12 +29,22 @@ function render(val = {}) {
29
29
  }`;
30
30
  else if (k[0] == "html") {
31
31
  inn = `${
32
- typeof k[1] != "function" ? k[1] : (k[1] + "").replace(/.*{/, "")
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
  });
47
+
38
48
  const stat = ![
39
49
  "input",
40
50
  "link",
@@ -52,14 +62,16 @@ function render(val = {}) {
52
62
  "track",
53
63
  "wbr",
54
64
  ].includes(e[0]);
55
- result += inn;
56
- for (let i of temp)
57
- if (stat) {
58
- result += ">";
65
+ if (stat) {
66
+ result += ">";
67
+ result += inn;
68
+ for (let i of temp) {
59
69
  result += render({ [i[0]]: i[1] });
60
- result += `</${e[0]}>`;
61
70
  }
62
- if (!stat) result += "/>";
71
+ result += `</${e[0]}>`;
72
+ } else {
73
+ if (!stat) result += "/>";
74
+ }
63
75
  }
64
76
  });
65
77
  result = result.replace(/[\r\n\t]/gm, "");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neobiz",
3
- "version": "1.3.9",
3
+ "version": "1.4.0",
4
4
  "description": "A simple view engine with JS features",
5
5
  "main": "index.js",
6
6
  "scripts": {
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 |