All files clamp.ts

100% Statements 10/10
87.5% Branches 7/8
100% Functions 2/2
100% Lines 10/10

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 478x                                                         8x 3x       3x 2x 1x     2x 2x 1x     1x    
import { purry } from './purry';
 
/**
 * Clamp the given value within the inclusive min and max bounds.
 * @param value - the number
 * @param limits - the bounds limits
 * @signature
 *    P.clamp(value, { min, max });
 * @signature
 *    P.clamp({ min, max })(value);
 * @example
 *    P.clamp(10, { min: 20 }) // => 20
 *    P.clamp(10, { max: 5 }) // => 5
 *    P.clamp(10, { max: 20, min: 5 }) // => 10
 *
 *    P.clamp({ min: 20 })(10) // => 20
 *    P.clamp({ max: 5 })(10) // => 5
 *    P.clamp({ max: 20, min: 5 })(10) // => 10
 * @category Number, Pipe
 */
export function clamp(
  value: number,
  limits: { min?: number; max?: number }
): number;
export function clamp(limits: {
  min?: number;
  max?: number;
}): (value: number) => number;
 
export function clamp() {
  return purry(_clamp, arguments);
}
 
function _clamp(value: number, limits: { min?: number; max?: number }) {
  if (limits.min != null) {
    if (limits.min > value) {
      return limits.min;
    }
  }
  Eif (limits.max != null) {
    if (limits.max < value) {
      return limits.max;
    }
  }
  return value;
}