UNPKG

@naverpay/hidash

Version:

improved lodash

37 lines (35 loc) 2.01 kB
/** * @description * Clamps `number` within inclusive bounds. The interpretation of `bounds` and `upperBound` arguments * determines the effective lower and upper limits for clamping: * * 1. **If `upperBound` is `undefined` (e.g., `clamp(number, singleBound)`):** * - `bounds` (the second argument) is treated as the **upper** limit. * - There is no explicit lower limit defined by the parameters. In this scenario, for the internal logic, * `number` itself effectively acts as the floor in the `Math.max(number, floor_value)` step, * meaning clamping primarily occurs against the provided upper limit. * * 2. **If `upperBound` is provided (e.g., `clamp(number, lowerLimit, upperLimit)`):** * - `bounds` (the second argument) is treated as the **lower** limit. * - `upperBound` (the third argument) is treated as the **upper** limit. * * Special handling for `NaN` values: * - If the input `number` itself is `NaN`, the function returns `NaN`. * - If any of the determined effective lower or upper limits (after parameter resolution and defaulting) * is `NaN`, the function returns `0`. * * This function is conceptually similar to Lodash's `_.clamp`, but specific behaviors (like * `NaN` handling for bounds and parameter interpretation) might differ from the linked Lodash version. * @see https://unpkg.com/lodash.clamp@4.0.3/index.js * * @param {number} number - The number to clamp. * @param {number} [bounds] - This argument's role depends on `upperBound`. * If `upperBound` is undefined, `bounds` acts as the upper limit. * Otherwise, `bounds` acts as the lower limit. * @param {number} [upperBound] - If provided, this is the upper limit, and the `bounds` * argument is treated as the lower limit. * @returns {number} The clamped number. Returns `NaN` if the input `number` is `NaN`. * Returns `0` if the determined effective bounds are `NaN`. */ declare function clamp(number: number, bounds?: number, upperBound?: number): number; export { clamp, clamp as default };