neobiz 1.2.5 → 1.3.4

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/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  /** @format */
2
2
  "use strict";
3
3
  const path = require("path");
4
-
5
4
  function render(val = {}) {
6
5
  let result = "";
7
6
  Object.entries(val).forEach((e) => {
@@ -10,14 +9,13 @@ function render(val = {}) {
10
9
  result += render({ [e[0]]: g });
11
10
  });
12
11
  } else if (typeof e[1] == "object") {
13
- e[0] = e[0].replace(" ", "");
12
+ e[0] = e[0].replace(/\s/g, "");
14
13
  result += `<${e[0]}`;
15
14
  const temp = [];
16
15
  let inn = "";
17
16
  Object.entries(e[1]).forEach((k) => {
18
17
  if (typeof k[1] == "string" && k[0] != "text" && k[0] != "html")
19
18
  result += ` ${k[0]}='${k[1]}'`;
20
- // else if (typeof k[1] == "function") result += ` ${k[0]}='${k[1] + ""}'`;
21
19
  else if (k[0] == "text")
22
20
  inn = `${
23
21
  typeof k[1] == "string"
@@ -63,11 +61,107 @@ function outFile(source, data) {
63
61
  console.log(`Output file: ${source}`);
64
62
  })();
65
63
  }
64
+ function fromStr(letter, prefix = 0) {
65
+ if (letter == "") return {};
66
+
67
+ letter = letter.replace(/[\r\n\t]/gm, "").replace(/>\s{2,}</gm, "><");
68
+ const result = {};
69
+
70
+ if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)?.[1]) {
71
+ result.tag = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(letter)[1];
72
+
73
+ result.tag = result.tag + Array(prefix).fill(" ").join("");
74
+
75
+ result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
76
+
77
+ result.inner = letter
78
+ .replace(result.head, "")
79
+ .split("")
80
+ .reverse()
81
+ .join("")
82
+ .replace(
83
+ `</${result.tag.replace(Array(prefix).fill(" ").join(""), "")}>`
84
+ .split("")
85
+ .reverse()
86
+ .join(""),
87
+ ""
88
+ )
89
+ .split("")
90
+ .reverse()
91
+ .join("");
92
+ } else {
93
+ result.tag = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(letter)[1];
94
+
95
+ result.tag = result.tag + Array(prefix).fill(" ").join("");
66
96
 
97
+ result.head = /<([a-zA-Z0-9]{1,}).*?>/.exec(letter)[0];
98
+ result.inner = "";
99
+ }
100
+ result.props = result.head.match(/[a-zA-Z0-9]{1,}=(["']).*?\1/g);
101
+
102
+ let temp = { [result.tag]: {} };
103
+ if (result.props)
104
+ result.attr = result.props.forEach((e) => {
105
+ let y = e.replace(/.*=(['"])/, "");
106
+ y = y.substring(0, y.length - 1);
107
+ let w = e.substring(0, e.indexOf("="));
108
+ temp[result.tag][w] = y;
109
+ });
110
+
111
+ let s = [];
112
+ let ttexr = result.inner;
113
+ while (
114
+ /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr) ||
115
+ /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)
116
+ ) {
117
+ let ss;
118
+ if (/<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)) {
119
+ ss = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)/.exec(ttexr)[0];
120
+ const h = /<([a-zA-Z0-9]{1,}).*?>.*?(<\/\1>)$/.exec(ss)[1];
121
+ const sss = ttexr.slice(ttexr.indexOf(ss));
122
+ const f = {
123
+ a: sss.replace("<" + h, "").indexOf(`</${h}>`),
124
+ b: sss.replace("<" + h, "").indexOf(`<${h}`),
125
+ };
126
+
127
+ ss = f.a > f.b && f.b > -1 ? sss : ss;
128
+ } else {
129
+ ss = /<([a-zA-Z0-9]{1,}).*?\/>/.exec(ttexr)[0];
130
+ }
131
+ ttexr = ttexr.replace(ss, "");
132
+ s.push(ss);
133
+ }
134
+ s.sort((a, b) => {
135
+ const r = { a: result.inner.indexOf(a), b: result.inner.indexOf(b) };
136
+ return r.a > r.b ? 1 : r.a < r.b ? -1 : 0;
137
+ });
138
+ result.status = s;
139
+ result.puretext = ttexr ? ttexr : ttexr + " ";
140
+ // result.temp = temp;
141
+ if (result.puretext != " ") temp[result.tag].text = result.puretext;
142
+ if (result.status) {
143
+ const ss = new Set();
144
+ const current = {};
145
+ s.forEach((e) => {
146
+ const hh = /<[a-zA-Z0-9]{1,}\s?/.exec(e)[0].substring(1);
147
+ if (ss.has(hh)) {
148
+ current[hh] = current[hh] + 1;
149
+ } else {
150
+ current[hh] = 0;
151
+ ss.add(hh);
152
+ }
153
+ temp = {
154
+ [result.tag]: { ...temp[result.tag], ...fromStr(e, current[hh]) },
155
+ };
156
+ });
157
+ }
158
+ return temp;
159
+ }
67
160
  module.exports = {
68
161
  render,
69
162
  range,
70
163
  outFile,
164
+ fromStr,
71
165
  partial: async (src) => {
72
166
  const { default: i } = await import(
73
167
  path.join("file:\\\\", process.cwd(), src)
package/package.json CHANGED
@@ -1,18 +1,17 @@
1
1
  {
2
2
  "name": "neobiz",
3
- "version": "1.2.5",
4
- "description": "simple view engine yet powerful with javascript features",
3
+ "version": "1.3.4",
4
+ "description": "A simple view engine with JS features",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
9
9
  "keywords": [
10
- "view engine",
10
+ "view",
11
11
  "template",
12
12
  "dom",
13
13
  "html",
14
- "",
15
- "view"
14
+ "express"
16
15
  ],
17
- "license": "ISC"
16
+ "license": "MIT"
18
17
  }
package/readme.md CHANGED
@@ -1,350 +1,24 @@
1
1
  <!-- @format -->
2
2
 
3
- # Documentation Neobiz.js
4
-
5
- > A simple view engine that has 100% JS features
6
-
7
- [[Github]](https://github.com/harakun24/neobiz.js)
8
- Neobiz.js is pure in JS lang, transform Object into HTML
9
-
10
- `read /example dir to understanding`
11
-
12
- [link to usage in express.js](https://www.npmjs.com/package/neobiz-express?activeTab=dependencies)
13
-
14
- **Function**
15
-
16
- | function | params | return-type | description |
17
- | -------- | ---------------------------------- | ----------- | ------------------------------- |
18
- | render | content:object | string | rendering to HTML |
19
- | outFile | destination:string, content:object | void | generate static HTML file |
20
- | range | length:int, start:int | Array | shortcut for looping in a range |
21
- | partial | source:string | AsyncFn | including other file |
22
-
23
- #### Why use this?
24
-
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
29
- - Almost no need to learn anything. Just need to learn how to render
30
- - Limitless potential, because it's just js. Almost all library should be compatible
31
-
32
- ## Basic syntax
33
-
34
- It's just an Object!
35
-
36
- ```js
37
- {
38
- // text property is same as innerText
39
- h1: {
40
- text: "Hello World!",
41
- }
42
- }
43
- // <h1>Hello World!</h1>
44
- ```
45
-
46
- properties dom
47
-
48
- ```js
49
- {
50
- h1: {
51
- class:"heading",
52
- text: "Hello World!",
53
- style:"color: #DC1E1E",
54
- }
55
- }
56
- // <h1 class="heading" style="color: #DC1E1E">Hello World!</h1>
57
- ```
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
-
68
- nested dom:
69
-
70
- ```js
71
- {
72
- body: {
73
- h1: {
74
- text: "Hello World!",
75
- }
76
- }
77
- }
78
- ```
79
-
80
- expression & operator
81
-
82
- ```js
83
- {
84
- span: [
85
- { text: 24 + 12 / 6 },
86
- { text: !false },
87
- ],
88
- }
89
- ```
90
-
91
- siblings dom:
92
-
93
- There is a case like this:
94
-
95
- ```html
96
- <li>orange</li>
97
- <li>banana</li>
98
- ```
99
-
100
- Object can not use 2 keys with the same name
101
-
102
- ```js
103
- // Don't do this
104
- {
105
- li:{text:"orange"},
106
- li:{text:"banana"},
107
- }
108
-
109
- // Do this
110
- {
111
- li:[
112
- {text:"orange"},
113
- {text:"banana"},
114
- ]
115
- }
116
- ```
117
-
118
- What if there is somethings in the middle of list?
119
-
120
- ```html
121
- <li>orange</li>
122
- <p>
123
- <span>This is in the middle</span>
124
- </p>
125
- <li>banana</li>
126
- ```
127
-
128
- Can't use array? just use this:
129
-
130
- > use string as key could distinc an element with just a space
131
- > no need to worry, it will render without space
132
-
133
- ```js
134
- {
135
- li:{text:"orange"},
136
- p:{span:{text:"This is in the middle"}},
137
- "li ":{text:"banana"},
138
- }
139
- ```
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
-
191
- ## Pass Value
192
-
193
- use function
194
-
195
- ```js
196
- import { render } from "../index.js";
197
-
198
- console.log(render(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins")));
199
- ```
200
-
201
- or
202
-
203
- ```js
204
- const greeting = (user) => ({ h1: `Hello, ${user}` });
205
-
206
- console.log(render(greeting("Vins")));
207
- ```
208
-
209
- ## Control Flow
210
-
211
- ternary:
212
-
213
- ```js
214
- {
215
- h1: status
216
- ? { text: "Hello World!" }
217
- : {
218
- a: { href: "#", text: "You are offline!" },
219
- },
220
- }
221
- ```
222
-
223
- IIFE if-else
224
-
225
- ```js
226
- {
227
- h1: (() => {
228
- let result;
229
- if (status) result = { text: "Hello World!" };
230
- else
231
- result = {
232
- a: { href: "#", text: "You are offline!" },
233
- };
234
- return result;
235
- })(),
236
- }
237
- ```
238
-
239
- IIFE switch-case
240
-
241
- ```js
242
- {
243
- h1: (() => {
244
- let result;
245
- switch (status) {
246
- case true:
247
- result = { text: "Hello World!" };
248
- break;
249
- case false:
250
- result = {
251
- a: { href: "#", text: "You are offline!" },
252
- };
3
+ <script src="https://cdn.jsdelivr.net/npm/showdown@2.1.0/dist/showdown.min.js"></script>
4
+ <script>
5
+ fetch(
6
+ "https://raw.githubusercontent.com/harakun24/assets-neobiz/main/readme.md"
7
+ )
8
+ .then((e) => e.text())
9
+ .then((e) => {
10
+ var converter = new showdown.Converter(),
11
+ text = e;
12
+ try {
13
+ document.getElementById("readme").innerHTML = converter.makeHtml(text);
14
+ } catch {
15
+ const t = document.createElement("a");
16
+ t.setAttribute(
17
+ "href",
18
+ "https://raw.githubusercontent.com/harakun24/assets-neobiz/main/readme.md"
19
+ );
20
+ t.innerText = "Go to readme";
21
+ document.querySelector("body").append(t);
253
22
  }
254
- return result;
255
- })(),
256
- }
257
- ```
258
-
259
- ## Loop & array manipulation
260
-
261
- range & map:
262
-
263
- ```js
264
- import { render, range } from "../index.js";
265
-
266
- {
267
- ul: {
268
- li: range(8, 1).map((item) => ({ text: "data-" + item })),
269
- },
270
- }
271
- ```
272
-
273
- map
274
-
275
- ```js
276
- {
277
- ul: {
278
- li: ["grape", "banana", "apple"].map((text) => ({ text })),
279
- },
280
- }
281
- ```
282
-
283
- for
284
-
285
- ```js
286
- {
287
- ul: {
288
- li: (() => {
289
- const result = [];
290
- for (let i = 1; i < 9; i++) result.push({ text: "data-" + i });
291
- return result;
292
- })(),
293
- },
294
- }
295
- ```
296
-
297
- while
298
-
299
- ```js
300
- {
301
- ul: {
302
- li: (() => {
303
- const result = [];
304
- let i = 0;
305
- while (++i < 9) result.push({ text: "data-" + i });
306
- return result;
307
- })(),
308
- },
309
- }
310
- ```
311
-
312
- ## partial & templating
313
-
314
- ### partial
315
-
316
- make sure to default export as a function
317
-
318
- ```js
319
- import { render, partial } from "../index.js";
320
-
321
- {
322
- head: (await partial("./exported.v.js"))("example page"),
323
- h1: { text: "This is example" },
324
- }
325
- ```
326
-
327
- `/exported.v.js`
328
-
329
- ```js
330
- export default (title) => ({ title: { text: title } });
331
- ```
332
-
333
- ### templating
334
-
335
- this example use single file usage. for modular usage leave to you, or just read the example dir
336
-
337
- ```js
338
- import { render } from "../index.js";
339
-
340
- const template = (content) => ({
341
- div: {
342
- class: "main wrapper",
343
- div: content,
344
- },
345
- });
346
-
347
- template({
348
- h1: { text: "Hello World!" },
349
- });
350
- ```
23
+ });
24
+ </script>
@@ -1,23 +0,0 @@
1
- /** @format */
2
-
3
- //use object literal
4
-
5
- import { render, outFile } from "../index.js";
6
-
7
- console.log(
8
- render({
9
- h1: { text: "Hello World!" },
10
- })
11
- );
12
-
13
- //output: <h1>Hello World!</h1>
14
-
15
- // output static file
16
- outFile("./basic.html", {
17
- h1: { text: "Hello World!" },
18
- });
19
-
20
- // properties
21
- render({
22
- h1: { class: "heading", id: "main", text: "Hello World!" },
23
- });
@@ -1,56 +0,0 @@
1
- /** @format */
2
-
3
- import { render } from "../index.js";
4
-
5
- const status = false;
6
-
7
- // ternary operator
8
-
9
- console.log(
10
- render({
11
- h1: status
12
- ? { text: "Hello World!" }
13
- : {
14
- a: { href: "#", text: "You are offline!" },
15
- },
16
- })
17
- //<h1><a href='#'>You are offline!</a></h1
18
- );
19
-
20
- // use IIFE
21
-
22
- // IF
23
- console.log(
24
- render({
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
- })
35
- //<h1><a href='#'>You are offline!</a></h1
36
- );
37
-
38
- // SWITCH
39
- console.log(
40
- render({
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
- })
55
- //<h1><a href='#'>You are offline!</a></h1
56
- );
@@ -1,3 +0,0 @@
1
- /** @format */
2
-
3
- export default (title) => ({ title: { text: title } });
package/example/index.js DELETED
@@ -1,3 +0,0 @@
1
- /** @format */
2
-
3
- import "./basic_syntax.v.js";
@@ -1,15 +0,0 @@
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
- });
@@ -1,16 +0,0 @@
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
- });
@@ -1,53 +0,0 @@
1
- /** @format */
2
-
3
- import { render, range } from "../index.js";
4
-
5
- // use range @param= length, startFrom
6
-
7
- console.log(
8
- render({
9
- ul: {
10
- li: range(8, 1).map((item) => ({ text: "data-" + item })),
11
- },
12
- })
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
- render({
19
- ul: {
20
- li: ["grape", "banana", "apple"].map((text) => ({ text })),
21
- },
22
- })
23
- //<ul><li>grape</li><li>banana</li><li>apple</li></ul>
24
- );
25
-
26
- // for
27
- console.log(
28
- render({
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
- })
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
- render({
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
- })
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
- );
@@ -1,13 +0,0 @@
1
- /** @format */
2
-
3
- import { render } from "../index.js";
4
-
5
- console.log(
6
- render({
7
- ul: {
8
- li: [{ text: "one" }, { text: "two" }, { text: "3" }],
9
- },
10
- })
11
- );
12
-
13
- // <ul><li>one</li><li>two</li><li>3</li></ul>
@@ -1,17 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
1
- /** @format */
2
-
3
- import { render, partial } from "../index.js";
4
-
5
- console.log(
6
- render({
7
- head: (await partial("./exported.v.js"))("example page"),
8
- h1: { text: "This is example" },
9
- })
10
- //<head><title>example page</title></head><h1>This is example</h1>
11
- );
@@ -1,17 +0,0 @@
1
- /** @format */
2
-
3
- //use function, iife recommended :)
4
-
5
- import { render } from "../index.js";
6
-
7
- console.log(
8
- // Ignore this
9
- render(((user) => ({ h1: { text: `Hello, ${user}` } }))("Vins"))
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(render(greeting("Vins")));
@@ -1,20 +0,0 @@
1
- /** @format */
2
-
3
- import { render } from "../index.js";
4
-
5
- const template = (content) => ({
6
- div: {
7
- class: "main wrapper",
8
- div: content,
9
- },
10
- });
11
-
12
- console.log(
13
- render(
14
- template({
15
- h1: { text: "Hello World!" },
16
- })
17
- )
18
- );
19
-
20
- //<div class='main wrapper'><div><h1>Hello World!</h1></div></div>