mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-28 03:15:16 +00:00
40 lines
888 B
JavaScript
40 lines
888 B
JavaScript
|
import baseClamp from './_baseClamp.js';
|
||
|
import toNumber from './toNumber.js';
|
||
|
|
||
|
/**
|
||
|
* Clamps `number` within the inclusive `lower` and `upper` bounds.
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @since 4.0.0
|
||
|
* @category Number
|
||
|
* @param {number} number The number to clamp.
|
||
|
* @param {number} [lower] The lower bound.
|
||
|
* @param {number} upper The upper bound.
|
||
|
* @returns {number} Returns the clamped number.
|
||
|
* @example
|
||
|
*
|
||
|
* _.clamp(-10, -5, 5);
|
||
|
* // => -5
|
||
|
*
|
||
|
* _.clamp(10, -5, 5);
|
||
|
* // => 5
|
||
|
*/
|
||
|
function clamp(number, lower, upper) {
|
||
|
if (upper === undefined) {
|
||
|
upper = lower;
|
||
|
lower = undefined;
|
||
|
}
|
||
|
if (upper !== undefined) {
|
||
|
upper = toNumber(upper);
|
||
|
upper = upper === upper ? upper : 0;
|
||
|
}
|
||
|
if (lower !== undefined) {
|
||
|
lower = toNumber(lower);
|
||
|
lower = lower === lower ? lower : 0;
|
||
|
}
|
||
|
return baseClamp(toNumber(number), lower, upper);
|
||
|
}
|
||
|
|
||
|
export default clamp;
|