neobiz 1.4.0 → 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 +108 -77
  2. package/index.js +6 -188
  3. package/package.json +1 -1
  4. package/readme.md +41 -63
package/browser.js CHANGED
@@ -1,9 +1,99 @@
1
1
  /** @format */
2
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
+ }
3
94
  function range(length, start = 0) {
4
95
  return [...Array(length)].map(() => start++);
5
96
  }
6
-
7
97
  function fromStr(letter, prefix = 0) {
8
98
  if (letter == "") return {};
9
99
 
@@ -100,80 +190,21 @@ function fromStr(letter, prefix = 0) {
100
190
  }
101
191
  return temp;
102
192
  }
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
193
 
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
- }
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,201 +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].toString()}'`;
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"
33
- ? k[1]
34
- : (k[1] + "")
35
- .replace(/.*{/, "")
36
- .split("")
37
- .reverse()
38
- .join("")
39
- .replace(/}/, "")
40
- .split("")
41
- .reverse()
42
- .join("")
43
- }`;
44
- // typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
45
- } else temp.push(k);
46
- });
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
- }
75
- }
76
- });
77
- result = result.replace(/[\r\n\t]/gm, "");
78
- result = result.replace(/\s{2,}/gm, " ");
79
- return result;
80
- }
81
- function range(length, start = 0) {
82
- return [...Array(length)].map(() => start++);
83
- }
84
4
 
85
5
  function outFile(source, data) {
86
6
  const fs = require("fs");
87
7
  (function () {
88
8
  const dir = path.dirname(path.resolve(source));
89
9
  if (!fs.existsSync(dir)) fs.mkdirSync(dir);
90
- fs.writeFileSync(source, render(data));
10
+ fs.writeFileSync(source, data.render);
91
11
  console.log(`Output file: ${source}`);
92
12
  })();
93
13
  }
94
- function fromStr(letter, prefix = 0) {
95
- if (letter == "") return {};
96
-
97
- letter = letter.replace(/[\r\n\t]/gm, "").replace(/>\s{2,}</gm, "><");
98
- const result = {};
99
-
100
- if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)?.[1]) {
101
- result.tag = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)[1];
102
-
103
- result.tag = result.tag + Array(prefix).fill(" ").join("");
104
-
105
- result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
106
14
 
107
- result.inner = letter
108
- .replace(result.head, "")
109
- .split("")
110
- .reverse()
111
- .join("")
112
- .replace(
113
- `</${result.tag.replace(Array(prefix).fill(" ").join(""), "")}>`
114
- .split("")
115
- .reverse()
116
- .join(""),
117
- ""
118
- )
119
- .split("")
120
- .reverse()
121
- .join("");
122
- } else {
123
- result.tag = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(letter)[1];
124
-
125
- result.tag = result.tag + Array(prefix).fill(" ").join("");
126
-
127
- result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
128
- result.inner = "";
129
- }
130
- result.props = result.head.match(/[a-zA-Z0-9]{1,}=(["']).*?\1/g);
131
-
132
- let temp = { [result.tag]: {} };
133
- if (result.props)
134
- result.attr = result.props.forEach((e) => {
135
- let y = e.replace(/.*=(['"])/, "");
136
- y = y.substring(0, y.length - 1);
137
- let w = e.substring(0, e.indexOf("="));
138
- temp[result.tag][w] = y;
139
- });
140
-
141
- let s = [];
142
- let ttexr = result.inner;
143
- while (
144
- /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr) ||
145
- /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)
146
- ) {
147
- let ss;
148
- if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)) {
149
- ss = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)[0];
150
- const h = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(ss)[1];
151
- const sss = ttexr.slice(ttexr.indexOf(ss));
152
- const f = {
153
- a: sss.replace("<" + h, "").indexOf(`</${h}>`),
154
- b: sss.replace("<" + h, "").indexOf(`<${h}`),
155
- };
156
-
157
- ss = f.a > f.b && f.b > -1 ? sss : ss;
158
- } else {
159
- ss = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)[0];
160
- }
161
- ttexr = ttexr.replace(ss, "");
162
- s.push(ss);
163
- }
164
- s.sort((a, b) => {
165
- const r = { a: result.inner.indexOf(a), b: result.inner.indexOf(b) };
166
- return r.a > r.b ? 1 : r.a < r.b ? -1 : 0;
167
- });
168
- result.status = s;
169
- result.puretext = ttexr ? ttexr : ttexr + " ";
170
- // result.temp = temp;
171
- if (result.puretext != " ") temp[result.tag].text = result.puretext;
172
- if (result.status) {
173
- const ss = new Set();
174
- const current = {};
175
- s.forEach((e) => {
176
- const hh = /<[a-zA-Z0-9]{1,}\s?/.exec(e)[0].substring(1);
177
- if (ss.has(hh)) {
178
- current[hh] = current[hh] + 1;
179
- } else {
180
- current[hh] = 0;
181
- ss.add(hh);
182
- }
183
- temp = {
184
- [result.tag]: { ...temp[result.tag], ...fromStr(e, current[hh]) },
185
- };
186
- });
187
- }
188
- return temp;
189
- }
190
- module.exports = {
191
- render,
192
- range,
193
- outFile,
194
- fromStr,
195
- partial: async (src) => {
196
- const { default: i } = await import(
197
- path.join("file:\\\\", process.cwd(), src)
198
- );
199
- return i;
15
+ Object.defineProperty({}.__proto__, "outFile", {
16
+ value: function (goes) {
17
+ return outFile(goes, this);
200
18
  },
201
- };
19
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neobiz",
3
- "version": "1.4.0",
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`
@@ -20,13 +19,13 @@ Neobiz.js is pure in JS lang, transform Object into HTML
20
19
  page.js
21
20
 
22
21
  ```js
23
- import { outFile } from "neobiz";
22
+ import "neobiz";
24
23
 
25
- outFile("./welcome.html", {
24
+ ({
26
25
  div: {
27
26
  script: [
28
27
  { src: "https://unpkg.com/vue@3/dist/vue.global.js" },
29
- { src: "https://unpkg.com/neobiz@1.4.0/browser.js" },
28
+ { src: "https://unpkg.com/neobiz@3.1.0/browser.js" },
30
29
  { src: "./my-component.js" },
31
30
  ],
32
31
  id: "app",
@@ -38,7 +37,7 @@ outFile("./welcome.html", {
38
37
  createApp(myComponent).mount("#app");
39
38
  },
40
39
  },
41
- });
40
+ }.outFile("./welcome.html"));
42
41
  ```
43
42
 
44
43
  my-component.js
@@ -48,19 +47,18 @@ const myComponent = {
48
47
  data() {
49
48
  return { msg: "hi, this is example" };
50
49
  },
51
- template: render({ h1: { text: "{{msg}}" } }),
50
+ template: { h1: { text: "{{msg}}" } }.render,
52
51
  };
53
52
  ```
54
53
 
55
54
  <br>
56
55
 
57
- | function | params | return-type | description |
58
- | -------- | ------------------------------------ | ----------- | -------------------------------- |
59
- | render | content[object] | string | rendering to HTML |
60
- | outFile | destination[string], content[object] | void | generate static HTML file |
61
- | range | length[int], start[int] | Array | shortcut for looping in a range |
62
- | partial | source[string] | AsyncFn | including other file |
63
- | fromStr | str[string] | Object | generate object from HTML string |
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 |
64
62
 
65
63
  #### Why use this?
66
64
 
@@ -73,15 +71,21 @@ const myComponent = {
73
71
 
74
72
  ## Basic syntax
75
73
 
76
- It's just an Object!
74
+ How to use? just import this:
77
75
 
78
76
  ```js
79
- {
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
+ ({
80
84
  // text property is same as innerText
81
85
  h1: {
82
86
  text: "Hello World!",
83
- }
84
- }
87
+ },
88
+ }.render);
85
89
  // <h1>Hello World!</h1>
86
90
  ```
87
91
 
@@ -194,17 +198,17 @@ Can't use array? just use this:
194
198
  }
195
199
  ```
196
200
 
197
- ### Components
201
+ ### Components / Partials
198
202
 
199
203
  ```js
200
204
  import header from "./header.v.js"; //function type
201
205
  import footer from "./footer.v.js"; //object type
202
206
 
203
- render({
207
+ {
204
208
  div: header({ user: "dim24" }),
205
209
  p: { text: "this is text" },
206
210
  div: footer,
207
- });
211
+ }.render;
208
212
  ```
209
213
 
210
214
  header.v.js
@@ -232,7 +236,7 @@ wrap the script with a function, or use string
232
236
  {
233
237
  button:{onclick:"sayHi(this)",id:"hi-button"},
234
238
  script:{
235
- html:()=>{
239
+ html(){
236
240
  document.getElementById("hi-button").innerText="Click me!";
237
241
 
238
242
  function sayHi(el){
@@ -244,32 +248,30 @@ wrap the script with a function, or use string
244
248
 
245
249
  ```
246
250
 
247
- ## fromStr
251
+ ## Repulse
248
252
 
249
- 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
250
254
 
251
- > 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
252
256
 
253
- - wrap all tag into single parent
254
- - use external script and css
255
- - 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!)
256
260
 
257
261
  I still try to improve this feature. **if you can provide some help** just tell me please... :)
258
262
 
259
263
  ```js
260
- const { fromStr, render } = require("./index");
261
-
262
- const { div } = fromStr(`
264
+ const { div } = `
263
265
  <div>
264
266
  <h1>Hello dims!</h1>
265
267
  </div>
266
- `);
268
+ `.repulse;
267
269
 
268
270
  div.id = "main";
269
271
  div.h1.class = "heading";
270
272
  div.p = { text: "this is a whole text" };
271
273
 
272
- console.log(render({ div }));
274
+ console.log({ div }.render);
273
275
  ```
274
276
 
275
277
  result
@@ -286,9 +288,7 @@ result
286
288
  use function
287
289
 
288
290
  ```js
289
- import { render } from "../index.js";
290
-
291
- console.log(render(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins")));
291
+ console.log(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins").render);
292
292
  ```
293
293
 
294
294
  or
@@ -296,7 +296,7 @@ or
296
296
  ```js
297
297
  const greeting = (user) => ({ h1: `Hello, ${user}` });
298
298
 
299
- console.log(render(greeting("Vins")));
299
+ console.log(greeting("Vins").render);
300
300
  ```
301
301
 
302
302
  ## Control Flow
@@ -354,11 +354,10 @@ IIFE switch-case
354
354
  range & map:
355
355
 
356
356
  ```js
357
- import { render, range } from "../index.js";
358
357
 
359
358
  {
360
359
  ul: {
361
- li: range(8, 1).map((item) => ({ text: "data-" + item })),
360
+ li: [].range(8, 1).map((item) => ({ text: "data-" + item })),
362
361
  },
363
362
  }
364
363
  ```
@@ -402,34 +401,12 @@ while
402
401
  }
403
402
  ```
404
403
 
405
- ## partial & templating
406
-
407
- ### partial
408
-
409
- make sure to default export as a function
410
-
411
- ```js
412
- import { render, partial } from "../index.js";
413
-
414
- {
415
- head: (await partial("./exported.v.js"))("example page"),
416
- h1: { text: "This is example" },
417
- }
418
- ```
419
-
420
- `/exported.v.js`
421
-
422
- ```js
423
- export default (title) => ({ title: { text: title } });
424
- ```
425
-
426
- ### templating
404
+ ## templating
427
405
 
428
406
  this example use single file usage. for modular usage leave to you, or just read the example dir
429
407
 
430
408
  ```js
431
- import { render } from "../index.js";
432
-
409
+ //template file
433
410
  const template = (content) => ({
434
411
  div: {
435
412
  class: "main wrapper",
@@ -437,6 +414,7 @@ const template = (content) => ({
437
414
  },
438
415
  });
439
416
 
417
+ //content file
440
418
  template({
441
419
  h1: { text: "Hello World!" },
442
420
  });