diff --git a/implementation.js b/implementation.js index 110ccd3..f83bc77 100644 --- a/implementation.js +++ b/implementation.js @@ -2,10 +2,18 @@ import esAbstract from 'es-abstract'; const {ToNumber: toNumber} = esAbstract; -export default function clamp(min, val, max) { - const minCoerced = toNumber(min); - const valCoerced = toNumber(val); - const maxCoerced = toNumber(max); +export default function clamp(number, min, max) { + number = toNumber(number); - return Math.max(minCoerced, Math.min(valCoerced, maxCoerced)); + if (max !== undefined && max !== null) { + max = toNumber(max); + number = Math.min(number, max); + } + + if (min !== undefined && min !== null) { + min = toNumber(min); + number = Math.max(number, min); + } + + return number; } diff --git a/index.html b/index.html index 674834d..39b5ee7 100644 --- a/index.html +++ b/index.html @@ -2407,7 +2407,7 @@
u
/
The clamp
function returns a number that is the result of constraining number between the lower bound defined by min and the upper bound defined by max.
This function returns the
© 2023 Richie Bendall
+© 2024 Richie Bendall
All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.
@@ -2437,4 +2437,5 @@THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-