All files proto.js

100% Statements 49/49
100% Branches 12/12
100% Functions 4/4
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 6x 22x 6x 16x 10x 10x 10x 1x   1x 4x 1x   1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x  
/**
 * Classe che contiene una lista di prototype.
 * @class
 */
export class Proto {
    constructor() {
        /**
         * Converte una stringa in un booleano, seguendo le regole indicate di seguito:
         * - La stringa "true" o "1" viene convertita in true.
         * - La stringa "false" o "0" viene convertita in false.
         * - Se la stringa non corrisponde a nessuno di questi valori, viene generata un'eccezione.
         *
         * @function
         * @memberof String.prototype
         * @returns {boolean} Restituisce un booleano in base al valore della stringa.
         * @throws {Error} Se la stringa non รจ una stringa booleana valida.
         * @example
         * const myString = 'True';
         * const myBoolean = myString.toBoolean();
         * console.log(myBoolean); // Output: true
         */
        String.prototype.toBoolean = function () {
            // Converti la stringa in caratteri minuscoli e rimuovi gli spazi bianchi.
            const lowerCaseString = this.toLowerCase().trim();
            // Confronta la stringa minuscola senza spazi bianchi con i valori booleani validi.
            if (lowerCaseString === "yes" || lowerCaseString === "true" || lowerCaseString === "1") {
                return true;
            } else if (lowerCaseString === "no" || lowerCaseString === "false" || lowerCaseString === "0") {
                return false;
            } else {
                // Se la stringa non corrisponde a nessun valore booleano valido, genera un'eccezione.
                throw new Error(`Invalid boolean string: ${this}`);
            }
        };
 
        String.prototype.toCapitalize = function () {
            return this.charAt(0).toUpperCase() + this.slice(1);
        };
 
        /**
         * Aggiunge al prototipo di Number un metodo toBoolean che converte il numero in una stringa
         * e applica la funzione toBoolean della stringa.
         * @returns {boolean} Il valore booleano corrispondente alla stringa convertita dal numero.
         * @throws {Error} Se la stringa non corrisponde a nessun valore booleano valido.
         */
        Number.prototype.toBoolean = function () {
            // Converti il numero in una stringa e applica la funzione toBoolean della stringa.
            return String(this).toBoolean();
        };
    }
}