All files / src helper.numbers.js

100% Statements 45/45
100% Branches 28/28
100% Functions 12/12
100% Lines 45/45

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                                          27x             24x                     24x   4x       20x   24x   2x   22x   2x   20x   2x   22x               4x   1x   3x 3x   1x   2x               5x   1x   4x 4x 4x   2x   2x               29x   4x   25x             116x   50x   116x             512x             32x             334x               295x   250x   45x             65x   77x 77x   60x     5x               66x   59x   7x          
/*
 * Part of Pleiar.no - a collection of tools for nurses
 *
 * Copyright (C) Eskild Hustvedt 2017-2018
 * Copyright (C) Fagforbundet 2019-2021
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
// @flow
 
import { getValueFromLiteralOrEvent } from './helper.general';
import type { ReduxEventOrString } from './types/callbacks';
 
/**
 * This provides some static helper functions for formatting and
 * parsing numbers.
 */
const numbers = {
    /**
     * This parses a ReduxEventOrString and then hands it off to
     * `numbers.parseFloatish`.
     */
    parseFloatishFromThing (thing: ReduxEventOrString): "" | number
    {
        return numbers.parseFloatish( getValueFromLiteralOrEvent(thing) );
    },
    /**
     * This parses a ReduxEventOrString, hands it off to numbers.parseFloatish
     * and then ensures that it is within the range supplied. If it is below the range,
     * it sets it to `min`, if it is above the range it sets it to `max`.
     * If it's an empty string, that's just returned directly.
     */
    floatishInRangeFromThing(thing: ReduxEventOrString | number, min: number, max: number): "" | number
    {
        let value: number | "";
        if(typeof(thing) === "number")
        {
            value = thing;
        }
        else
        {
            value = numbers.parseFloatishFromThing(thing);
        }
        if(value === "")
        {
            return value;
        }
        if(value > max)
        {
            value = max;
        }
        else if(value < min)
        {
            value = min;
        }
        return value;
    },
    /**
     * This does basically the same thing as parseFloatishFromThing, but
     * it guarantees that it will return a number (or NaN).
     */
    getFloatishFromThing (thing: number | ReduxEventOrString): number
    {
        if(typeof(thing) === "number")
        {
            return thing;
        }
        const value = getValueFromLiteralOrEvent(thing);
        if(value === "")
        {
            throw('getFloatishFromThing: got empty string');
        }
        return numbers.parseFloat(value);
    },
    /**
     * A third variant of parseFloatishFromThing. This one will return zero
     * if it ends up with a string
     */
    getFloatishOrZeroFromThing (thing: number | string | SyntheticEvent<HTMLInputElement>): number
    {
        if(typeof(thing) === "number")
        {
            return thing;
        }
        const valueFromThing = getValueFromLiteralOrEvent(thing);
        const result = numbers.parseFloat(valueFromThing);
        if(Number.isNaN(result))
        {
            return 0;
        }
        return result;
    },
    /**
     * A function that either returns an empty string, or the result from
     * `numbers.parseFloat`
     */
    parseFloatish (toParse: string): "" | number
    {
        if(toParse === "")
        {
            return "";
        }
        return numbers.parseFloat(toParse);
    },
    /**
     * A `Number.parseFloat` wrapper that handles both . and ,-delimited numbers.
     */
    parseFloat (toParse: string): number
    {
        if(toParse.replace !== undefined)
        {
            toParse = toParse.replace(/,/g,'.');
        }
        return Number.parseFloat(toParse);
    },
    /**
     * Formats a number according to the locale (no)
     */
    format(number: number): string
    {
        return Number(number).toLocaleString("no");
    },
    /**
     * Rounds a number to its closest 10
     */
    roundNearestTen(number: number): number
    {
        return Math.round(number / 10) * 10;
    },
    /**
     * Rounds a number to a fixed maximum of decimal places
     */
    round(no: number, roundTo: number = 4): number
    {
        return (+Number(no).toFixed(roundTo));
    },
    /**
     * Variant of `.round()` that only applies rounding if the roundingEnabled
     * parameter is true
     */
    conditionalRound(no: number, roundingEnabled: boolean, roundTo: number = 4): number
    {
        if(roundingEnabled)
        {
            return numbers.round(no,roundTo);
        }
        return no;
    },
    /**
     * Rounds a number to its simplest significant decimal.
     */
    roundToSimplestSignificant(no: number): number
    {
        for(const attempt of [0, 2, 3])
        {
            const result = numbers.round(no,attempt);
            if(result > 0)
            {
                return result;
            }
        }
        return no;
    },
    /**
     * Variant of `.roundToSimplestSignificant` that only applies rounding if
     * the roundingEnabled parameter is true
     */
    conditionalRoundToSimplestSignificant(no: number, roundingEnabled: boolean): number
    {
        if(roundingEnabled)
        {
            return numbers.roundToSimplestSignificant(no);
        }
        return no;
    }
};
 
export { numbers };