@constela/runtime 0.16.6 → 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 +328 -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)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/runtime",
3
- "version": "0.16.6",
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"