@constela/core 0.18.2 → 0.18.4
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 +76 -1
- package/dist/index.js +133 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2438,4 +2438,79 @@ declare function isSuspenseBoundary(value: unknown): value is SuspenseBoundary;
|
|
|
2438
2438
|
*/
|
|
2439
2439
|
declare function isStreamChunk(value: unknown): value is StreamChunk;
|
|
2440
2440
|
|
|
2441
|
-
|
|
2441
|
+
/**
|
|
2442
|
+
* Chart Helper Functions
|
|
2443
|
+
*
|
|
2444
|
+
* Shared chart helper functions used by both runtime evaluator and SSR renderer.
|
|
2445
|
+
* This module provides coordinate calculation, path generation, and data utilities
|
|
2446
|
+
* for chart rendering.
|
|
2447
|
+
*/
|
|
2448
|
+
/**
|
|
2449
|
+
* Normalizes a value to 0-1 range
|
|
2450
|
+
*
|
|
2451
|
+
* @param value - The value to normalize
|
|
2452
|
+
* @param min - The minimum value of the range
|
|
2453
|
+
* @param max - The maximum value of the range
|
|
2454
|
+
* @returns Normalized value (0-1), or undefined for invalid input
|
|
2455
|
+
*/
|
|
2456
|
+
declare function normalizeValue(value: unknown, min: unknown, max: unknown): number | undefined;
|
|
2457
|
+
/**
|
|
2458
|
+
* Scales a value from one range to another
|
|
2459
|
+
*
|
|
2460
|
+
* @param value - The value to scale
|
|
2461
|
+
* @param domainMin - Minimum of source range
|
|
2462
|
+
* @param domainMax - Maximum of source range
|
|
2463
|
+
* @param rangeMin - Minimum of target range
|
|
2464
|
+
* @param rangeMax - Maximum of target range
|
|
2465
|
+
* @returns Scaled value, or undefined for invalid input
|
|
2466
|
+
*/
|
|
2467
|
+
declare function scaleValue(value: unknown, domainMin: unknown, domainMax: unknown, rangeMin: unknown, rangeMax: unknown): number | undefined;
|
|
2468
|
+
/**
|
|
2469
|
+
* Finds min and max values in data
|
|
2470
|
+
*
|
|
2471
|
+
* @param data - Array of objects
|
|
2472
|
+
* @param valueKey - Key to use for values
|
|
2473
|
+
* @returns Object with min and max, or undefined for invalid input
|
|
2474
|
+
*/
|
|
2475
|
+
declare function getChartBounds(data: unknown, valueKey: unknown): {
|
|
2476
|
+
min: number;
|
|
2477
|
+
max: number;
|
|
2478
|
+
} | undefined;
|
|
2479
|
+
/**
|
|
2480
|
+
* Generates scaled line chart points from data.
|
|
2481
|
+
*
|
|
2482
|
+
* @param data - Array of data objects
|
|
2483
|
+
* @param valueKey - Key to extract values from data
|
|
2484
|
+
* @param width - Chart width
|
|
2485
|
+
* @param height - Chart height
|
|
2486
|
+
* @param padding - Padding around chart area (default: 40)
|
|
2487
|
+
* @returns Array of {x, y} points scaled to fit the chart area
|
|
2488
|
+
*/
|
|
2489
|
+
declare function getLinePoints(data: unknown, valueKey: unknown, width: unknown, height: unknown, padding?: unknown): Array<{
|
|
2490
|
+
x: number;
|
|
2491
|
+
y: number;
|
|
2492
|
+
}> | undefined;
|
|
2493
|
+
/**
|
|
2494
|
+
* Generates SVG path string for line chart
|
|
2495
|
+
*
|
|
2496
|
+
* @param points - Array of {x, y} points
|
|
2497
|
+
* @param curved - Whether to use curved lines (Catmull-Rom spline)
|
|
2498
|
+
* @returns SVG path string, or undefined for invalid input
|
|
2499
|
+
*/
|
|
2500
|
+
declare function getLinePath(points: unknown, curved?: unknown): string | undefined;
|
|
2501
|
+
/**
|
|
2502
|
+
* Registry of chart helper functions that can be called from expressions.
|
|
2503
|
+
* This is used by both runtime evaluator and SSR renderer.
|
|
2504
|
+
*/
|
|
2505
|
+
declare const CHART_HELPERS: Record<string, (...args: unknown[]) => unknown>;
|
|
2506
|
+
/**
|
|
2507
|
+
* Calls a chart helper function by name.
|
|
2508
|
+
* Returns undefined if the function is not found.
|
|
2509
|
+
*
|
|
2510
|
+
* @param method - The function name to call
|
|
2511
|
+
* @param args - Arguments to pass to the function
|
|
2512
|
+
* @returns The function result, or undefined if not found
|
|
2513
|
+
*/
|
|
2514
|
+
declare function callChartHelper(method: string, args: unknown[]): unknown;
|
|
2515
|
+
|
|
2516
|
+
export { AI_OUTPUT_TYPES, AI_PROVIDER_TYPES, type ActionDefinition, type ActionStep, type AiDataSource, type AiOutputType, type AiProviderType, type ArrayExpr, BINARY_OPERATORS, type BinExpr, type BinaryOperator, type BindStep, type BooleanField, CHART_HELPERS, CLIPBOARD_OPERATIONS, COLOR_SCHEMES, type CallExpr, type CallStep, type ClearTimerStep, type ClipboardOperation, type ClipboardStep, type CloseStep, type CodeNode, type ColorScheme, type ComponentDef, type ComponentNode, type ComponentsRef, type CompoundVariant, type ConcatExpr, type CondExpr, type ConfirmStep, type ConstelaAst, ConstelaError, type ConstelaProgram, type CookieInitialExpr, DATA_SOURCE_TYPES, DATA_TRANSFORMS, type DataExpr, type DataSource, type DataSourceType, type DataTransform, type DelayStep, type DisposeStep, type DomStep, type EachNode, type ElementNode, type ErrorBoundaryNode, type ErrorCode, type ErrorOptions, type EventHandler, type EventHandlerOptions, type Expression, FOCUS_OPERATIONS, type FetchStep, type FlushStrategy, type FocusOperation, type FocusStep, type GenerateStep, type GetExpr, HTTP_METHODS, type HttpMethod, ISLAND_STRATEGIES, type IfNode, type IfStep, type ImportExpr, type ImportStep, type IndexExpr, type IntervalStep, type IslandNode, type IslandStrategy, type IslandStrategyOptions, type LambdaExpr, type LayoutProgram, type LifecycleHooks, type ListField, type LitExpr, type LocalActionDefinition, type LocalActionStep, type LocalExpr, type MarkdownNode, NAVIGATE_TARGETS, type NavigateStep, type NavigateTarget, type NotExpr, type NumberField, type ObjExpr, type ObjectField, type OptimisticStep, PARAM_TYPES, type ParamDef, type ParamExpr, type ParamType, type PortalNode, type Program, type ReconnectConfig, type RefExpr, type RejectStep, type RouteDefinition, type RouteExpr, type SSECloseStep, type SSEConnectStep, STORAGE_OPERATIONS, STORAGE_TYPES, type SendStep, type SetPathStep, type SetStep, type SlotNode, type StateExpr, type StateField, type StaticPathsDefinition, type StorageOperation, type StorageStep, type StorageType, type StreamChunk, type StreamChunkType, type StreamingRenderOptions, type StringField, type StyleExpr, type StylePreset, type SubscribeStep, type SuspenseBoundary, type SuspenseNode, type TextNode, type ThemeColors, type ThemeConfig, type ThemeFonts, UPDATE_OPERATIONS, type UnbindStep, type UpdateOperation, type UpdateStep, VALIDITY_PROPERTIES, type ValidationFailure, type ValidationResult, type ValidationSuccess, type ValidityExpr, type ValidityProperty, type VarExpr, type ViewNode, astSchema, callChartHelper, createClipboardWriteMissingValueError, createComponentCycleError, createComponentNotFoundError, createComponentPropMissingError, createComponentPropTypeError, createCondElseRequiredError, createDataNotDefinedError, createDuplicateActionError, createDuplicateDefaultSlotError, createDuplicateIslandIdError, createDuplicateSlotNameError, createImportsNotDefinedError, createInvalidClipboardOperationError, createInvalidDataSourceError, createInvalidNavigateTargetError, createInvalidSlotNameError, createInvalidStorageOperationError, createInvalidStorageTypeError, createLayoutMissingSlotError, createLayoutNotFoundError, createLocalActionInvalidStepError, createOperationInvalidForTypeError, createOperationMissingFieldError, createOperationUnknownError, createRouteNotDefinedError, createSchemaError, createSlotInLoopError, createStorageSetMissingValueError, createUndefinedActionError, createUndefinedDataError, createUndefinedDataSourceError, createUndefinedImportError, createUndefinedLocalStateError, createUndefinedParamError, createUndefinedRefError, createUndefinedRouteParamError, createUndefinedStateError, createUndefinedStyleError, createUndefinedVarError, createUndefinedVariantError, createUnsupportedVersionError, findSimilarNames, getChartBounds, getLinePath, getLinePoints, isActionStep, isAiDataSource, isArrayExpr, isBinExpr, isBooleanField, isCallStep, isClipboardStep, isCodeNode, isColorScheme, isComponentNode, isConcatExpr, isCondExpr, isConstelaError, isCookieInitialExpr, isDataExpr, isDataSource, isDisposeStep, isEachNode, isElementNode, isErrorBoundaryNode, isEventHandler, isExpression, isFetchStep, isFocusStep, isGenerateStep, isGetExpr, isIfNode, isImportExpr, isImportStep, isIslandNode, isIslandStrategy, isIslandStrategyOptions, isLayoutProgram, isLifecycleHooks, isListField, isLitExpr, isLocalActionDefinition, isLocalActionStep, isMarkdownNode, isNamedSlotNode, isNavigateStep, isNotExpr, isNumberField, isObjectField, isParamExpr, isPortalNode, isRefExpr, isRouteDefinition, isRouteExpr, isSetPathStep, isSetStep, isSlotNode, isStateExpr, isStateField, isStaticPathsDefinition, isStorageStep, isStreamChunk, isStreamingRenderOptions, isStringField, isStyleExpr, isSubscribeStep, isSuspenseBoundary, isSuspenseNode, isTextNode, isThemeColors, isThemeConfig, isThemeFonts, isUpdateStep, isValidityExpr, isVarExpr, isViewNode, normalizeValue, scaleValue, validateAst };
|
package/dist/index.js
CHANGED
|
@@ -2654,10 +2654,137 @@ function isStreamChunk(value) {
|
|
|
2654
2654
|
}
|
|
2655
2655
|
return true;
|
|
2656
2656
|
}
|
|
2657
|
+
|
|
2658
|
+
// src/helpers/chart.ts
|
|
2659
|
+
function normalizeValue(value, min, max) {
|
|
2660
|
+
if (typeof value !== "number" || typeof min !== "number" || typeof max !== "number") {
|
|
2661
|
+
return void 0;
|
|
2662
|
+
}
|
|
2663
|
+
if (max === min) {
|
|
2664
|
+
return 0;
|
|
2665
|
+
}
|
|
2666
|
+
return (value - min) / (max - min);
|
|
2667
|
+
}
|
|
2668
|
+
function scaleValue(value, domainMin, domainMax, rangeMin, rangeMax) {
|
|
2669
|
+
if (typeof value !== "number" || typeof domainMin !== "number" || typeof domainMax !== "number" || typeof rangeMin !== "number" || typeof rangeMax !== "number") {
|
|
2670
|
+
return void 0;
|
|
2671
|
+
}
|
|
2672
|
+
if (domainMax === domainMin) {
|
|
2673
|
+
return rangeMin;
|
|
2674
|
+
}
|
|
2675
|
+
const normalized = (value - domainMin) / (domainMax - domainMin);
|
|
2676
|
+
return rangeMin + normalized * (rangeMax - rangeMin);
|
|
2677
|
+
}
|
|
2678
|
+
function getChartBounds(data, valueKey) {
|
|
2679
|
+
if (!Array.isArray(data) || data.length === 0) {
|
|
2680
|
+
return void 0;
|
|
2681
|
+
}
|
|
2682
|
+
if (typeof valueKey !== "string") {
|
|
2683
|
+
return void 0;
|
|
2684
|
+
}
|
|
2685
|
+
const values = [];
|
|
2686
|
+
for (const item of data) {
|
|
2687
|
+
if (typeof item !== "object" || item === null) {
|
|
2688
|
+
continue;
|
|
2689
|
+
}
|
|
2690
|
+
const val = item[valueKey];
|
|
2691
|
+
if (typeof val === "number") {
|
|
2692
|
+
values.push(val);
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
if (values.length === 0) {
|
|
2696
|
+
return void 0;
|
|
2697
|
+
}
|
|
2698
|
+
return {
|
|
2699
|
+
min: Math.min(...values),
|
|
2700
|
+
max: Math.max(...values)
|
|
2701
|
+
};
|
|
2702
|
+
}
|
|
2703
|
+
function getLinePoints(data, valueKey, width, height, padding) {
|
|
2704
|
+
if (!Array.isArray(data) || data.length === 0) return void 0;
|
|
2705
|
+
if (typeof valueKey !== "string") return void 0;
|
|
2706
|
+
if (typeof width !== "number" || typeof height !== "number") return void 0;
|
|
2707
|
+
if (width <= 0 || height <= 0) return void 0;
|
|
2708
|
+
const pad = typeof padding === "number" ? padding : 40;
|
|
2709
|
+
if (typeof padding !== "undefined" && typeof padding !== "number") return void 0;
|
|
2710
|
+
const bounds = getChartBounds(data, valueKey);
|
|
2711
|
+
if (!bounds) return void 0;
|
|
2712
|
+
const { min, max } = bounds;
|
|
2713
|
+
const chartWidth = width - pad * 2;
|
|
2714
|
+
const chartHeight = height - pad * 2;
|
|
2715
|
+
return data.map((item, idx) => {
|
|
2716
|
+
const value = item[valueKey];
|
|
2717
|
+
if (typeof value !== "number") return { x: 0, y: 0 };
|
|
2718
|
+
const x = data.length === 1 ? pad : pad + idx / (data.length - 1) * chartWidth;
|
|
2719
|
+
const y = min === max ? pad + chartHeight / 2 : pad + chartHeight - scaleValue(value, min, max, 0, chartHeight);
|
|
2720
|
+
return { x, y };
|
|
2721
|
+
});
|
|
2722
|
+
}
|
|
2723
|
+
function getCurvedPath(points) {
|
|
2724
|
+
if (points.length < 2) {
|
|
2725
|
+
return points.length === 1 ? `M${points[0].x},${points[0].y}` : "";
|
|
2726
|
+
}
|
|
2727
|
+
if (points.length === 2) {
|
|
2728
|
+
return `M${points[0].x},${points[0].y} L${points[1].x},${points[1].y}`;
|
|
2729
|
+
}
|
|
2730
|
+
const pathParts = [`M${points[0].x},${points[0].y}`];
|
|
2731
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
2732
|
+
const p0 = points[Math.max(0, i - 1)];
|
|
2733
|
+
const p1 = points[i];
|
|
2734
|
+
const p2 = points[i + 1];
|
|
2735
|
+
const p3 = points[Math.min(points.length - 1, i + 2)];
|
|
2736
|
+
const cp1x = p1.x + (p2.x - p0.x) / 6;
|
|
2737
|
+
const cp1y = p1.y + (p2.y - p0.y) / 6;
|
|
2738
|
+
const cp2x = p2.x - (p3.x - p1.x) / 6;
|
|
2739
|
+
const cp2y = p2.y - (p3.y - p1.y) / 6;
|
|
2740
|
+
pathParts.push(`C${cp1x},${cp1y} ${cp2x},${cp2y} ${p2.x},${p2.y}`);
|
|
2741
|
+
}
|
|
2742
|
+
return pathParts.join(" ");
|
|
2743
|
+
}
|
|
2744
|
+
function getLinePath(points, curved) {
|
|
2745
|
+
if (!Array.isArray(points)) {
|
|
2746
|
+
return void 0;
|
|
2747
|
+
}
|
|
2748
|
+
if (points.length === 0) {
|
|
2749
|
+
return "";
|
|
2750
|
+
}
|
|
2751
|
+
for (const point of points) {
|
|
2752
|
+
if (typeof point !== "object" || point === null || typeof point.x !== "number" || typeof point.y !== "number") {
|
|
2753
|
+
return void 0;
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
const validPoints = points;
|
|
2757
|
+
if (validPoints.length === 1) {
|
|
2758
|
+
return `M${validPoints[0].x},${validPoints[0].y}`;
|
|
2759
|
+
}
|
|
2760
|
+
if (curved !== true) {
|
|
2761
|
+
const pathParts = validPoints.map((point, i) => {
|
|
2762
|
+
const command = i === 0 ? "M" : "L";
|
|
2763
|
+
return `${command}${point.x},${point.y}`;
|
|
2764
|
+
});
|
|
2765
|
+
return pathParts.join(" ");
|
|
2766
|
+
}
|
|
2767
|
+
return getCurvedPath(validPoints);
|
|
2768
|
+
}
|
|
2769
|
+
var CHART_HELPERS = {
|
|
2770
|
+
normalizeValue: (value, min, max) => normalizeValue(value, min, max),
|
|
2771
|
+
scaleValue: (value, domainMin, domainMax, rangeMin, rangeMax) => scaleValue(value, domainMin, domainMax, rangeMin, rangeMax),
|
|
2772
|
+
getChartBounds: (data, valueKey) => getChartBounds(data, valueKey),
|
|
2773
|
+
getLinePoints: (data, valueKey, width, height, padding) => getLinePoints(data, valueKey, width, height, padding),
|
|
2774
|
+
getLinePath: (points, curved) => getLinePath(points, curved)
|
|
2775
|
+
};
|
|
2776
|
+
function callChartHelper(method, args) {
|
|
2777
|
+
const fn = CHART_HELPERS[method];
|
|
2778
|
+
if (!fn) {
|
|
2779
|
+
return void 0;
|
|
2780
|
+
}
|
|
2781
|
+
return fn(...args);
|
|
2782
|
+
}
|
|
2657
2783
|
export {
|
|
2658
2784
|
AI_OUTPUT_TYPES,
|
|
2659
2785
|
AI_PROVIDER_TYPES,
|
|
2660
2786
|
BINARY_OPERATORS,
|
|
2787
|
+
CHART_HELPERS,
|
|
2661
2788
|
CLIPBOARD_OPERATIONS,
|
|
2662
2789
|
COLOR_SCHEMES,
|
|
2663
2790
|
ConstelaError,
|
|
@@ -2673,6 +2800,7 @@ export {
|
|
|
2673
2800
|
UPDATE_OPERATIONS,
|
|
2674
2801
|
VALIDITY_PROPERTIES,
|
|
2675
2802
|
astSchema,
|
|
2803
|
+
callChartHelper,
|
|
2676
2804
|
createClipboardWriteMissingValueError,
|
|
2677
2805
|
createComponentCycleError,
|
|
2678
2806
|
createComponentNotFoundError,
|
|
@@ -2715,6 +2843,9 @@ export {
|
|
|
2715
2843
|
createUndefinedVariantError,
|
|
2716
2844
|
createUnsupportedVersionError,
|
|
2717
2845
|
findSimilarNames,
|
|
2846
|
+
getChartBounds,
|
|
2847
|
+
getLinePath,
|
|
2848
|
+
getLinePoints,
|
|
2718
2849
|
isActionStep,
|
|
2719
2850
|
isAiDataSource,
|
|
2720
2851
|
isArrayExpr,
|
|
@@ -2786,5 +2917,7 @@ export {
|
|
|
2786
2917
|
isValidityExpr,
|
|
2787
2918
|
isVarExpr,
|
|
2788
2919
|
isViewNode,
|
|
2920
|
+
normalizeValue,
|
|
2921
|
+
scaleValue,
|
|
2789
2922
|
validateAst
|
|
2790
2923
|
};
|