All files / src/core validarfecha.ts

100% Statements 24/24
100% Branches 27/27
100% Functions 2/2
100% Lines 22/22

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 461x 1x 1x 1x             1x 325x   325x 311x   311x 3x     308x 11x     297x 8x     289x 59x 54x     282x 3330x 7x       275x     50x      
import { CodigoFormatoFecha, diasDelMes, ErroresComunes } from "../constants";
import { getFormatoFecha } from "./getformatofecha";
import { disgregarFecha } from "./disgregarfecha";
import { isBisiesto } from "./bisiesto";
 
/**
 * 
 * @param fecha 
 * Valida la consistencia de una fecha dada
 */
export function validarFecha (fecha: string): CodigoFormatoFecha {
    try {
 
        getFormatoFecha(fecha);
        const {dia, mes, anio} = disgregarFecha(fecha);
    
        if (anio < 1901 || anio > 2150) {
            throw new Error(ErroresComunes.ERR_ANIO_INVALIDO);   
        }
 
        if (mes < 1 || mes  > 12) {
            throw new Error(ErroresComunes.ERR_MES_INVALIDO);
        }
 
        if (dia < 1 || dia > 31) {
            throw new Error(ErroresComunes.ERR_DIA_INVALIDO);
        }
 
        if (mes === '02') {
            if (!isBisiesto(anio) && dia > 28) { throw new Error(ErroresComunes.ERR_DIA_INVALIDO); }
            if (dia < 1 || dia > 29) { throw new Error(ErroresComunes.ERR_DIA_INVALIDO); }
        }
 
        diasDelMes.map(fecha => {
            if (mes !== '02' && mes === fecha.mes  && dia > fecha.dia) { 
                throw new Error(ErroresComunes.ERR_DIA_INVALIDO);
            }
        });
 
        return CodigoFormatoFecha.FMT_CORRECTO;
 
    } catch (error) {
        throw (error);
    }
 
}