swiing/Bit-TypedArray

Complete implementation of standard typed arrays

Opened this issue · 4 comments

Current code only partially implements standard features of typed arrays. This issue provides a place to track progress and view it all in one shot.

PRs welcome!

Constructor

  • new TypedArray()
  • new TypedArray(length)
  • new TypedArray(typedArray)
  • new TypedArray(object)
  • new TypedArray(buffer): see here

Unrelevant (= won't implement)

  • new TypedArray(buffer, byteOffset): see here
  • new TypedArray(buffer, byteOffset, length): see here

Static properties

  • BYTES_PER_ELEMENT
  • name
  • get BitArray[@@species]

Static methods

  • from()
  • of()

Instance properties

  • buffer
  • byteLength
  • byteOffset
  • length

Instance methods

  • at(): see here for discussion
  • copyWithin()
  • entries()
  • every()
  • fill()
  • filter()
  • find()
  • findIndex()
  • forEach()
  • includes()
  • indexOf()
  • join()
  • keys()
  • lastIndexOf()
  • map()
  • reduce()
  • reduceRight()
  • reverse()
  • set()
  • slice()
  • some()
  • sort()
  • subarray()
  • values()
  • toString(): see here for note on implementation.
  • [@@iterator]()

Irrelevant (= won't implement)

  • toLocaleString(): there is no local variation to displaying 1s or 0s.

Fwiw, I got a hacky horrible inefficient version of "ArrayBuffer to BitArray" to work via the following

    const vals = []
    for(const b of new Uint8Array(buffer)) {
        for (let i = 0; i < 8; i++) {
            vals.push(!!(b & (1 << i)))
        }
    }
    let arr = BitArray.from(vals)

Obviously the correct version doesn't have so much unnecessary byte->string->byte conversion :)

Indeed! :)

Tbh, I have not yet made my mind whether it makes sense to construct a BitArray by passing an array buffer to the constructor. FFS...

I actually have a use case! I have a settings object in my react app; it has a great number of boolean or near boolean enums. I want to store the user settings in a query param, so I load the settings into a bit array, and then turn that into base64 from that array buffer. This allows the settings to be shared or survive a page reload. To deserialize, I load the bas64 string back into array buffer, and then into the bitarray to reconstruct the settings.

@rollie42, in order to not pollute this general-purpose issue, I have created a specific issue here.