htmv 0.0.33 → 0.0.35

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.
@@ -15,15 +15,36 @@ export function render(node, context) {
15
15
  throw new Error("La lista pasada no es un array");
16
16
  }
17
17
  if (node.type === "interpolation") {
18
- return String(context[node.value]);
18
+ return String(resolvePropertyPath(node.value));
19
19
  }
20
20
  if (node.type === "isset") {
21
- if (context[node.itemName] !== undefined &&
22
- context[node.itemName] !== null) {
21
+ const isNegated = node.itemName.startsWith("!");
22
+ const propName = isNegated ? node.itemName.slice(1) : node.itemName;
23
+ const prop = resolvePropertyPath(propName);
24
+ const exists = isset(prop);
25
+ if (isNegated ? !exists : exists) {
23
26
  return node.children.map((node) => render(node, context)).join("");
24
27
  }
25
28
  return "";
26
29
  }
27
30
  const output = node.children.map((node) => render(node, context));
28
31
  return output.join("");
32
+ function resolvePropertyPath(path) {
33
+ const [variable, ...properties] = path.split(".");
34
+ if (variable === undefined)
35
+ throw new Error("Missing variable name on interpolation");
36
+ let result = context[variable];
37
+ for (const property of properties) {
38
+ if (typeof result !== "object" || result === null)
39
+ throw new Error("Property access attempt on non-object.");
40
+ result = result[property];
41
+ }
42
+ return result;
43
+ }
44
+ }
45
+ function isset(prop) {
46
+ if (Array.isArray(prop)) {
47
+ return prop.length > 0;
48
+ }
49
+ return prop !== undefined && prop !== null && prop !== false;
29
50
  }
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.33",
5
+ "version": "0.0.35",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "2.3.3",
8
8
  "@types/bun": "latest"
@@ -52,17 +52,39 @@ export function render(node: Node, context: Record<string, unknown>): string {
52
52
  throw new Error("La lista pasada no es un array");
53
53
  }
54
54
  if (node.type === "interpolation") {
55
- return String(context[node.value]);
55
+ return String(resolvePropertyPath(node.value));
56
56
  }
57
57
  if (node.type === "isset") {
58
- if (
59
- context[node.itemName] !== undefined &&
60
- context[node.itemName] !== null
61
- ) {
58
+ const isNegated = node.itemName.startsWith("!");
59
+ const propName = isNegated ? node.itemName.slice(1) : node.itemName;
60
+ const prop = resolvePropertyPath(propName);
61
+ const exists = isset(prop);
62
+ if (isNegated ? !exists : exists) {
62
63
  return node.children.map((node) => render(node, context)).join("");
63
64
  }
64
65
  return "";
65
66
  }
66
67
  const output = node.children.map((node) => render(node, context));
67
68
  return output.join("");
69
+
70
+ function resolvePropertyPath(path: string) {
71
+ const [variable, ...properties] = path.split(".");
72
+ if (variable === undefined)
73
+ throw new Error("Missing variable name on interpolation");
74
+ let result = context[variable];
75
+ for (const property of properties) {
76
+ if (typeof result !== "object" || result === null)
77
+ throw new Error("Property access attempt on non-object.");
78
+ result = (result as Record<string, unknown>)[property];
79
+ }
80
+ return result;
81
+ }
82
+ }
83
+
84
+ function isset(prop: unknown) {
85
+ if (Array.isArray(prop)) {
86
+ return prop.length > 0;
87
+ }
88
+
89
+ return prop !== undefined && prop !== null && prop !== false;
68
90
  }