neobiz 3.1.0 → 3.3.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 CHANGED
@@ -5,7 +5,7 @@ function render(val = {}) {
5
5
  let result = "";
6
6
  try {
7
7
  Object.entries(val).forEach((e) => {
8
- if (e[1].length) {
8
+ if (Array.isArray(e[1])) {
9
9
  e[1].forEach((g) => {
10
10
  result += render({ [e[0]]: g });
11
11
  });
@@ -17,6 +17,17 @@ function render(val = {}) {
17
17
  Object.entries(e[1]).forEach((k) => {
18
18
  if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html") {
19
19
  result += ` ${k[0]}='${k[1].toString()}'`;
20
+ }
21
+ if (typeof k[1] == "function" && k[0] != "text" && k[0] != "html") {
22
+ result += ` ${k[0]}='${(k[1] + "")
23
+ .replace(/.*{/, "")
24
+ .split("")
25
+ .reverse()
26
+ .join("")
27
+ .replace(/}/, "")
28
+ .split("")
29
+ .reverse()
30
+ .join("")}'`;
20
31
  } else if (k[0] == "text")
21
32
  inn = `${
22
33
  typeof k[1] == "string"
@@ -83,7 +94,7 @@ function render(val = {}) {
83
94
  return result;
84
95
  }
85
96
  function rdr(val = {}) {
86
- let result = "";
97
+ let result = "<!DOCTYPE html>";
87
98
  if (Array.isArray(val)) {
88
99
  val.forEach((value) => {
89
100
  result += render(value);
@@ -193,7 +204,40 @@ function fromStr(letter, prefix = 0) {
193
204
 
194
205
  Object.defineProperty({}.__proto__, "render", {
195
206
  get: function () {
196
- return rdr(this);
207
+ return "<!DOCTYPE html>" + rdr(this);
208
+ },
209
+ });
210
+ Object.defineProperty({}.__proto__, "css", {
211
+ get: function () {
212
+ let y = "";
213
+ Object.entries(this).forEach((item) => {
214
+ y += `${item[0]}{`;
215
+ Object.entries(item[1]).forEach((itm) => {
216
+ y += `${itm[0].replace(/[A-Z]/g, (m) => "-" + m.toLowerCase())}${
217
+ typeof itm[1] == "object" ? "" : ":"
218
+ }${typeof itm[1] == "object" ? "{" + itm[1].cssText + "}" : itm[1]}${
219
+ typeof itm[1] == "object" ? "" : ";"
220
+ }`;
221
+ });
222
+ y += "}";
223
+ });
224
+ return { text: y };
225
+ },
226
+ });
227
+ Object.defineProperty({}.__proto__, "cssText", {
228
+ get: function () {
229
+ let y = "";
230
+ Object.entries(this).forEach((itm) => {
231
+ y += `${itm[0].replace(/[A-Z]/g, (m) => "-" + m.toLowerCase())}:${
232
+ itm[1]
233
+ };`;
234
+ });
235
+ return y;
236
+ },
237
+ });
238
+ Object.defineProperty({}.__proto__, "neobiz", {
239
+ get: function () {
240
+ return { render, fromStr, range };
197
241
  },
198
242
  });
199
243
 
@@ -0,0 +1,25 @@
1
+ /** @format */
2
+
3
+ //use object literal
4
+
5
+ import "../index.js";
6
+
7
+ console.log(
8
+ {
9
+ h1: { text: "Hello World!" },
10
+ }.render
11
+ );
12
+
13
+ //output: <h1>Hello World!</h1>
14
+
15
+ // output static file
16
+ console.log(
17
+ {
18
+ h1: { text: "Hello World!" },
19
+ }.outFile("./basic_syntax.html:")
20
+ );
21
+
22
+ // properties
23
+ const page = {
24
+ h1: { class: "heading", id: "main", text: "Hello World!" },
25
+ }.render;
@@ -0,0 +1,56 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ const status = false;
6
+
7
+ // ternary operator
8
+
9
+ console.log(
10
+ {
11
+ h1: status
12
+ ? { text: "Hello World!" }
13
+ : {
14
+ a: { href: "#", text: "You are offline!" },
15
+ },
16
+ }.render
17
+ //<h1><a href='#'>You are offline!</a></h1
18
+ );
19
+
20
+ // use IIFE
21
+
22
+ // IF
23
+ console.log(
24
+ {
25
+ h1: (() => {
26
+ let result;
27
+ if (status) result = { text: "Hello World!" };
28
+ else
29
+ result = {
30
+ a: { href: "#", text: "You are offline!" },
31
+ };
32
+ return result;
33
+ })(),
34
+ }.render
35
+ //<h1><a href='#'>You are offline!</a></h1
36
+ );
37
+
38
+ // SWITCH
39
+ console.log(
40
+ {
41
+ h1: (() => {
42
+ let result;
43
+ switch (status) {
44
+ case true:
45
+ result = { text: "Hello World!" };
46
+ break;
47
+ case false:
48
+ result = {
49
+ a: { href: "#", text: "You are offline!" },
50
+ };
51
+ }
52
+ return result;
53
+ })(),
54
+ }.render
55
+ //<h1><a href='#'>You are offline!</a></h1
56
+ );
@@ -0,0 +1,3 @@
1
+ /** @format */
2
+
3
+ export default (title) => ({ title: { text: title } });
@@ -0,0 +1,27 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ const temp = `
6
+ <div id="first">
7
+ <ul id="outer">
8
+ <li>1</li>
9
+ <li>2</li>
10
+ </ul>
11
+ <div id="inner-div">
12
+ <ul id="inner-ul">
13
+ <li>3</li>
14
+ <li>4</li>
15
+ </ul>
16
+ </div>
17
+ </div>
18
+ `;
19
+
20
+ const { div } = temp.repulse;
21
+
22
+ div.ul.li.text = "data-1";
23
+ div.div.ul.class = "inner";
24
+
25
+ ({ div }.outFile("./temp.html"));
26
+
27
+ console.dir({ div }, { depth: null });
@@ -0,0 +1,3 @@
1
+ /** @format */
2
+
3
+ import "./basic_syntax.v.js";
@@ -0,0 +1,15 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ console.log(
6
+ {
7
+ h1: { text: "Hello World!<b>this is text</b>" },
8
+ ul: { html: "<li>Text hello</li>" },
9
+ }.render
10
+ );
11
+
12
+ ({
13
+ h1: { text: "Hello World! <b>this is \"text\"&'blue'</b>" },
14
+ ul: { html: "<li>Text hello</li>" },
15
+ }.outFile("./select.html"));
@@ -0,0 +1,18 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ ({
6
+ button: {
7
+ text: "click me",
8
+ onclick() {
9
+ this.innerText = "hi!";
10
+ },
11
+ id: "example",
12
+ },
13
+ script: {
14
+ html() {
15
+ document.getElementById("example").innerText = "click me!";
16
+ },
17
+ },
18
+ }.outFile("./temp.html"));
@@ -0,0 +1,53 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ // use range @param= length, startFrom
6
+
7
+ console.log(
8
+ {
9
+ ul: {
10
+ li: [].range(8, 1).map((item) => ({ text: "data-" + item })),
11
+ },
12
+ }.render
13
+ //<ul><li>data-1</li><li>data-2</li><li>data-3</li><li>data-4</li><li>data-5</li><li>data-6</li><li>data-7</li><li>data-8</li></ul>
14
+ );
15
+
16
+ // map
17
+ console.log(
18
+ {
19
+ ul: {
20
+ li: ["grape", "banana", "apple"].map((text) => ({ text })),
21
+ },
22
+ }.render
23
+ //<ul><li>grape</li><li>banana</li><li>apple</li></ul>
24
+ );
25
+
26
+ // for
27
+ console.log(
28
+ {
29
+ ul: {
30
+ li: (() => {
31
+ const result = [];
32
+ for (let i = 1; i < 9; i++) result.push({ text: "data-" + i });
33
+ return result;
34
+ })(),
35
+ },
36
+ }.render
37
+ //<ul><li>data-1</li><li>data-2</li><li>data-3</li><li>data-4</li><li>data-5</li><li>data-6</li><li>data-7</li><li>data-8</li></ul>
38
+ );
39
+
40
+ // while
41
+ console.log(
42
+ {
43
+ ul: {
44
+ li: (() => {
45
+ const result = [];
46
+ let i = 0;
47
+ while (++i < 9) result.push({ text: "data-" + i });
48
+ return result;
49
+ })(),
50
+ },
51
+ }.render
52
+ //<ul><li>data-1</li><li>data-2</li><li>data-3</li><li>data-4</li><li>data-5</li><li>data-6</li><li>data-7</li><li>data-8</li></ul>
53
+ );
@@ -0,0 +1,13 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ console.log(
6
+ {
7
+ ul: {
8
+ li: [{ text: "one" }, { text: "two" }, { text: "3" }],
9
+ },
10
+ }.render
11
+ );
12
+
13
+ // <ul><li>one</li><li>two</li><li>3</li></ul>
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "example",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "example of neobiz.js cases",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [
11
+ "neobiz.js",
12
+ "view",
13
+ "template"
14
+ ],
15
+ "author": "",
16
+ "license": "ISC"
17
+ }
@@ -0,0 +1,11 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+ import head from "./exported.v.js";
5
+ console.log(
6
+ render({
7
+ head: head("example page"),
8
+ h1: { text: "This is example" },
9
+ })
10
+ //<head><title>example page</title></head><h1>This is example</h1>
11
+ );
@@ -0,0 +1,17 @@
1
+ /** @format */
2
+
3
+ //use function, iife recommended :)
4
+
5
+ import "../index.js";
6
+
7
+ console.log(
8
+ // Ignore this
9
+ ((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins").render
10
+ //<h1>Hello, Vins</h1>
11
+ );
12
+
13
+ // or just use this below
14
+
15
+ const greeting = (user) => ({ h1: `Hello, ${user}` });
16
+
17
+ console.log(greeting("Vins").render);
@@ -0,0 +1,18 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ const template = (content) => ({
6
+ div: {
7
+ class: "main wrapper",
8
+ div: content,
9
+ },
10
+ });
11
+
12
+ console.log(
13
+ template({
14
+ h1: { text: "Hello World!" },
15
+ }).render
16
+ );
17
+
18
+ //<div class='main wrapper'><div><h1>Hello World!</h1></div></div>
package/index.js CHANGED
@@ -10,6 +10,7 @@ function outFile(source, data) {
10
10
  fs.writeFileSync(source, data.render);
11
11
  console.log(`Output file: ${source}`);
12
12
  })();
13
+ return data.render;
13
14
  }
14
15
 
15
16
  Object.defineProperty({}.__proto__, "outFile", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neobiz",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "description": "A simple view engine with JS features",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -1,8 +1,6 @@
1
1
  <!-- @format -->
2
2
 
3
- <!-- @format -->
4
-
5
- # Documentation Neobiz.js 3.1
3
+ # Documentation Neobiz.js 3.3
6
4
 
7
5
  > A simple view engine that has 100% JS features
8
6
 
@@ -53,12 +51,14 @@ const myComponent = {
53
51
 
54
52
  <br>
55
53
 
56
- | properties | context | return-type | description |
54
+ | properties | caller | return-type | description |
57
55
  | ---------- | ------------------------------------- | ----------- | -------------------------------- |
58
56
  | render | Object | string | rendering to HTML |
59
57
  | outFile | Object @params[destination] | void | generate static HTML file |
60
58
  | range | Array @params length[int], start[int] | Array | shortcut for looping in a range |
61
59
  | repulse | String | Object | generate object from HTML string |
60
+ | css | Object | Object | generate css from object |
61
+ | cssText | Object | String | inline css |
62
62
 
63
63
  #### Why use this?
64
64
 
@@ -80,12 +80,12 @@ import "neobiz";
80
80
  It's just an Object! `wrap in () to create a statement or just assign to a var`
81
81
 
82
82
  ```js
83
- ({
83
+ {
84
84
  // text property is same as innerText
85
85
  h1: {
86
86
  text: "Hello World!",
87
87
  },
88
- }.render);
88
+ }.render;
89
89
  // <h1>Hello World!</h1>
90
90
  ```
91
91
 
@@ -226,6 +226,59 @@ export default {
226
226
  };
227
227
  ```
228
228
 
229
+ ### Style
230
+
231
+ use camel case to css properties
232
+
233
+ #### inline CSS:
234
+
235
+ ```js
236
+ {
237
+ h1:{
238
+ text:"Hello World",
239
+ style:{
240
+ color:"#1B73F8",
241
+ fontSize:"150%"
242
+ }.cssText
243
+ }
244
+ }
245
+ ```
246
+
247
+ use style :
248
+
249
+ `global-style.js`:
250
+
251
+ ```js
252
+ export default {
253
+ h1: {
254
+ color: "red",
255
+ },
256
+ body: {
257
+ margin: "0px",
258
+ textAlign: "center",
259
+ },
260
+ };
261
+ ```
262
+
263
+ why should use object css rather than direct css?
264
+
265
+ we can easily modify the style
266
+
267
+ `main-page.js`:
268
+
269
+ ```js
270
+ import style from "./global-style.js";
271
+
272
+ style.body.margin = "12px";
273
+
274
+ {
275
+ style:style.css,
276
+ div:{
277
+ h1:{text:"Hello World!"}
278
+ }
279
+ }.render
280
+ ```
281
+
229
282
  ### Interacting with DOM
230
283
 
231
284
  use `script` to interact with browser with **html** property
@@ -248,6 +301,22 @@ wrap the script with a function, or use string
248
301
 
249
302
  ```
250
303
 
304
+ ### Event
305
+
306
+ you can add an event `this` keyword always refered to the node itseft
307
+
308
+ ```js
309
+ {
310
+ input:{type:"password",onkeypress(){
311
+ if(event.keyCode==13)
312
+ alert(this.value)
313
+ }},
314
+ button:{text:"Click me!",onclick(){
315
+ this.innerText = this.innerText=="clicked"?"Click me!":"clicked";
316
+ }}
317
+ }
318
+ ```
319
+
251
320
  ## Repulse
252
321
 
253
322
  If you already have an html file and want to modify it, or make it as a template, just use this