According to the documentation on the Uint8ClampedArray,
The Uint8ClampedArray typed array represents an array of 8-bit unsigned integers clamped to 0-255; if you specified a value that is out of the range of [0,255], 0 or 255 will be set instead.
Other TypedArrays function similarly. Given any typed array amongst the types listed, is there a way to programmatically derive the max/min value possible to store therein?
Something along the lines of:
Uint8ClampedArray().maxItemValue // returns 255
I'd use the following:
function maxElementValue(arr) {
const c = arr.constructor;
const test = c.of(-1.5)[0];
if (test > 0) // unsigned integers
return test;
// return 0xFFFFFFFF >>> (32 - 8 * c.BYTES_PER_ELEMENT);
// return Math.pow(2, 8 * c.BYTES_PER_ELEMENT) - 1;
if (test == -1) // signed integers
return 0x7FFFFFFF >>> (32 - 8 * c.BYTES_PER_ELEMENT);
// return Math.pow(2, 8 * c.BYTES_PER_ELEMENT - 1) - 1;
if (test == 0) // clamped
return 0xFF; // there's only one of these
if (test == -1.5)
throw new TypeError("floats are not supported");
throw new TypeError("weirdly behaving typed array");
}