Prototypes.js

If you use prototypes.js in Node, the html prototypes will be based on the Object type. Ex: HTMLSelectElement.prototype.getTextOptSelected -> Object.prototype.getTextOptSelected. They will work the same way, it will only change the specific object it is aimed at.

Usage



                                    


                                    

Arrays

# orderBy

This prototype orders an array by string, date or number according to the attribute and in both ascending and descending order.

Code


                                    
Example
# groupByAttribute

This prototype groups an array by category/attribute. Just work in a new variable.

Code


                                    
Example
# fusionByAttribute

This prototype merges arrays and groups them based on the value of an attribute that matches between them. Just work in a new variable.

Code


                                    
Example
# removeItemsByIndex

This prototype removes the elements of an array based on their index, respecting the initial order

Code


                                    
Example

Strings

Strings prototype set.

# capFirstLetter

This prototype makes the first letter uppercase.


    
                                        
Example
# addLineBreak

This prototype does a line break every x number of characters.


    
                                        
Example
# validateEmail

This prototype validates if an email is valid or not.


    
                                        
Example
@
The email entered is valid!
The email entered is not valid

Numbers

Numbers prototype set.

# formatPrice

This prototype gives the price format "x.xxx", "xx.xxx", etc.



                                        
# roundPrice

This prototype rounds the number in base 10, "0-5" remains at 0, "6-9" goes to the next number in base 10.


    
                                        

Select

Select elements prototype set.

# getTextOptSelected

This prototype gets the text of the selected option.


    
                                        
Example
# getDataOptSelected

This prototype gets the specified attribute of the selected option from the select element.


    
                                        
Example
# getAllDataOptSelected

This prototype gets all the attributes of the selected option from the select element.


    
                                        
Example
# getValuesMultipleOpts

This prototype gets the values of the selected options from the select multiple element.


    
                                        
Example
# setValue

This prototype change the value of a select element.

Code

        
                                        
Example
# setMultipleValues

This function assign values to a select multiple.

Code

        
                                        
Example

Table

Table prototype set.

# addRow

This prototype add a row to an HTML table in a designed position.

Code

        
                                        
Example
Column 1 Column 2 Column 3 Column 4 Column 5
Data 1 Data 2 Data 3 Data 4 Data 5

TableRow

TableRow prototype set.

# deleteRow

This prototype delete a row from an HTML table.

Code

        
                                        
Example
Column 1 Column 2 Column 3 Column 4 Action
Data 1 Data 2 Data 3 Data 4
Data 5 Data 6 Data 7 Data 8
Data 9 Data 10 Data 11 Data 12

Input / TextArea

Input / TextArea elements prototype set.

# simulateKeyPress

This function simulates that the user is pressing a key and writes the character in the input.

Code
/**
 * This prototype simulates that the user is pressing a key and writes the character in the input
 * 
 * @param {string|number} value value to write to input
 */
HTMLInputElement.prototype.simulateKeyPress =  function simulateKeyPress(value){
    let event = new Event('keypress');
    let text = value.toString();
    let index = 0;

    setInterval(addCharacter, 100);

    function addCharacter() {
        if (index < text.length) {
            var currentChar = text.charAt(index);
            this.value += currentChar;
            this.dispatchEvent(event);
            index++;
        } else {
            clearInterval();
        }
    }
}

HTMLTextAreaElement.prototype.simulateKeyPress =  function simulateKeyPress(value){
    let event = new Event('keypress');
    let text = value.toString();
    let index = 0;

    setInterval(addCharacter, 100);

    function addCharacter() {
        if (index < text.length) {
            var currentChar = text.charAt(index);
            this.value += currentChar;
            this.dispatchEvent(event);
            index++;
        } else {
            clearInterval();
        }
    }
}
                                        
Example