Couple of custom written prototype methods for TypedArrays in JavaScript
Tags: javascript, prototyping, advanced
Published: 2022-04-07 17:28:16

I started transitioning from Node.js (_more precisely – JavaScript_) to Golang (Go) I've touched the subject of TypedArrays in general in [this article](https://dev.to/projektorius96/golang-perks-1ken) . As **TypedArray**s require[s] <u>**strictly** to be contained data of one type</u> , I decided I should share this abandoned custom prototype I've wrote recently that might be handy for some of you, folks ! :

```javascript

// In case I'd add new features :
// @https://codepen.io/projektorius96/pen/xxdvqpZ

// ===

// SYNOPSIS [].purify() :

let arr1 = ["4", 5, undefined, null, ""]; // concrete practical example
Array.prototype.purify = function(){ 

    for (let index = 0; index < /* arr1 */this.length; index++){
    for (let nonExsisting of [undefined, null, ""]) {
        if (/* arr1 */this[index] == nonExsisting) { /* arr1 */this[index] = "0"; /* console.log(' converted to "0" successfully ') */ } 
        else { /* console.log(' item was already typeof "string" ') */ };
    }
}
for (let item in /* arr1 */this) { if(typeof /* arr1 */this[item] != "string") {/* arr1 */this[item] = /* arr1 */this[item].toString(); console.log("value stringified: ", ) } }

return this;
}

let res = arr1.purify();
console.log(res);

// ===

// SYNOPSIS [].deleteItems(/* atPos, end, included = false | if true - end will be included */) :

let months = ['Jan', 'March', 'April', 'June'];
let variousTypes = ["4", 5, undefined, null, ""];
Array.prototype.deleteItems = function(atPos, end, included = false){
    for (let pointer = 0; pointer < this.length ; pointer++) {
            // console.log("BEFORE IF: ", pointer, atPos, end)
         if (included) {

             if (pointer == atPos && atPos <= end) {
                // console.log("before delete & increment: ", pointer, atPos, end)
                delete this[pointer];
                atPos++; // 1
                // console.log("after delete & increment: ", pointer, atPos , end);
            }
            // break; [this was a culprit so commented out]
         }

              else {

                if (pointer == atPos && atPos < end) {
                // console.log("before delete & increment: ", pointer, atPos, end)
                delete this[pointer];
                atPos++; // 1
                // console.log("after delete & increment: ", pointer, atPos , end);
                }
                
              }

         }
          
     return this;
}

variousTypes.deleteItems(0, 2)
console.log("(end) not included", variousTypes);

variousTypes = ["4", 5, undefined, null, ""]; // restore variousTypes for another try ;
// IMPORTANT NOTICE : even without restoring variousTypes <empty> indices will be iterated over whatsoever i.e. not ignored by pointer

variousTypes.deleteItems(0, 2, true);
console.log("(end) is included", variousTypes);

months.deleteItems(0,2);
console.log("months after deletion: ", months); // months after deletion: (4) [empty × 2, "April", "June"]
months = ['Jan', 'March', 'April', 'June']; // restore values to (4) ["Jan", "March", "April", "June"] for another try ;
months.deleteItems(0,2, true); // (4) [empty × 3, "June"]

// p.s. I left my comments at some point as someone may find it helpful whilst examining each prototype

```


In each augment **Array.prototype** the _keyword_ `this` should be replaced with a real life single dimensional array ; in case you have **_n-th_ multi-dimension array** , first make it to be de-flatted with built-in [.flat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) & only when apply my provided custom prototype methods .

---

Hopefully someone will find it helpful, thanks & see you in the next one !