htmv 0.0.30 → 0.0.32

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/README.md CHANGED
@@ -109,4 +109,8 @@ bunx htmv@latest gen view MyCoolView --path cool_stuff/my_custom_views_folder
109
109
  ```
110
110
 
111
111
  # Hot reloading
112
- Having to restart the server every time you make a change can be quite tedious. HTMV takes care of this thanks to Bun. Just develop with `bun dev` and it should work out of the box! Note that this does not include hot reloading in the browser. As of now, you have to refresh the page to see new changes. It doesn't update in real time.
112
+ Having to restart the server every time you make a change can be quite tedious. HTMV takes care of this thanks to Bun. Just develop with `bun dev` and it should work out of the box! Note that this does not include hot reloading in the browser. As of now, you have to refresh the page to see new changes. It doesn't update in real time.
113
+
114
+ # Still have questions?
115
+ How about asking the DeepWiki instead?
116
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/Fabrisdev/htmv)
package/dist/views.js CHANGED
@@ -10,6 +10,14 @@ export async function view(view, props) {
10
10
  const filePath = path.join(viewsPath, `${view}.html`);
11
11
  const code = await fs.readFile(filePath, "utf-8");
12
12
  const replacedCode = code
13
+ .replace(/<For\s+(\w+)\s+in\s+(\w+)>([\s\S]*?)<\/For>/g, (_, itemName, listName, innerContent) => {
14
+ const list = props[listName];
15
+ if (!Array.isArray(list))
16
+ throw new Error(`${listName} on view ${view} is not an array. If you wish for the For to not do anything pass an empty array instead.`);
17
+ return list
18
+ .map((item) => innerContent.replace(new RegExp(`{${itemName}}`, "g"), String(item)))
19
+ .join("");
20
+ })
13
21
  .replace(/{(\w+)}/g, (_, propName) => {
14
22
  return props[propName];
15
23
  })
@@ -18,7 +26,7 @@ export async function view(view, props) {
18
26
  const propName = isNegated
19
27
  ? propNameWithPrefix.slice(1)
20
28
  : propNameWithPrefix;
21
- const exists = props[propName] !== undefined && props[propName] !== null;
29
+ const exists = isset(props[propName]);
22
30
  if (isNegated ? !exists : exists)
23
31
  return innerContent;
24
32
  return "";
@@ -27,3 +35,9 @@ export async function view(view, props) {
27
35
  headers: { "Content-Type": "text/html; charset=utf-8" },
28
36
  });
29
37
  }
38
+ function isset(prop) {
39
+ if (Array.isArray(prop)) {
40
+ return prop.length > 0;
41
+ }
42
+ return prop !== undefined && prop !== null;
43
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "htmv",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.30",
5
+ "version": "0.0.32",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "2.3.3",
8
8
  "@types/bun": "latest"
package/src/views.ts CHANGED
@@ -15,6 +15,25 @@ export async function view(view: string, props: Record<string, unknown>) {
15
15
  const filePath = path.join(viewsPath, `${view}.html`);
16
16
  const code = await fs.readFile(filePath, "utf-8");
17
17
  const replacedCode = code
18
+ .replace(
19
+ /<For\s+(\w+)\s+in\s+(\w+)>([\s\S]*?)<\/For>/g,
20
+ (_, itemName: string, listName: string, innerContent: string) => {
21
+ const list = props[listName];
22
+ if (!Array.isArray(list))
23
+ throw new Error(
24
+ `${listName} on view ${view} is not an array. If you wish for the For to not do anything pass an empty array instead.`,
25
+ );
26
+
27
+ return list
28
+ .map((item) =>
29
+ innerContent.replace(
30
+ new RegExp(`{${itemName}}`, "g"),
31
+ String(item),
32
+ ),
33
+ )
34
+ .join("");
35
+ },
36
+ )
18
37
  .replace(/{(\w+)}/g, (_, propName) => {
19
38
  return props[propName] as string;
20
39
  })
@@ -25,8 +44,7 @@ export async function view(view: string, props: Record<string, unknown>) {
25
44
  const propName = isNegated
26
45
  ? propNameWithPrefix.slice(1)
27
46
  : propNameWithPrefix;
28
- const exists =
29
- props[propName] !== undefined && props[propName] !== null;
47
+ const exists = isset(props[propName]);
30
48
 
31
49
  if (isNegated ? !exists : exists) return innerContent;
32
50
  return "";
@@ -36,3 +54,10 @@ export async function view(view: string, props: Record<string, unknown>) {
36
54
  headers: { "Content-Type": "text/html; charset=utf-8" },
37
55
  });
38
56
  }
57
+
58
+ function isset(prop: unknown) {
59
+ if (Array.isArray(prop)) {
60
+ return prop.length > 0;
61
+ }
62
+ return prop !== undefined && prop !== null;
63
+ }