All files / components/number-input number-input-utils.ts

100% Statements 19/19
100% Branches 14/14
100% Functions 3/3
100% Lines 16/16

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 361x           8x 8x 1x   7x 2x   5x 2x   3x     1x             4x 4x 4x     1x 3x 3x    
export const getValidatedValue = (
    newValue: any,
    oldValue: number,
    max: number,
    min: number
): number => {
    const newNumber = +newValue;
    if (isNaN(newNumber)) {
        return oldValue;
    }
    if (!isNaN(max) && newNumber > max) {
        return max;
    }
    if (!isNaN(min) && newNumber < min) {
        return min;
    }
    return Math.round(newNumber);
};
 
export const calculateValue = (
    type: string,
    value: number,
    increment: number,
    max: number,
    min: number
) => {
    const factor = type === "decrement" ? -1 : 1;
    const newValue = value + factor * increment;
    return getValidatedValue(newValue, value, max, min);
};
 
export const getNumberValue = (string: string): number => {
    const number = string === "" ? NaN : +string;
    return number;
};