MCPcopy Index your code
hub / github.com/ChartGPU/ChartGPU / packDataPoints

Function packDataPoints

src/data/packDataPoints.ts:59–118  ·  view source on GitHub ↗
(points: ReadonlyArray<DataPoint>)

Source from the content-addressed store, hash-verified

57 * ```
58 */
59export function packDataPoints(points: ReadonlyArray<DataPoint>): Float32Array {
60 // Input validation
61 if (!points) {
62 throw new TypeError('packDataPoints: points parameter is required');
63 }
64
65 if (!Array.isArray(points)) {
66 throw new TypeError('packDataPoints: points must be an array');
67 }
68
69 if (points.length === 0) {
70 // Return empty array for empty input (valid case)
71 return new Float32Array(0);
72 }
73
74 // Validate array length doesn't exceed safe limits
75 // Max safe array size: ~2GB / 8 bytes = 268M points
76 const MAX_POINTS = 268_435_456; // 2^28 points = 2GB buffer
77 if (points.length > MAX_POINTS) {
78 throw new RangeError(
79 `packDataPoints: points array too large (${points.length} points). ` +
80 `Maximum supported: ${MAX_POINTS.toLocaleString()} points (2GB buffer limit)`
81 );
82 }
83
84 // Allocate buffer: 2 floats per point × 4 bytes per float = 8 bytes per point
85 const buffer = new ArrayBuffer(points.length * 2 * 4);
86 const f32 = new Float32Array(buffer);
87
88 for (let i = 0; i < points.length; i++) {
89 const point = points[i];
90
91 // Validate point is not null/undefined
92 if (point === null || point === undefined) {
93 throw new TypeError(
94 `packDataPoints: Invalid point at index ${i}. ` +
95 `Expected DataPoint (tuple or object), got ${point}`
96 );
97 }
98
99 const x = isTupleDataPoint(point) ? point[0] : point.x;
100 const y = isTupleDataPoint(point) ? point[1] : point.y;
101
102 // Validate numeric values (catches NaN, undefined properties)
103 if (typeof x !== 'number' || typeof y !== 'number') {
104 throw new TypeError(
105 `packDataPoints: Invalid coordinate values at index ${i}. ` +
106 `Expected numbers, got x=${typeof x}, y=${typeof y}`
107 );
108 }
109
110 // Note: NaN and Infinity are valid Float32 values and will be preserved
111 // If you need to reject them, add additional checks here
112
113 f32[i * 2 + 0] = x;
114 f32[i * 2 + 1] = y;
115 }
116

Callers 6

setSeriesFunction · 0.90
appendSeriesFunction · 0.90
benchmarkTypedArrayCopyFunction · 0.90
streamFrameFunction · 0.85
mainFunction · 0.85

Calls 1

isTupleDataPointFunction · 0.70

Tested by

no test coverage detected