@constela/runtime 0.16.5 → 0.17.0

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.
Files changed (2) hide show
  1. package/dist/index.js +486 -0
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -374,6 +374,307 @@ function createTypedStateStore(definitions) {
374
374
  }
375
375
 
376
376
  // src/expression/evaluator.ts
377
+ var SAFE_ARRAY_METHODS = /* @__PURE__ */ new Set([
378
+ "length",
379
+ "at",
380
+ "includes",
381
+ "slice",
382
+ "indexOf",
383
+ "join",
384
+ "filter",
385
+ "map",
386
+ "find",
387
+ "findIndex",
388
+ "some",
389
+ "every"
390
+ ]);
391
+ var SAFE_STRING_METHODS = /* @__PURE__ */ new Set([
392
+ "length",
393
+ "charAt",
394
+ "substring",
395
+ "slice",
396
+ "split",
397
+ "trim",
398
+ "toUpperCase",
399
+ "toLowerCase",
400
+ "replace",
401
+ "includes",
402
+ "startsWith",
403
+ "endsWith",
404
+ "indexOf"
405
+ ]);
406
+ var SAFE_MATH_METHODS = /* @__PURE__ */ new Set([
407
+ "min",
408
+ "max",
409
+ "round",
410
+ "floor",
411
+ "ceil",
412
+ "abs",
413
+ "sqrt",
414
+ "pow",
415
+ "random",
416
+ "sin",
417
+ "cos",
418
+ "tan"
419
+ ]);
420
+ var SAFE_DATE_STATIC_METHODS = /* @__PURE__ */ new Set([
421
+ "now",
422
+ "parse"
423
+ ]);
424
+ var SAFE_DATE_INSTANCE_METHODS = /* @__PURE__ */ new Set([
425
+ "toISOString",
426
+ "toDateString",
427
+ "toTimeString",
428
+ "getTime",
429
+ "getFullYear",
430
+ "getMonth",
431
+ "getDate",
432
+ "getHours",
433
+ "getMinutes",
434
+ "getSeconds",
435
+ "getMilliseconds"
436
+ ]);
437
+ function createLambdaFunction(lambda, ctx) {
438
+ return (item, index) => {
439
+ const lambdaLocals = {
440
+ ...ctx.locals,
441
+ [lambda.param]: item
442
+ };
443
+ if (lambda.index !== void 0) {
444
+ lambdaLocals[lambda.index] = index;
445
+ }
446
+ return evaluate(lambda.body, { ...ctx, locals: lambdaLocals });
447
+ };
448
+ }
449
+ function callArrayMethod(target, method, args, ctx, rawArgs) {
450
+ if (!SAFE_ARRAY_METHODS.has(method)) {
451
+ return void 0;
452
+ }
453
+ switch (method) {
454
+ case "length":
455
+ return target.length;
456
+ case "at": {
457
+ const index = typeof args[0] === "number" ? args[0] : 0;
458
+ return target.at(index);
459
+ }
460
+ case "includes": {
461
+ const searchElement = args[0];
462
+ const fromIndex = typeof args[1] === "number" ? args[1] : void 0;
463
+ return target.includes(searchElement, fromIndex);
464
+ }
465
+ case "slice": {
466
+ const start = typeof args[0] === "number" ? args[0] : void 0;
467
+ const end = typeof args[1] === "number" ? args[1] : void 0;
468
+ return target.slice(start, end);
469
+ }
470
+ case "indexOf": {
471
+ const searchElement = args[0];
472
+ const fromIndex = typeof args[1] === "number" ? args[1] : void 0;
473
+ return target.indexOf(searchElement, fromIndex);
474
+ }
475
+ case "join": {
476
+ const separator = typeof args[0] === "string" ? args[0] : ",";
477
+ return target.join(separator);
478
+ }
479
+ case "filter": {
480
+ const lambdaExpr = rawArgs?.[0];
481
+ if (!lambdaExpr || lambdaExpr.expr !== "lambda") return void 0;
482
+ const fn = createLambdaFunction(lambdaExpr, ctx);
483
+ return target.filter((item, index) => !!fn(item, index));
484
+ }
485
+ case "map": {
486
+ const lambdaExpr = rawArgs?.[0];
487
+ if (!lambdaExpr || lambdaExpr.expr !== "lambda") return void 0;
488
+ const fn = createLambdaFunction(lambdaExpr, ctx);
489
+ return target.map((item, index) => fn(item, index));
490
+ }
491
+ case "find": {
492
+ const lambdaExpr = rawArgs?.[0];
493
+ if (!lambdaExpr || lambdaExpr.expr !== "lambda") return void 0;
494
+ const fn = createLambdaFunction(lambdaExpr, ctx);
495
+ return target.find((item, index) => !!fn(item, index));
496
+ }
497
+ case "findIndex": {
498
+ const lambdaExpr = rawArgs?.[0];
499
+ if (!lambdaExpr || lambdaExpr.expr !== "lambda") return void 0;
500
+ const fn = createLambdaFunction(lambdaExpr, ctx);
501
+ return target.findIndex((item, index) => !!fn(item, index));
502
+ }
503
+ case "some": {
504
+ const lambdaExpr = rawArgs?.[0];
505
+ if (!lambdaExpr || lambdaExpr.expr !== "lambda") return void 0;
506
+ const fn = createLambdaFunction(lambdaExpr, ctx);
507
+ return target.some((item, index) => !!fn(item, index));
508
+ }
509
+ case "every": {
510
+ const lambdaExpr = rawArgs?.[0];
511
+ if (!lambdaExpr || lambdaExpr.expr !== "lambda") return void 0;
512
+ const fn = createLambdaFunction(lambdaExpr, ctx);
513
+ return target.every((item, index) => !!fn(item, index));
514
+ }
515
+ default:
516
+ return void 0;
517
+ }
518
+ }
519
+ function callStringMethod(target, method, args) {
520
+ if (!SAFE_STRING_METHODS.has(method)) {
521
+ return void 0;
522
+ }
523
+ switch (method) {
524
+ case "length":
525
+ return target.length;
526
+ case "charAt": {
527
+ const index = typeof args[0] === "number" ? args[0] : 0;
528
+ return target.charAt(index);
529
+ }
530
+ case "substring": {
531
+ const start = typeof args[0] === "number" ? args[0] : 0;
532
+ const end = typeof args[1] === "number" ? args[1] : void 0;
533
+ return target.substring(start, end);
534
+ }
535
+ case "slice": {
536
+ const start = typeof args[0] === "number" ? args[0] : void 0;
537
+ const end = typeof args[1] === "number" ? args[1] : void 0;
538
+ return target.slice(start, end);
539
+ }
540
+ case "split": {
541
+ const separator = typeof args[0] === "string" ? args[0] : "";
542
+ return target.split(separator);
543
+ }
544
+ case "trim":
545
+ return target.trim();
546
+ case "toUpperCase":
547
+ return target.toUpperCase();
548
+ case "toLowerCase":
549
+ return target.toLowerCase();
550
+ case "replace": {
551
+ const search = typeof args[0] === "string" ? args[0] : "";
552
+ const replace = typeof args[1] === "string" ? args[1] : "";
553
+ return target.replace(search, replace);
554
+ }
555
+ case "includes": {
556
+ const search = typeof args[0] === "string" ? args[0] : "";
557
+ const position = typeof args[1] === "number" ? args[1] : void 0;
558
+ return target.includes(search, position);
559
+ }
560
+ case "startsWith": {
561
+ const search = typeof args[0] === "string" ? args[0] : "";
562
+ const position = typeof args[1] === "number" ? args[1] : void 0;
563
+ return target.startsWith(search, position);
564
+ }
565
+ case "endsWith": {
566
+ const search = typeof args[0] === "string" ? args[0] : "";
567
+ const length = typeof args[1] === "number" ? args[1] : void 0;
568
+ return target.endsWith(search, length);
569
+ }
570
+ case "indexOf": {
571
+ const search = typeof args[0] === "string" ? args[0] : "";
572
+ const position = typeof args[1] === "number" ? args[1] : void 0;
573
+ return target.indexOf(search, position);
574
+ }
575
+ default:
576
+ return void 0;
577
+ }
578
+ }
579
+ function callMathMethod(method, args) {
580
+ if (!SAFE_MATH_METHODS.has(method)) {
581
+ return void 0;
582
+ }
583
+ const numbers = args.filter((a) => typeof a === "number");
584
+ switch (method) {
585
+ case "min":
586
+ return Math.min(...numbers);
587
+ case "max":
588
+ return Math.max(...numbers);
589
+ case "round": {
590
+ const num = numbers[0];
591
+ return num !== void 0 ? Math.round(num) : void 0;
592
+ }
593
+ case "floor": {
594
+ const num = numbers[0];
595
+ return num !== void 0 ? Math.floor(num) : void 0;
596
+ }
597
+ case "ceil": {
598
+ const num = numbers[0];
599
+ return num !== void 0 ? Math.ceil(num) : void 0;
600
+ }
601
+ case "abs": {
602
+ const num = numbers[0];
603
+ return num !== void 0 ? Math.abs(num) : void 0;
604
+ }
605
+ case "sqrt": {
606
+ const num = numbers[0];
607
+ return num !== void 0 ? Math.sqrt(num) : void 0;
608
+ }
609
+ case "pow": {
610
+ const base = numbers[0];
611
+ const exponent = numbers[1];
612
+ return base !== void 0 && exponent !== void 0 ? Math.pow(base, exponent) : void 0;
613
+ }
614
+ case "random":
615
+ return Math.random();
616
+ case "sin": {
617
+ const num = numbers[0];
618
+ return num !== void 0 ? Math.sin(num) : void 0;
619
+ }
620
+ case "cos": {
621
+ const num = numbers[0];
622
+ return num !== void 0 ? Math.cos(num) : void 0;
623
+ }
624
+ case "tan": {
625
+ const num = numbers[0];
626
+ return num !== void 0 ? Math.tan(num) : void 0;
627
+ }
628
+ default:
629
+ return void 0;
630
+ }
631
+ }
632
+ function callDateStaticMethod(method, args) {
633
+ if (!SAFE_DATE_STATIC_METHODS.has(method)) {
634
+ return void 0;
635
+ }
636
+ switch (method) {
637
+ case "now":
638
+ return Date.now();
639
+ case "parse": {
640
+ const dateString = args[0];
641
+ return typeof dateString === "string" ? Date.parse(dateString) : void 0;
642
+ }
643
+ default:
644
+ return void 0;
645
+ }
646
+ }
647
+ function callDateInstanceMethod(target, method) {
648
+ if (!SAFE_DATE_INSTANCE_METHODS.has(method)) {
649
+ return void 0;
650
+ }
651
+ switch (method) {
652
+ case "toISOString":
653
+ return target.toISOString();
654
+ case "toDateString":
655
+ return target.toDateString();
656
+ case "toTimeString":
657
+ return target.toTimeString();
658
+ case "getTime":
659
+ return target.getTime();
660
+ case "getFullYear":
661
+ return target.getFullYear();
662
+ case "getMonth":
663
+ return target.getMonth();
664
+ case "getDate":
665
+ return target.getDate();
666
+ case "getHours":
667
+ return target.getHours();
668
+ case "getMinutes":
669
+ return target.getMinutes();
670
+ case "getSeconds":
671
+ return target.getSeconds();
672
+ case "getMilliseconds":
673
+ return target.getMilliseconds();
674
+ default:
675
+ return void 0;
676
+ }
677
+ }
377
678
  function evaluate(expr, ctx) {
378
679
  switch (expr.expr) {
379
680
  case "lit":
@@ -518,6 +819,33 @@ function evaluate(expr, ctx) {
518
819
  }
519
820
  return validity[property] ?? null;
520
821
  }
822
+ case "call": {
823
+ const callExpr = expr;
824
+ const target = evaluate(callExpr.target, ctx);
825
+ if (target == null) return void 0;
826
+ const args = callExpr.args?.map((arg) => {
827
+ if (arg.expr === "lambda") return arg;
828
+ return evaluate(arg, ctx);
829
+ }) ?? [];
830
+ if (Array.isArray(target)) {
831
+ return callArrayMethod(target, callExpr.method, args, ctx, callExpr.args);
832
+ }
833
+ if (typeof target === "string") {
834
+ return callStringMethod(target, callExpr.method, args);
835
+ }
836
+ if (target === Math) {
837
+ return callMathMethod(callExpr.method, args);
838
+ }
839
+ if (target === Date) {
840
+ return callDateStaticMethod(callExpr.method, args);
841
+ }
842
+ if (target instanceof Date) {
843
+ return callDateInstanceMethod(target, callExpr.method);
844
+ }
845
+ return void 0;
846
+ }
847
+ case "lambda":
848
+ return void 0;
521
849
  default: {
522
850
  const _exhaustiveCheck = expr;
523
851
  throw new Error(`Unknown expression type: ${JSON.stringify(_exhaustiveCheck)}`);
@@ -14539,6 +14867,8 @@ function hydrateChildren(children, parent, ctx) {
14539
14867
  hydrate(childNode, firstDomChild, ctx);
14540
14868
  domIndex += itemCount;
14541
14869
  }
14870
+ } else {
14871
+ hydrateEachEmpty(childNode, parent, domChildren[domIndex] || null, ctx);
14542
14872
  }
14543
14873
  } else {
14544
14874
  const domChild = domChildren[domIndex];
@@ -14766,6 +15096,162 @@ function hydrateIfWithoutDom(node, parent, nextSibling, ctx, branchInfo) {
14766
15096
  }
14767
15097
  });
14768
15098
  }
15099
+ function hydrateEachEmpty(node, parent, insertBefore, ctx) {
15100
+ const anchor = document.createComment("each");
15101
+ if (insertBefore) {
15102
+ parent.insertBefore(anchor, insertBefore);
15103
+ } else {
15104
+ parent.appendChild(anchor);
15105
+ }
15106
+ const hasKey = !!node.key;
15107
+ let itemStateMap = /* @__PURE__ */ new Map();
15108
+ let currentNodes = [];
15109
+ let itemCleanups = [];
15110
+ const effectCleanup = createEffect(() => {
15111
+ const items = evaluate(node.items, {
15112
+ state: ctx.state,
15113
+ locals: ctx.locals,
15114
+ ...ctx.imports && { imports: ctx.imports },
15115
+ ...ctx.route && { route: ctx.route }
15116
+ });
15117
+ if (!hasKey || !node.key) {
15118
+ for (const cleanup of itemCleanups) {
15119
+ cleanup();
15120
+ }
15121
+ itemCleanups = [];
15122
+ for (const oldNode of currentNodes) {
15123
+ if (oldNode.parentNode) {
15124
+ oldNode.parentNode.removeChild(oldNode);
15125
+ }
15126
+ }
15127
+ currentNodes = [];
15128
+ if (Array.isArray(items)) {
15129
+ items.forEach((item, index) => {
15130
+ const itemLocals = {
15131
+ ...ctx.locals,
15132
+ [node.as]: item
15133
+ };
15134
+ if (node.index) {
15135
+ itemLocals[node.index] = index;
15136
+ }
15137
+ const localCleanups = [];
15138
+ const itemCtx = {
15139
+ state: ctx.state,
15140
+ actions: ctx.actions,
15141
+ locals: itemLocals,
15142
+ cleanups: localCleanups,
15143
+ ...ctx.imports && { imports: ctx.imports }
15144
+ };
15145
+ const itemNode = render(node.body, itemCtx);
15146
+ currentNodes.push(itemNode);
15147
+ itemCleanups.push(...localCleanups);
15148
+ if (anchor.parentNode) {
15149
+ let refNode = anchor.nextSibling;
15150
+ if (currentNodes.length > 1) {
15151
+ const lastExisting = currentNodes[currentNodes.length - 2];
15152
+ if (lastExisting) {
15153
+ refNode = lastExisting.nextSibling;
15154
+ }
15155
+ }
15156
+ anchor.parentNode.insertBefore(itemNode, refNode);
15157
+ }
15158
+ });
15159
+ }
15160
+ return;
15161
+ }
15162
+ const newItemStateMap = /* @__PURE__ */ new Map();
15163
+ const newNodes = [];
15164
+ const seenKeys = /* @__PURE__ */ new Set();
15165
+ if (Array.isArray(items)) {
15166
+ items.forEach((item, index) => {
15167
+ const tempLocals = {
15168
+ ...ctx.locals,
15169
+ [node.as]: item,
15170
+ ...node.index ? { [node.index]: index } : {}
15171
+ };
15172
+ const keyValue = evaluate(node.key, {
15173
+ state: ctx.state,
15174
+ locals: tempLocals,
15175
+ ...ctx.imports && { imports: ctx.imports },
15176
+ ...ctx.route && { route: ctx.route }
15177
+ });
15178
+ if (seenKeys.has(keyValue)) {
15179
+ if (typeof process !== "undefined" && process.env?.["NODE_ENV"] !== "production") {
15180
+ console.warn(`Duplicate key "${keyValue}" in each loop. Keys should be unique.`);
15181
+ }
15182
+ }
15183
+ seenKeys.add(keyValue);
15184
+ const existingState = itemStateMap.get(keyValue);
15185
+ if (existingState) {
15186
+ existingState.itemSignal.set(item);
15187
+ existingState.indexSignal.set(index);
15188
+ newItemStateMap.set(keyValue, existingState);
15189
+ newNodes.push(existingState.node);
15190
+ } else {
15191
+ const itemSignal = createSignal(item);
15192
+ const indexSignal = createSignal(index);
15193
+ const reactiveLocals = createReactiveLocals2(
15194
+ ctx.locals,
15195
+ itemSignal,
15196
+ indexSignal,
15197
+ node.as,
15198
+ node.index
15199
+ );
15200
+ const localCleanups = [];
15201
+ const itemCtx = {
15202
+ state: ctx.state,
15203
+ actions: ctx.actions,
15204
+ locals: reactiveLocals,
15205
+ cleanups: localCleanups,
15206
+ ...ctx.imports && { imports: ctx.imports }
15207
+ };
15208
+ const itemNode = render(node.body, itemCtx);
15209
+ const newState = {
15210
+ key: keyValue,
15211
+ node: itemNode,
15212
+ cleanups: localCleanups,
15213
+ itemSignal,
15214
+ indexSignal
15215
+ };
15216
+ newItemStateMap.set(keyValue, newState);
15217
+ newNodes.push(itemNode);
15218
+ }
15219
+ });
15220
+ }
15221
+ for (const [key2, state] of itemStateMap) {
15222
+ if (!newItemStateMap.has(key2)) {
15223
+ for (const cleanup of state.cleanups) {
15224
+ cleanup();
15225
+ }
15226
+ if (state.node.parentNode) {
15227
+ state.node.parentNode.removeChild(state.node);
15228
+ }
15229
+ }
15230
+ }
15231
+ if (anchor.parentNode) {
15232
+ let refNode = anchor;
15233
+ for (const itemNode of newNodes) {
15234
+ const nextSibling = refNode.nextSibling;
15235
+ if (nextSibling !== itemNode) {
15236
+ anchor.parentNode.insertBefore(itemNode, refNode.nextSibling);
15237
+ }
15238
+ refNode = itemNode;
15239
+ }
15240
+ }
15241
+ itemStateMap = newItemStateMap;
15242
+ currentNodes = newNodes;
15243
+ itemCleanups = [];
15244
+ for (const state of itemStateMap.values()) {
15245
+ itemCleanups.push(...state.cleanups);
15246
+ }
15247
+ });
15248
+ ctx.cleanups.push(effectCleanup);
15249
+ ctx.cleanups.push(() => {
15250
+ for (const cleanup of itemCleanups) {
15251
+ cleanup();
15252
+ }
15253
+ });
15254
+ }
14769
15255
  function hydrateEach(node, firstItemDomNode, ctx) {
14770
15256
  const parent = firstItemDomNode.parentNode;
14771
15257
  if (!parent) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/runtime",
3
- "version": "0.16.5",
3
+ "version": "0.17.0",
4
4
  "description": "Runtime DOM renderer for Constela UI framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,8 +18,8 @@
18
18
  "dompurify": "^3.3.1",
19
19
  "marked": "^17.0.1",
20
20
  "shiki": "^3.20.0",
21
- "@constela/compiler": "0.12.0",
22
- "@constela/core": "0.13.0"
21
+ "@constela/compiler": "0.13.0",
22
+ "@constela/core": "0.14.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/dompurify": "^3.2.0",
@@ -29,7 +29,7 @@
29
29
  "tsup": "^8.0.0",
30
30
  "typescript": "^5.3.0",
31
31
  "vitest": "^2.0.0",
32
- "@constela/server": "9.0.0"
32
+ "@constela/server": "10.0.1"
33
33
  },
34
34
  "engines": {
35
35
  "node": ">=20.0.0"