DEV Community

Lukas Gaucas
Lukas Gaucas

Posted on

Couple of custom written prototype methods for TypedArrays in JavaScript

I started transitioning from Node.js (more precisely – JavaScript) to Golang (Go) I've touched the subject of TypedArrays in general in this article . As TypedArrays require[s] strictly to be contained data of one type , I decided I should share this abandoned custom prototype I've wrote recently that might be handy for some of you, folks ! :


// 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

Enter fullscreen mode Exit fullscreen mode

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() & only when apply my provided custom prototype methods .


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

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

You might want to consider using my 'Metho' library to monkey patch native prototypes in a safe way.

Collapse
 
projektorius96 profile image
Lukas Gaucas

Never heard about it , appreciated ! <345