neobiz 1.2.2 → 1.2.5

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.
@@ -16,3 +16,8 @@ console.log(
16
16
  outFile("./basic.html", {
17
17
  h1: { text: "Hello World!" },
18
18
  });
19
+
20
+ // properties
21
+ render({
22
+ h1: { class: "heading", id: "main", text: "Hello World!" },
23
+ });
@@ -0,0 +1,15 @@
1
+ /** @format */
2
+
3
+ import { outFile, render } from "../index.js";
4
+
5
+ console.log(
6
+ render({
7
+ h1: { text: "Hello World!<b>this is text</b>" },
8
+ ul: { html: "<li>Text hello</li>" },
9
+ })
10
+ );
11
+
12
+ outFile("./select.html", {
13
+ h1: { text: "Hello World! <b>this is \"text\"&'blue'</b>" },
14
+ ul: { html: "<li>Text hello</li>" },
15
+ });
@@ -0,0 +1,16 @@
1
+ /** @format */
2
+
3
+ import { outFile } from "../index.js";
4
+
5
+ outFile("./temp.html", {
6
+ button: { onclick: "sayHi(this)", id: "example" },
7
+ script: {
8
+ html: function () {
9
+ document.getElementById("example").innerText = "click me!";
10
+
11
+ function sayHi(el) {
12
+ el.innerText = "hi!";
13
+ }
14
+ },
15
+ },
16
+ });
package/index.js CHANGED
@@ -15,11 +15,27 @@ function render(val = {}) {
15
15
  const temp = [];
16
16
  let inn = "";
17
17
  Object.entries(e[1]).forEach((k) => {
18
- if (typeof k[1] == "string" && k[0] != "text")
18
+ if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html")
19
19
  result += ` ${k[0]}='${k[1]}'`;
20
- else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`;
21
- else if (k[0] == "text") inn = `${k[1]}`;
22
- else temp.push(k);
20
+ // else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`;
21
+ else if (k[0] == "text")
22
+ inn = `${
23
+ typeof k[1] == "string"
24
+ ? k[1]
25
+ .replace(/&/g, "&amp;")
26
+ .replace(/</g, "&lt;")
27
+ .replace(/>/g, "&gt;")
28
+ .replace(/"/g, "&quot;")
29
+ .replace(/'/g, "&#39;")
30
+ : k[1].toString()
31
+ }`;
32
+ else if (k[0] == "html") {
33
+ inn = `${
34
+ typeof k[1] != "function" ? k[1] : (k[1] + "").replace(/.*{/, "")
35
+ }`;
36
+ inn =
37
+ typeof k[1] == "string" ? inn : inn.substring(0, inn.length - 1);
38
+ } else temp.push(k);
23
39
  });
24
40
  const stat = !["input", "link", "br", "hr"].includes(e[0]);
25
41
  if (stat) result += ">";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neobiz",
3
- "version": "1.2.2",
3
+ "version": "1.2.5",
4
4
  "description": "simple view engine yet powerful with javascript features",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,7 +8,10 @@
8
8
  },
9
9
  "keywords": [
10
10
  "view engine",
11
- "template engine",
11
+ "template",
12
+ "dom",
13
+ "html",
14
+ "",
12
15
  "view"
13
16
  ],
14
17
  "license": "ISC"
package/readme.md CHANGED
@@ -4,40 +4,40 @@
4
4
 
5
5
  > A simple view engine that has 100% JS features
6
6
 
7
- Neobiz.js is pure JS lang, transpile Object to HTML as string
7
+ [[Github]](https://github.com/harakun24/neobiz.js)
8
+ Neobiz.js is pure in JS lang, transform Object into HTML
8
9
 
9
10
  `read /example dir to understanding`
10
11
 
11
- [this link to usage in express.js](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
12
+ [link to usage in express.js](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
12
13
 
13
- **Features**
14
+ **Function**
14
15
 
15
16
  | function | params | return-type | description |
16
17
  | -------- | ---------------------------------- | ----------- | ------------------------------- |
17
18
  | render | content:object | string | rendering to HTML |
18
- | outFile | destination:string, content:object | | generate static HTML file |
19
+ | outFile | destination:string, content:object | void | generate static HTML file |
19
20
  | range | length:int, start:int | Array | shortcut for looping in a range |
20
21
  | partial | source:string | AsyncFn | including other file |
21
22
 
22
23
  #### Why use this?
23
24
 
24
- - One lang for all, backend-view-browser script all just use js!
25
- - **directly** interact data or Model with a view
26
- - Could generate static html file
27
- - Full control of serving the view. Such as encrypting the data before serve, or just like conditional and loop case
25
+ - Integrating back-end to view
26
+ - **directly** manipulation data or Model with a view
27
+ - Generate static html file
28
+ - Full control of view. Such as filtering, encrypting the data before serve, or just like conditional and loop case
28
29
  - Almost no need to learn anything. Just need to learn how to render
29
30
  - Limitless potential, because it's just js. Almost all library should be compatible
30
- - Just like JSON replace XML
31
31
 
32
32
  ## Basic syntax
33
33
 
34
- It's just Object in js!
34
+ It's just an Object!
35
35
 
36
36
  ```js
37
37
  {
38
- // text property is same as innerHTML
38
+ // text property is same as innerText
39
39
  h1: {
40
- text: "Hello World!";
40
+ text: "Hello World!",
41
41
  }
42
42
  }
43
43
  // <h1>Hello World!</h1>
@@ -47,28 +47,47 @@ properties dom
47
47
 
48
48
  ```js
49
49
  {
50
- // text property is same as innerHTML
51
50
  h1: {
52
51
  class:"heading",
53
52
  text: "Hello World!",
54
- style:"color: #DC1E1E"
53
+ style:"color: #DC1E1E",
55
54
  }
56
55
  }
57
56
  // <h1 class="heading" style="color: #DC1E1E">Hello World!</h1>
58
57
  ```
59
58
 
59
+ innerText & innerHTML:
60
+
61
+ ```js
62
+ {
63
+ h1: { text: "<b>this is a text</b>" },
64
+ ul: { html: "<li>this will be render as html</li>" },
65
+ }
66
+ ```
67
+
60
68
  nested dom:
61
69
 
62
70
  ```js
63
71
  {
64
72
  body: {
65
73
  h1: {
66
- text: "Hello World!";
74
+ text: "Hello World!",
67
75
  }
68
76
  }
69
77
  }
70
78
  ```
71
79
 
80
+ expression & operator
81
+
82
+ ```js
83
+ {
84
+ span: [
85
+ { text: 24 + 12 / 6 },
86
+ { text: !false },
87
+ ],
88
+ }
89
+ ```
90
+
72
91
  siblings dom:
73
92
 
74
93
  There is a case like this:
@@ -78,7 +97,7 @@ There is a case like this:
78
97
  <li>banana</li>
79
98
  ```
80
99
 
81
- Object can not use 2 keys with same name, instead use this:
100
+ Object can not use 2 keys with the same name
82
101
 
83
102
  ```js
84
103
  // Don't do this
@@ -108,17 +127,67 @@ What if there is somethings in the middle of list?
108
127
 
109
128
  Can't use array? just use this:
110
129
 
111
- > use string as key could distinc element just with a space
130
+ > use string as key could distinc an element with just a space
112
131
  > no need to worry, it will render without space
113
132
 
114
133
  ```js
115
134
  {
116
135
  li:{text:"orange"},
117
- p:{span:{text:"This is in the middle"}}
136
+ p:{span:{text:"This is in the middle"}},
118
137
  "li ":{text:"banana"},
119
138
  }
120
139
  ```
121
140
 
141
+ ### Components
142
+
143
+ ```js
144
+ import header from "./header.v.js"; //function type
145
+ import footer from "./footer.v.js"; //object type
146
+
147
+ render({
148
+ div: header({ user: "dim24" }),
149
+ p: { text: "this is text" },
150
+ div: footer,
151
+ });
152
+ ```
153
+
154
+ header.v.js
155
+
156
+ ```js
157
+ export default ({ user }) => ({ h1: { text: `Hi, ${user}` } });
158
+ ```
159
+
160
+ footer.v.js
161
+
162
+ ```js
163
+ export default {
164
+ script: { src: "/assets/js/main.js" },
165
+ span: { text: "this is footer" },
166
+ };
167
+ ```
168
+
169
+ ### Interacting with DOM
170
+
171
+ use `script` to interact with browser with **html** property
172
+
173
+ wrap the script with a function, or use string
174
+
175
+ ```js
176
+ {
177
+ button:{onclick:"sayHi(this)",id:"hi-button"},
178
+ script:{
179
+ html:()=>{
180
+ document.getElementById("hi-button").innerText="Click me!";
181
+
182
+ function sayHi(el){
183
+ el.innerText="Hi!";
184
+ }
185
+ }
186
+ }
187
+ }
188
+
189
+ ```
190
+
122
191
  ## Pass Value
123
192
 
124
193
  use function
@@ -263,7 +332,7 @@ export default (title) => ({ title: { text: title } });
263
332
 
264
333
  ### templating
265
334
 
266
- this example use single file usage. for modular usage leave to you
335
+ this example use single file usage. for modular usage leave to you, or just read the example dir
267
336
 
268
337
  ```js
269
338
  import { render } from "../index.js";
@@ -1 +0,0 @@
1
- <h1>Hello World!</h1>
package/minify.js DELETED
@@ -1,11 +0,0 @@
1
- /** @format */
2
- const fs = require("fs");
3
-
4
- fs.writeFileSync(
5
- "./neobiz.min.js",
6
- fs
7
- .readFileSync("./index.js")
8
- .toString()
9
- .replace(/[\r\n\t]/gm, "")
10
- .replace(/\s{2,}/gm, " ")
11
- );
package/neobiz.min.js DELETED
@@ -1 +0,0 @@
1
- /** @format */"use strict";const path = require("path");function render(val = {}) { let result = ""; Object.entries(val).forEach((e) => { if (e[1].length) { e[1].forEach((g) => { result += render({ [e[0]]: g }); }); } else if (typeof e[1] == "object") { e[0] = e[0].replace(" ", ""); result += `<${e[0]}`; const temp = []; let inn = ""; Object.entries(e[1]).forEach((k) => { if (typeof k[1] == "string" && k[0] != "text") result += ` ${k[0]}='${k[1]}'`; else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`; else if (k[0] == "text") inn = `${k[1]}`; else temp.push(k); }); const stat = !["input", "link", "br", "hr"].includes(e[0]); if (stat) result += ">"; result += inn; for (let i of temp) result += render({ [i[0]]: i[1] }); if (stat) result += `</${e[0]}>`; else result += "/>"; } else { } }); result = result.replace(/[\r\n\t]/gm, ""); result = result.replace(/\s{2,}/gm, " "); return result;}function range(length, start = 0) { return [...Array(length)].map(() => start++);}function outFile(source, data) { const fs = require("fs"); (function () { const dir = path.dirname(path.resolve(source)); if (!fs.existsSync(dir)) fs.mkdirSync(dir); fs.writeFileSync(source, render(data)); console.log(`Output file: ${source}`); })();}module.exports = { render, range, outFile, partial: async (src) => { console.log(path.join("file:\\\\", process.cwd(), src)); const { default: i } = await import( path.join("file:\\\\", process.cwd(), src) ); return i; },};