neobiz 3.1.0 → 3.8.1

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,42 @@ 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
+
239
+ Object.defineProperty({}.__proto__, "alter", {
240
+ value: function (fn) {
241
+ fn(this);
242
+ return this;
197
243
  },
198
244
  });
199
245
 
@@ -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,28 @@
1
+ /** @format */
2
+
3
+ import "../index.js";
4
+
5
+ // inline
6
+ console.log(
7
+ {
8
+ h1: {
9
+ text: "this is text",
10
+ style: { color: "blue", fontSize: "140%" }.cssText,
11
+ },
12
+ }.render
13
+ );
14
+
15
+ // tag style
16
+ console.log(
17
+ {
18
+ style: {
19
+ h1: {
20
+ // something
21
+ },
22
+ body: {
23
+ margin: "0px",
24
+ padding: "0px",
25
+ },
26
+ }.css,
27
+ }.render
28
+ );
@@ -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.8.1",
4
4
  "description": "A simple view engine with JS features",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,7 +11,9 @@
11
11
  "template",
12
12
  "dom",
13
13
  "html",
14
- "express"
14
+ "express",
15
+ "front-end",
16
+ "layout"
15
17
  ],
16
18
  "license": "MIT"
17
19
  }
package/readme.md CHANGED
@@ -1,13 +1,17 @@
1
1
  <!-- @format -->
2
2
 
3
- <!-- @format -->
4
-
5
- # Documentation Neobiz.js 3.1
3
+ # Documentation Neobiz.js 3.8
6
4
 
7
5
  > A simple view engine that has 100% JS features
8
6
 
9
7
  Neobiz.js is pure in JS lang, transform Object into HTML
10
8
 
9
+ new features:
10
+
11
+ - add eventHandler
12
+ - style/css
13
+ - altering object
14
+
11
15
  `read /example dir to understanding`
12
16
 
13
17
  [link to usage in express.js](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
@@ -31,7 +35,7 @@ import "neobiz";
31
35
  id: "app",
32
36
  },
33
37
  script: {
34
- html: () => {
38
+ html() {
35
39
  const { createApp } = Vue;
36
40
 
37
41
  createApp(myComponent).mount("#app");
@@ -53,12 +57,15 @@ const myComponent = {
53
57
 
54
58
  <br>
55
59
 
56
- | properties | context | return-type | description |
60
+ | properties | caller | return-type | description |
57
61
  | ---------- | ------------------------------------- | ----------- | -------------------------------- |
58
62
  | render | Object | string | rendering to HTML |
59
- | outFile | Object @params[destination] | void | generate static HTML file |
63
+ | outFile | Object @params destination[string] | void | generate static HTML file |
60
64
  | range | Array @params length[int], start[int] | Array | shortcut for looping in a range |
61
65
  | repulse | String | Object | generate object from HTML string |
66
+ | css | Object | Object | generate css from object |
67
+ | cssText | Object | Object | inline css |
68
+ | alter | Object @params cb[function] | Object | altering object before rendering |
62
69
 
63
70
  #### Why use this?
64
71
 
@@ -80,12 +87,12 @@ import "neobiz";
80
87
  It's just an Object! `wrap in () to create a statement or just assign to a var`
81
88
 
82
89
  ```js
83
- ({
90
+ {
84
91
  // text property is same as innerText
85
92
  h1: {
86
93
  text: "Hello World!",
87
94
  },
88
- }.render);
95
+ }.render;
89
96
  // <h1>Hello World!</h1>
90
97
  ```
91
98
 
@@ -198,6 +205,22 @@ Can't use array? just use this:
198
205
  }
199
206
  ```
200
207
 
208
+ ### Modify an Object
209
+
210
+ use `alter` properties, with function as params. no need to return a value
211
+
212
+ ```js
213
+ const y = {
214
+ h1: { text: "Hello World!" },
215
+ p: { text: "this is a p" }.alter((e) => (e.class = "lorem")),
216
+ }.alter((e) => {
217
+ e.h1.text = "thi is altered text";
218
+ e.h1.style = { color: "red", fontSize: "150%" }.cssText;
219
+ });
220
+
221
+ console.log(y.render);
222
+ ```
223
+
201
224
  ### Components / Partials
202
225
 
203
226
  ```js
@@ -226,6 +249,80 @@ export default {
226
249
  };
227
250
  ```
228
251
 
252
+ ### Style
253
+
254
+ use camel case to css properties
255
+
256
+ #### inline CSS:
257
+
258
+ ```js
259
+ {
260
+ h1:{
261
+ text:"Hello World",
262
+ style:{
263
+ color:"#1B73F8",
264
+ fontSize:"150%"
265
+ }.cssText
266
+ }
267
+ }
268
+ ```
269
+
270
+ use style :
271
+
272
+ `global-style.js`:
273
+
274
+ ```js
275
+ export default {
276
+ h1: {
277
+ color: "red",
278
+ },
279
+ body: {
280
+ margin: "0px",
281
+ textAlign: "center",
282
+ },
283
+ };
284
+ ```
285
+
286
+ why should use object css rather than direct css?
287
+
288
+ we can easily modify the style
289
+
290
+ `main-page.js`:
291
+
292
+ ```js
293
+ import style from "./global-style.js";
294
+
295
+ style.body.margin = "12px";
296
+
297
+ {
298
+ style:style.css,
299
+ div:{
300
+ h1:{text:"Hello World!"}
301
+ }
302
+ }.render
303
+ ```
304
+
305
+ reusable style / mixin
306
+
307
+ ```js
308
+ const flex = (justify, align, direction = "row") => ({
309
+ display: "flex",
310
+ justifyContent: justify,
311
+ alignItems: align,
312
+ flexDirection: direction,
313
+ });
314
+
315
+ const space = {
316
+ padding: "6px 8px",
317
+ margin: "6px 10px",
318
+ };
319
+
320
+ console.log({
321
+ style: { div: flex("center", "center"), button: { border: "none", ...space } }
322
+ .css,
323
+ });
324
+ ```
325
+
229
326
  ### Interacting with DOM
230
327
 
231
328
  use `script` to interact with browser with **html** property
@@ -248,6 +345,22 @@ wrap the script with a function, or use string
248
345
 
249
346
  ```
250
347
 
348
+ ### Event
349
+
350
+ you can add an event `this` keyword always refered to the node itseft
351
+
352
+ ```js
353
+ {
354
+ input:{type:"password",onkeypress(){
355
+ if(event.keyCode==13)
356
+ alert(this.value)
357
+ }},
358
+ button:{text:"Click me!",onclick(){
359
+ this.innerText = this.innerText=="clicked"?"Click me!":"clicked";
360
+ }}
361
+ }
362
+ ```
363
+
251
364
  ## Repulse
252
365
 
253
366
  If you already have an html file and want to modify it, or make it as a template, just use this
@@ -285,7 +398,7 @@ result
285
398
 
286
399
  ## Pass Value
287
400
 
288
- use function
401
+ use shorthand anonym function
289
402
 
290
403
  ```js
291
404
  console.log(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins").render);
@@ -329,6 +442,22 @@ IIFE if-else
329
442
  }
330
443
  ```
331
444
 
445
+ normal if else
446
+
447
+ ```js
448
+ let result;
449
+ if (status) result = { text: "Hello World!" };
450
+ else
451
+ result = {
452
+ a: { href: "#", text: "You are offline!" },
453
+ };
454
+
455
+ //use
456
+ {
457
+ h1: result,
458
+ }
459
+ ```
460
+
332
461
  IIFE switch-case
333
462
 
334
463
  ```js