{"version":3,"file":"clamp-zT1oLiyc.cjs","names":[],"sources":["../src/functions/clamp/clamp.ts"],"sourcesContent":["import type { CastToString } from '../../types';\n\ninterface ClampOptions {\n  /**\n   * Whether the clamping should be inclusive of the range.\n   * @default true\n   */\n  inclusive?: boolean | 'min' | 'max';\n}\n\n/**\n * Clamps a number to a specified range.\n * @param value The number to clamp.\n * @param range The range to clamp to, as a tuple of min and max values.\n * @param options Optional parameters.\n * @param options.inclusive Whether the clamping should be inclusive of the min and/or max value.\n * - If `true`, the clamping is inclusive of both min and max.\n * - If `false`, the clamping is exclusive of both min and max.\n * - If `'min'`, the clamping is inclusive of the min value but exclusive of the max value.\n * - If `'max'`, the clamping is exclusive of the min value but inclusive of the max value.\n * @returns The clamped number.\n * @example\n * ```typescript\n * clamp(3, [1, 5]); // 3\n * clamp(5, [1, 5]); // 5\n * clamp(5, [1, 5], { inclusive: false }); // 4\n * clamp(1, [1, 5], { inclusive: 'max' }); // 5\n * ```\n */\nexport function clamp(\n  value: number,\n  range: readonly [min: number, max: number],\n  options?: ClampOptions\n): number {\n  const [min, max] = range;\n  if (min > max) {\n    throw new RangeError(`Invalid range: [${min},${max}]`);\n  }\n\n  const { inclusive = true } = options ?? {};\n\n  return {\n    true: () => Math.max(min, Math.min(max, value)),\n    false: () => Math.max(min + 1, Math.min(max - 1, value)),\n    min: () => Math.max(min, Math.min(max - 1, value)),\n    max: () => Math.max(min + 1, Math.min(max, value)),\n  }[inclusive as CastToString<typeof inclusive>]();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,MACd,OACA,OACA,SACQ;CACR,MAAM,CAAC,KAAK,OAAO;AACnB,KAAI,MAAM,IACR,OAAM,IAAI,WAAW,mBAAmB,IAAI,GAAG,IAAI,GAAG;CAGxD,MAAM,EAAE,YAAY,SAAS,WAAW,EAAE;AAE1C,QAAO;EACL,YAAY,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,MAAM,CAAC;EAC/C,aAAa,KAAK,IAAI,MAAM,GAAG,KAAK,IAAI,MAAM,GAAG,MAAM,CAAC;EACxD,WAAW,KAAK,IAAI,KAAK,KAAK,IAAI,MAAM,GAAG,MAAM,CAAC;EAClD,WAAW,KAAK,IAAI,MAAM,GAAG,KAAK,IAAI,KAAK,MAAM,CAAC;EACnD,CAAC,YAA8C"}