# All files / src/utils loop.ts

100% Statements 59/59
100% Branches 8/8
100% Functions 1/1
100% Lines 59/59

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
    601x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    72x
    72x
    72x
    26x
    72x
    8x
    46x
    11x
    11x
    72x
    72x
    72x
     /**
     * @remarks \@since 2.7.0
     * @internal
     */
    export interface LoopOptions {
      /**
       * The current value that should be modified.
       */
      value: number;

      /**
       * An optional `min` value that can be used before looping to the `max` value.
       *
       * @defaultValue `0`
       */
      min?: number;

      /**
       * The max number that can be used before looping to the `min` value.
       */
      max: number;

      /**
       * Boolean if the `value` should be incremented or decremented by `1`.
       */
      increment: boolean;

      /**
       * Boolean if the looping should be ignored and only the `min`/`max` options
       * should be respected. In other words, the looping  behavior will be disabled
       * and the `value` must be: `min >= value <= max`
       */
      minmax?: boolean;
    }

    /**
     * A small util that is used to increment or decrement a number until it reaches
     * the max value or -1. When that happens, it will loop around to 0 or the max
     * value respectively. This does not work for different increment numbers or any
     * values below 0 for now.
     *
     * @internal
     * @param options - {@link LoopOptions}
     * @remarks \@since 2.7.0 The `min` option was added and the arguments changed to an
     * object
     */
    export function loop(options: LoopOptions): number {
      const { value, min = 0, max, increment, minmax = false } = options;
      let next = value + (increment ? 1 : -1);
      if (minmax) {
        next = Math.min(max, Math.max(min, next));
      } else if (next > max) {
        next = min;
      } else if (next < min) {
        next = max;
      }

      return next;
    }