JSFiddle - React, Tailwind, and code Playground

by KgomDr

JavaScript

const Endianness = new Uint8Array(new Uint32Array([0x12345678]).buffer)[0] === 0x78 ? "little" : "big";

const isIntString = value => {
    if (/^[-+]?(\d+)$/.test(value))
        return !isNaN(value)
    else
        return false
}

const byte_to_bits = byte => {
    if (byte > 255 || byte < 0 || isNaN(byte))
        throw new Error("Byte is smaller than 0 or bigger than 255.");
    const ret = new Array(8);
    for (let i = 0; i < 8; i++)
        ret[i] = (byte >> i & 1) === 1 ? 1 : 0;
    // if the least significant bit is placed last.
    if (Endianness === "big")
        ret.reverse();
    return ret
}

const bits_to_byte = bits => {
    if (Endianness === "big") {
        bits = bits.slice();
        bits.reverse();
    }
    return Number.parseInt(bits.join(""), 2)
};

class Uint1Array extends Array {
    name = "Uint1Array";

    constructor(param) {
        let bytearray;
        if (param instanceof Array) {
            const array = new Array(Math.ceil(param.length / 8));
            for (let i = 0; i < param.length; i++)
                array[i] = bits_to_byte(param.slice(i * 8, (i + 1) * 8));
            super(param.length);
            bytearray = new Uint8Array(array);
            Object.defineProperty(this, 'bytearray', bytearray);
        } else {
            bytearray = new Uint8Array(param);
            super(bytearray.length * 8);
            Object.defineProperty(this, 'bytearray', bytearray);
        }
        // the Uint8Array that holds the bytes/bits.

        const get = (target, prop) => {
            if (typeof prop !== "symbol" && isIntString(prop)) {
                const quotient = ~~(prop / 8);
                const remainder = prop % 8;
                return byte_to_bits(bytearray[quotient])[remainder]
            } else {
                return target[prop]
            }
        };

        const set = (target, prop, value) => {
            if (typeof prop !== "symbol" && isIntString(prop)) {
                const quotient =...