neobiz 1.3.9 → 3.1.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 +210 -0
  2. package/index.js +6 -176
  3. package/package.json +1 -1
  4. package/readme.md +75 -58
package/browser.js ADDED
@@ -0,0 +1,210 @@
1
+ /** @format */
2
+
3
+ "use strict";
4
+ function render(val = {}) {
5
+ let result = "";
6
+ try {
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(/\s/g, "");
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" && k[0] != "html") {
19
+ result += ` ${k[0]}='${k[1].toString()}'`;
20
+ } else if (k[0] == "text")
21
+ inn = `${
22
+ typeof k[1] == "string"
23
+ ? k[1]
24
+ .replace(/&/g, "&amp;")
25
+ .replace(/</g, "&lt;")
26
+ .replace(/>/g, "&gt;")
27
+ .replace(/"/g, "&quot;")
28
+ .replace(/'/g, "&#39;")
29
+ : k[1].toString()
30
+ }`;
31
+ else if (k[0] == "html") {
32
+ inn = `${
33
+ typeof k[1] != "function"
34
+ ? k[1]
35
+ : (k[1] + "")
36
+ .replace(/.*{/, "")
37
+ .split("")
38
+ .reverse()
39
+ .join("")
40
+ .replace(/}/, "")
41
+ .split("")
42
+ .reverse()
43
+ .join("")
44
+ }`;
45
+ // typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
46
+ } else temp.push(k);
47
+ });
48
+
49
+ const stat = ![
50
+ "input",
51
+ "link",
52
+ "br",
53
+ "hr",
54
+ "area",
55
+ "base",
56
+ "br",
57
+ "col",
58
+ "embed",
59
+ "img",
60
+ "meta",
61
+ "param",
62
+ "source",
63
+ "track",
64
+ "wbr",
65
+ ].includes(e[0]);
66
+ if (stat) {
67
+ result += ">";
68
+ result += inn;
69
+ for (let i of temp) {
70
+ result += render({ [i[0]]: i[1] });
71
+ }
72
+ result += `</${e[0]}>`;
73
+ } else {
74
+ if (!stat) result += "/>";
75
+ }
76
+ }
77
+ });
78
+ result = result.replace(/[\r\n\t]/gm, "");
79
+ result = result.replace(/\s{2,}/gm, " ");
80
+ } catch (e) {
81
+ result = `<p>Error: ${e.message}</p>`;
82
+ }
83
+ return result;
84
+ }
85
+ function rdr(val = {}) {
86
+ let result = "";
87
+ if (Array.isArray(val)) {
88
+ val.forEach((value) => {
89
+ result += render(value);
90
+ });
91
+ } else result = render(val);
92
+ return result;
93
+ }
94
+ function range(length, start = 0) {
95
+ return [...Array(length)].map(() => start++);
96
+ }
97
+ function fromStr(letter, prefix = 0) {
98
+ if (letter == "") return {};
99
+
100
+ letter = letter.replace(/[\r\n\t]/gm, "").replace(/>\s{2,}</gm, "><");
101
+ const result = {};
102
+
103
+ if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)?.[1]) {
104
+ result.tag = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)[1];
105
+
106
+ result.tag = result.tag + Array(prefix).fill(" ").join("");
107
+
108
+ result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
109
+
110
+ result.inner = letter
111
+ .replace(result.head, "")
112
+ .split("")
113
+ .reverse()
114
+ .join("")
115
+ .replace(
116
+ `</${result.tag.replace(Array(prefix).fill(" ").join(""), "")}>`
117
+ .split("")
118
+ .reverse()
119
+ .join(""),
120
+ ""
121
+ )
122
+ .split("")
123
+ .reverse()
124
+ .join("");
125
+ } else {
126
+ result.tag = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(letter)[1];
127
+
128
+ result.tag = result.tag + Array(prefix).fill(" ").join("");
129
+
130
+ result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
131
+ result.inner = "";
132
+ }
133
+ result.props = result.head.match(/[a-zA-Z0-9]{1,}=(["']).*?\1/g);
134
+
135
+ let temp = { [result.tag]: {} };
136
+ if (result.props)
137
+ result.attr = result.props.forEach((e) => {
138
+ let y = e.replace(/.*=(['"])/, "");
139
+ y = y.substring(0, y.length - 1);
140
+ let w = e.substring(0, e.indexOf("="));
141
+ temp[result.tag][w] = y;
142
+ });
143
+
144
+ let s = [];
145
+ let ttexr = result.inner;
146
+ while (
147
+ /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr) ||
148
+ /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)
149
+ ) {
150
+ let ss;
151
+ if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)) {
152
+ ss = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)[0];
153
+ const h = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(ss)[1];
154
+ const sss = ttexr.slice(ttexr.indexOf(ss));
155
+ const f = {
156
+ a: sss.replace("<" + h, "").indexOf(`</${h}>`),
157
+ b: sss.replace("<" + h, "").indexOf(`<${h}`),
158
+ };
159
+
160
+ ss = f.a > f.b && f.b > -1 ? sss : ss;
161
+ } else {
162
+ ss = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)[0];
163
+ }
164
+ ttexr = ttexr.replace(ss, "");
165
+ s.push(ss);
166
+ }
167
+ s.sort((a, b) => {
168
+ const r = { a: result.inner.indexOf(a), b: result.inner.indexOf(b) };
169
+ return r.a > r.b ? 1 : r.a < r.b ? -1 : 0;
170
+ });
171
+ result.status = s;
172
+ result.puretext = ttexr ? ttexr : ttexr + " ";
173
+ // result.temp = temp;
174
+ if (result.puretext != " ") temp[result.tag].text = result.puretext;
175
+ if (result.status) {
176
+ const ss = new Set();
177
+ const current = {};
178
+ s.forEach((e) => {
179
+ const hh = /<[a-zA-Z0-9]{1,}\s?/.exec(e)[0].substring(1);
180
+ if (ss.has(hh)) {
181
+ current[hh] = current[hh] + 1;
182
+ } else {
183
+ current[hh] = 0;
184
+ ss.add(hh);
185
+ }
186
+ temp = {
187
+ [result.tag]: { ...temp[result.tag], ...fromStr(e, current[hh]) },
188
+ };
189
+ });
190
+ }
191
+ return temp;
192
+ }
193
+
194
+ Object.defineProperty({}.__proto__, "render", {
195
+ get: function () {
196
+ return rdr(this);
197
+ },
198
+ });
199
+
200
+ Object.defineProperty("".__proto__, "repulse", {
201
+ get: function () {
202
+ return fromStr(this);
203
+ },
204
+ });
205
+
206
+ Object.defineProperty([].__proto__, "range", {
207
+ value: function (range2, start = 0) {
208
+ return range(range2, start);
209
+ },
210
+ });
package/index.js CHANGED
@@ -1,189 +1,19 @@
1
1
  /** @format */
2
- "use strict";
2
+ require("./browser");
3
3
  const path = require("path");
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(/\s/g, "");
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" && k[0] != "html")
18
- result += ` ${k[0]}='${k[1]}'`;
19
- else if (k[0] == "text")
20
- inn = `${
21
- typeof k[1] == "string"
22
- ? k[1]
23
- .replace(/&/g, "&amp;")
24
- .replace(/</g, "&lt;")
25
- .replace(/>/g, "&gt;")
26
- .replace(/"/g, "&quot;")
27
- .replace(/'/g, "&#39;")
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);
37
- });
38
- const stat = ![
39
- "input",
40
- "link",
41
- "br",
42
- "hr",
43
- "area",
44
- "base",
45
- "br",
46
- "col",
47
- "embed",
48
- "img",
49
- "meta",
50
- "param",
51
- "source",
52
- "track",
53
- "wbr",
54
- ].includes(e[0]);
55
- result += inn;
56
- for (let i of temp)
57
- if (stat) {
58
- result += ">";
59
- result += render({ [i[0]]: i[1] });
60
- result += `</${e[0]}>`;
61
- }
62
- if (!stat) result += "/>";
63
- }
64
- });
65
- result = result.replace(/[\r\n\t]/gm, "");
66
- result = result.replace(/\s{2,}/gm, " ");
67
- return result;
68
- }
69
- function range(length, start = 0) {
70
- return [...Array(length)].map(() => start++);
71
- }
72
4
 
73
5
  function outFile(source, data) {
74
6
  const fs = require("fs");
75
7
  (function () {
76
8
  const dir = path.dirname(path.resolve(source));
77
9
  if (!fs.existsSync(dir)) fs.mkdirSync(dir);
78
- fs.writeFileSync(source, render(data));
10
+ fs.writeFileSync(source, data.render);
79
11
  console.log(`Output file: ${source}`);
80
12
  })();
81
13
  }
82
- function fromStr(letter, prefix = 0) {
83
- if (letter == "") return {};
84
-
85
- letter = letter.replace(/[\r\n\t]/gm, "").replace(/>\s{2,}</gm, "><");
86
- const result = {};
87
-
88
- if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)?.[1]) {
89
- result.tag = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)[1];
90
-
91
- result.tag = result.tag + Array(prefix).fill(" ").join("");
92
-
93
- result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
94
14
 
95
- result.inner = letter
96
- .replace(result.head, "")
97
- .split("")
98
- .reverse()
99
- .join("")
100
- .replace(
101
- `</${result.tag.replace(Array(prefix).fill(" ").join(""), "")}>`
102
- .split("")
103
- .reverse()
104
- .join(""),
105
- ""
106
- )
107
- .split("")
108
- .reverse()
109
- .join("");
110
- } else {
111
- result.tag = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(letter)[1];
112
-
113
- result.tag = result.tag + Array(prefix).fill(" ").join("");
114
-
115
- result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
116
- result.inner = "";
117
- }
118
- result.props = result.head.match(/[a-zA-Z0-9]{1,}=(["']).*?\1/g);
119
-
120
- let temp = { [result.tag]: {} };
121
- if (result.props)
122
- result.attr = result.props.forEach((e) => {
123
- let y = e.replace(/.*=(['"])/, "");
124
- y = y.substring(0, y.length - 1);
125
- let w = e.substring(0, e.indexOf("="));
126
- temp[result.tag][w] = y;
127
- });
128
-
129
- let s = [];
130
- let ttexr = result.inner;
131
- while (
132
- /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr) ||
133
- /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)
134
- ) {
135
- let ss;
136
- if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)) {
137
- ss = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)[0];
138
- const h = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(ss)[1];
139
- const sss = ttexr.slice(ttexr.indexOf(ss));
140
- const f = {
141
- a: sss.replace("<" + h, "").indexOf(`</${h}>`),
142
- b: sss.replace("<" + h, "").indexOf(`<${h}`),
143
- };
144
-
145
- ss = f.a > f.b && f.b > -1 ? sss : ss;
146
- } else {
147
- ss = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)[0];
148
- }
149
- ttexr = ttexr.replace(ss, "");
150
- s.push(ss);
151
- }
152
- s.sort((a, b) => {
153
- const r = { a: result.inner.indexOf(a), b: result.inner.indexOf(b) };
154
- return r.a > r.b ? 1 : r.a < r.b ? -1 : 0;
155
- });
156
- result.status = s;
157
- result.puretext = ttexr ? ttexr : ttexr + " ";
158
- // result.temp = temp;
159
- if (result.puretext != " ") temp[result.tag].text = result.puretext;
160
- if (result.status) {
161
- const ss = new Set();
162
- const current = {};
163
- s.forEach((e) => {
164
- const hh = /<[a-zA-Z0-9]{1,}\s?/.exec(e)[0].substring(1);
165
- if (ss.has(hh)) {
166
- current[hh] = current[hh] + 1;
167
- } else {
168
- current[hh] = 0;
169
- ss.add(hh);
170
- }
171
- temp = {
172
- [result.tag]: { ...temp[result.tag], ...fromStr(e, current[hh]) },
173
- };
174
- });
175
- }
176
- return temp;
177
- }
178
- module.exports = {
179
- render,
180
- range,
181
- outFile,
182
- fromStr,
183
- partial: async (src) => {
184
- const { default: i } = await import(
185
- path.join("file:\\\\", process.cwd(), src)
186
- );
187
- return i;
15
+ Object.defineProperty({}.__proto__, "outFile", {
16
+ value: function (goes) {
17
+ return outFile(goes, this);
188
18
  },
189
- };
19
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neobiz",
3
- "version": "1.3.9",
3
+ "version": "3.1.0",
4
4
  "description": "A simple view engine with JS features",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -2,11 +2,10 @@
2
2
 
3
3
  <!-- @format -->
4
4
 
5
- # Documentation Neobiz.js
5
+ # Documentation Neobiz.js 3.1
6
6
 
7
7
  > A simple view engine that has 100% JS features
8
8
 
9
- [[Github example]](https://github.com/harakun24/assets-neobiz)
10
9
  Neobiz.js is pure in JS lang, transform Object into HTML
11
10
 
12
11
  `read /example dir to understanding`
@@ -15,13 +14,51 @@ Neobiz.js is pure in JS lang, transform Object into HTML
15
14
 
16
15
  ---
17
16
 
18
- | function | params | return-type | description |
19
- | -------- | ------------------------------------ | ----------- | -------------------------------- |
20
- | render | content[object] | string | rendering to HTML |
21
- | outFile | destination[string], content[object] | void | generate static HTML file |
22
- | range | length[int], start[int] | Array | shortcut for looping in a range |
23
- | partial | source[string] | AsyncFn | including other file |
24
- | fromStr | str[string] | Object | generate object from HTML string |
17
+ ## Example with `vue` in browser
18
+
19
+ page.js
20
+
21
+ ```js
22
+ import "neobiz";
23
+
24
+ ({
25
+ div: {
26
+ script: [
27
+ { src: "https://unpkg.com/vue@3/dist/vue.global.js" },
28
+ { src: "https://unpkg.com/neobiz@3.1.0/browser.js" },
29
+ { src: "./my-component.js" },
30
+ ],
31
+ id: "app",
32
+ },
33
+ script: {
34
+ html: () => {
35
+ const { createApp } = Vue;
36
+
37
+ createApp(myComponent).mount("#app");
38
+ },
39
+ },
40
+ }.outFile("./welcome.html"));
41
+ ```
42
+
43
+ my-component.js
44
+
45
+ ```js
46
+ const myComponent = {
47
+ data() {
48
+ return { msg: "hi, this is example" };
49
+ },
50
+ template: { h1: { text: "{{msg}}" } }.render,
51
+ };
52
+ ```
53
+
54
+ <br>
55
+
56
+ | properties | context | return-type | description |
57
+ | ---------- | ------------------------------------- | ----------- | -------------------------------- |
58
+ | render | Object | string | rendering to HTML |
59
+ | outFile | Object @params[destination] | void | generate static HTML file |
60
+ | range | Array @params length[int], start[int] | Array | shortcut for looping in a range |
61
+ | repulse | String | Object | generate object from HTML string |
25
62
 
26
63
  #### Why use this?
27
64
 
@@ -34,15 +71,21 @@ Neobiz.js is pure in JS lang, transform Object into HTML
34
71
 
35
72
  ## Basic syntax
36
73
 
37
- It's just an Object!
74
+ How to use? just import this:
38
75
 
39
76
  ```js
40
- {
77
+ import "neobiz";
78
+ ```
79
+
80
+ It's just an Object! `wrap in () to create a statement or just assign to a var`
81
+
82
+ ```js
83
+ ({
41
84
  // text property is same as innerText
42
85
  h1: {
43
86
  text: "Hello World!",
44
- }
45
- }
87
+ },
88
+ }.render);
46
89
  // <h1>Hello World!</h1>
47
90
  ```
48
91
 
@@ -155,17 +198,17 @@ Can't use array? just use this:
155
198
  }
156
199
  ```
157
200
 
158
- ### Components
201
+ ### Components / Partials
159
202
 
160
203
  ```js
161
204
  import header from "./header.v.js"; //function type
162
205
  import footer from "./footer.v.js"; //object type
163
206
 
164
- render({
207
+ {
165
208
  div: header({ user: "dim24" }),
166
209
  p: { text: "this is text" },
167
210
  div: footer,
168
- });
211
+ }.render;
169
212
  ```
170
213
 
171
214
  header.v.js
@@ -193,7 +236,7 @@ wrap the script with a function, or use string
193
236
  {
194
237
  button:{onclick:"sayHi(this)",id:"hi-button"},
195
238
  script:{
196
- html:()=>{
239
+ html(){
197
240
  document.getElementById("hi-button").innerText="Click me!";
198
241
 
199
242
  function sayHi(el){
@@ -205,32 +248,30 @@ wrap the script with a function, or use string
205
248
 
206
249
  ```
207
250
 
208
- ## fromStr
251
+ ## Repulse
209
252
 
210
- If you already have an html file and want to modify it, or make it as a template, just use `fromStr` function
253
+ If you already have an html file and want to modify it, or make it as a template, just use this
211
254
 
212
- > this function is still has some issues, but for simple html fromStr is work fine!
255
+ > this function is still has a lot issues, use only in a simple case
213
256
 
214
- - wrap all tag into single parent
215
- - use external script and css
216
- - repetitive nested could caused weird behavior (help me, please!)
257
+ - need to wrap all tag into a single parent
258
+ - recommended to use external script and css
259
+ - repetitive / nested could caused weird behavior (help me, please!)
217
260
 
218
261
  I still try to improve this feature. **if you can provide some help** just tell me please... :)
219
262
 
220
263
  ```js
221
- const { fromStr, render } = require("./index");
222
-
223
- const { div } = fromStr(`
264
+ const { div } = `
224
265
  <div>
225
266
  <h1>Hello dims!</h1>
226
267
  </div>
227
- `);
268
+ `.repulse;
228
269
 
229
270
  div.id = "main";
230
271
  div.h1.class = "heading";
231
272
  div.p = { text: "this is a whole text" };
232
273
 
233
- console.log(render({ div }));
274
+ console.log({ div }.render);
234
275
  ```
235
276
 
236
277
  result
@@ -247,9 +288,7 @@ result
247
288
  use function
248
289
 
249
290
  ```js
250
- import { render } from "../index.js";
251
-
252
- console.log(render(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins")));
291
+ console.log(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins").render);
253
292
  ```
254
293
 
255
294
  or
@@ -257,7 +296,7 @@ or
257
296
  ```js
258
297
  const greeting = (user) => ({ h1: `Hello, ${user}` });
259
298
 
260
- console.log(render(greeting("Vins")));
299
+ console.log(greeting("Vins").render);
261
300
  ```
262
301
 
263
302
  ## Control Flow
@@ -315,11 +354,10 @@ IIFE switch-case
315
354
  range & map:
316
355
 
317
356
  ```js
318
- import { render, range } from "../index.js";
319
357
 
320
358
  {
321
359
  ul: {
322
- li: range(8, 1).map((item) => ({ text: "data-" + item })),
360
+ li: [].range(8, 1).map((item) => ({ text: "data-" + item })),
323
361
  },
324
362
  }
325
363
  ```
@@ -363,34 +401,12 @@ while
363
401
  }
364
402
  ```
365
403
 
366
- ## partial & templating
367
-
368
- ### partial
369
-
370
- make sure to default export as a function
371
-
372
- ```js
373
- import { render, partial } from "../index.js";
374
-
375
- {
376
- head: (await partial("./exported.v.js"))("example page"),
377
- h1: { text: "This is example" },
378
- }
379
- ```
380
-
381
- `/exported.v.js`
382
-
383
- ```js
384
- export default (title) => ({ title: { text: title } });
385
- ```
386
-
387
- ### templating
404
+ ## templating
388
405
 
389
406
  this example use single file usage. for modular usage leave to you, or just read the example dir
390
407
 
391
408
  ```js
392
- import { render } from "../index.js";
393
-
409
+ //template file
394
410
  const template = (content) => ({
395
411
  div: {
396
412
  class: "main wrapper",
@@ -398,6 +414,7 @@ const template = (content) => ({
398
414
  },
399
415
  });
400
416
 
417
+ //content file
401
418
  template({
402
419
  h1: { text: "Hello World!" },
403
420
  });