@constela/runtime 0.2.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -42,6 +42,8 @@ declare function createStateStore(definitions: Record<string, StateDefinition>):
42
42
  * - Variable reads (var)
43
43
  * - Binary operations (bin)
44
44
  * - Not operation (not)
45
+ * - Conditional expressions (cond)
46
+ * - Property access (get)
45
47
  */
46
48
 
47
49
  interface EvaluationContext {
package/dist/index.js CHANGED
@@ -150,8 +150,25 @@ function evaluate(expr, ctx) {
150
150
  return evaluateBinary(expr.op, expr.left, expr.right, ctx);
151
151
  case "not":
152
152
  return !evaluate(expr.operand, ctx);
153
- default:
154
- throw new Error("Unknown expression type");
153
+ case "cond":
154
+ return evaluate(expr.if, ctx) ? evaluate(expr.then, ctx) : evaluate(expr.else, ctx);
155
+ case "get": {
156
+ const baseValue = evaluate(expr.base, ctx);
157
+ if (baseValue == null) return void 0;
158
+ const pathParts = expr.path.split(".");
159
+ const forbiddenKeys = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
160
+ let value = baseValue;
161
+ for (const part of pathParts) {
162
+ if (forbiddenKeys.has(part)) return void 0;
163
+ if (value == null) return void 0;
164
+ value = value[part];
165
+ }
166
+ return value;
167
+ }
168
+ default: {
169
+ const _exhaustiveCheck = expr;
170
+ throw new Error(`Unknown expression type: ${JSON.stringify(_exhaustiveCheck)}`);
171
+ }
155
172
  }
156
173
  }
157
174
  function evaluateBinary(op, left, right, ctx) {
@@ -228,7 +245,7 @@ async function executeStep(step, ctx) {
228
245
  await executeSetStep(step.target, step.value, ctx);
229
246
  break;
230
247
  case "update":
231
- await executeUpdateStep(step.target, step.operation, step.value, ctx);
248
+ await executeUpdateStep(step, ctx);
232
249
  break;
233
250
  case "fetch":
234
251
  await executeFetchStep(step, ctx);
@@ -240,7 +257,8 @@ async function executeSetStep(target, value, ctx) {
240
257
  const newValue = evaluate(value, evalCtx);
241
258
  ctx.state.set(target, newValue);
242
259
  }
243
- async function executeUpdateStep(target, operation, value, ctx) {
260
+ async function executeUpdateStep(step, ctx) {
261
+ const { target, operation, value } = step;
244
262
  const evalCtx = { state: ctx.state, locals: ctx.locals };
245
263
  const currentValue = ctx.state.get(target);
246
264
  switch (operation) {
@@ -279,6 +297,50 @@ async function executeUpdateStep(target, operation, value, ctx) {
279
297
  }
280
298
  break;
281
299
  }
300
+ case "toggle": {
301
+ const current = typeof currentValue === "boolean" ? currentValue : false;
302
+ ctx.state.set(target, !current);
303
+ break;
304
+ }
305
+ case "merge": {
306
+ const evalResult = value ? evaluate(value, evalCtx) : {};
307
+ const mergeValue = typeof evalResult === "object" && evalResult !== null ? evalResult : {};
308
+ const current = typeof currentValue === "object" && currentValue !== null ? currentValue : {};
309
+ ctx.state.set(target, { ...current, ...mergeValue });
310
+ break;
311
+ }
312
+ case "replaceAt": {
313
+ const idx = step.index ? evaluate(step.index, evalCtx) : 0;
314
+ const newValue = value ? evaluate(value, evalCtx) : void 0;
315
+ const arr = Array.isArray(currentValue) ? [...currentValue] : [];
316
+ if (typeof idx === "number" && idx >= 0 && idx < arr.length) {
317
+ arr[idx] = newValue;
318
+ }
319
+ ctx.state.set(target, arr);
320
+ break;
321
+ }
322
+ case "insertAt": {
323
+ const idx = step.index ? evaluate(step.index, evalCtx) : 0;
324
+ const newValue = value ? evaluate(value, evalCtx) : void 0;
325
+ const arr = Array.isArray(currentValue) ? [...currentValue] : [];
326
+ if (typeof idx === "number" && idx >= 0) {
327
+ arr.splice(idx, 0, newValue);
328
+ }
329
+ ctx.state.set(target, arr);
330
+ break;
331
+ }
332
+ case "splice": {
333
+ const idx = step.index ? evaluate(step.index, evalCtx) : 0;
334
+ const delCount = step.deleteCount ? evaluate(step.deleteCount, evalCtx) : 0;
335
+ const items = value ? evaluate(value, evalCtx) : [];
336
+ const arr = Array.isArray(currentValue) ? [...currentValue] : [];
337
+ if (typeof idx === "number" && typeof delCount === "number") {
338
+ const insertItems = Array.isArray(items) ? items : [];
339
+ arr.splice(idx, delCount, ...insertItems);
340
+ }
341
+ ctx.state.set(target, arr);
342
+ break;
343
+ }
282
344
  }
283
345
  }
284
346
  async function executeFetchStep(step, ctx) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/runtime",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Runtime DOM renderer for Constela UI framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -15,8 +15,8 @@
15
15
  "dist"
16
16
  ],
17
17
  "dependencies": {
18
- "@constela/core": "0.2.0",
19
- "@constela/compiler": "0.2.0"
18
+ "@constela/core": "0.3.1",
19
+ "@constela/compiler": "0.3.1"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/node": "^20.10.0",