From 90c0817b6db256faf4e562a9c9f4a7ce6a371c1c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 7 Dec 2020 22:39:23 +0600 Subject: [PATCH] add clamp --- CHANGELOG.md | 3 +++ .../kotlin/dev/inmo/micro_utils/common/Clamp.kt | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ec8418a370..d00f69cbe2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.4.11 +* `Common` + * Add `clamp` function + ## 0.4.10 * `Versions`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt new file mode 100644 index 00000000000..98eab69372c --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/Clamp.kt @@ -0,0 +1,10 @@ +package dev.inmo.micro_utils.common + +@Suppress("NOTHING_TO_INLINE") +inline fun > T.clamp(min: T, max: T): T { + return when { + this < min -> min + this > max -> max + else -> this + } +}