Skip to content

Instantly share code, notes, and snippets.

@joelnet
Created September 19, 2019 18:14

Revisions

  1. joelnet created this gist Sep 19, 2019.
    32 changes: 32 additions & 0 deletions clamp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const pipe = require('ramda/src/pipe')
    const curry = require('ramda/src/curry')

    const clampA = (min, max, value) => {
    let newValue = Number(value)
    newValue = Math.min(max, newValue)
    newValue = Math.max(min, newValue)
    return newValue
    }

    const clampB = (min, max, value) =>
    Math.max(min, Math.min(max, Number(value)))

    const clampC = (min, max, value) => pipe(
    Number,
    num => Math.min(max, num)
    num => Math.max(min, num)
    )

    const mathMin = curry(Math.min)
    const mathMax = curry(Math.max)

    const clampD = min => max => pipe(
    Number,
    mathMin(max)
    mathMax(min)
    )

    const clampE = (min, max, num) => num
    |> Number
    |> Math.min(max, ?)
    |> Math.max(min, ?)