main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
888 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import baseClamp from './_baseClamp.js';
|
---|
| 2 | import toNumber from './toNumber.js';
|
---|
| 3 |
|
---|
| 4 | /**
|
---|
| 5 | * Clamps `number` within the inclusive `lower` and `upper` bounds.
|
---|
| 6 | *
|
---|
| 7 | * @static
|
---|
| 8 | * @memberOf _
|
---|
| 9 | * @since 4.0.0
|
---|
| 10 | * @category Number
|
---|
| 11 | * @param {number} number The number to clamp.
|
---|
| 12 | * @param {number} [lower] The lower bound.
|
---|
| 13 | * @param {number} upper The upper bound.
|
---|
| 14 | * @returns {number} Returns the clamped number.
|
---|
| 15 | * @example
|
---|
| 16 | *
|
---|
| 17 | * _.clamp(-10, -5, 5);
|
---|
| 18 | * // => -5
|
---|
| 19 | *
|
---|
| 20 | * _.clamp(10, -5, 5);
|
---|
| 21 | * // => 5
|
---|
| 22 | */
|
---|
| 23 | function clamp(number, lower, upper) {
|
---|
| 24 | if (upper === undefined) {
|
---|
| 25 | upper = lower;
|
---|
| 26 | lower = undefined;
|
---|
| 27 | }
|
---|
| 28 | if (upper !== undefined) {
|
---|
| 29 | upper = toNumber(upper);
|
---|
| 30 | upper = upper === upper ? upper : 0;
|
---|
| 31 | }
|
---|
| 32 | if (lower !== undefined) {
|
---|
| 33 | lower = toNumber(lower);
|
---|
| 34 | lower = lower === lower ? lower : 0;
|
---|
| 35 | }
|
---|
| 36 | return baseClamp(toNumber(number), lower, upper);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | export default clamp;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.