def fix2range(vals, minval, maxval):
    """
    A helper function that sets the value of the array or number `vals` to
    fall within the range `minval` <= `vals` <= `maxval`.

    Values of `vals` that are greater than `maxval` are set to
    `maxval` (and similar for `minval`).

    Parameters
    ----------
    vals : float or array_like
           The value(s) to 'fix'.
    minval : float
             The minimum value.
    maxval : float
             The maximum value.

    Returns
    -------
    fixed_vals : float or array_like (matches `vals`)
                 The fixed values.
    """
    if not hasattr(vals, '__len__'):
        return max(min(vals, maxval), minval)
    vals[vals > maxval], vals[vals < minval] = maxval, minval
    return vals