(points: ReadonlyArray<DataPoint>)
| 57 | * ``` |
| 58 | */ |
| 59 | export 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 |
no test coverage detected