# All files / src/utils withinRange.ts

100% Statements 33/33
100% Branches 3/3
100% Functions 1/1
100% Lines 33/33

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
    341x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    73x
    73x
    73x
    73x
    70x
    70x
    73x
    73x
    70x
    70x
    73x
    73x
    73x
     /**
     * @internal
     * @remarks \@since 6.0.0
     */
    export interface WithinRangeOptions {
      min: number | undefined;
      max: number | undefined;
      value: number;
    }

    /**
     * A simple util that will ensure that a number is within the optional min and
     * max values.
     *
     * @returns the updated value
     * @remarks
     * \@since 2.5.0
     * \@since 6.0.0 Converted to using an object instead of multiple arguments.
     */
    export function withinRange(options: WithinRangeOptions): number {
      const { min, max, value } = options;

      let nextValue = value;
      if (typeof min === "number") {
        nextValue = Math.max(min, nextValue);
      }

      if (typeof max === "number") {
        nextValue = Math.min(max, nextValue);
      }

      return nextValue;
    }