Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

Commit

Permalink
feat(clamp): add function
Browse files Browse the repository at this point in the history
  • Loading branch information
David Zukowski committed Jan 23, 2017
1 parent 278e209 commit 32b18ee
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"no-whitespace-before-property": 2,
"no-with": 2,
"one-var": 2,
"operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }],
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
"padded-blocks": [2, "never"],
"quotes": [2, "single", "avoid-escape"],
"semi": [2, "never"],
Expand Down
1 change: 1 addition & 0 deletions src/bundles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { default as any } from '../any'
export { default as append } from '../append'
export { default as assoc } from '../assoc'
export { default as chain } from '../chain'
export { default as clamp } from '../clamp'
export { default as concat } from '../concat'
export { default as cond } from '../cond'
export { default as contains } from '../contains'
Expand Down
18 changes: 18 additions & 0 deletions src/clamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _curry3 from './internal/_curry3'

/**
* @name clamp
* @signature Number -> Number -> Number -> Number
* @since v0.18.0
* @description
* Restricts a value to a given range (inclusive).
* @example
* clamp(1, 10, 5) // => 5
* clamp(1, 10, -5) // => 1
* clamp(1, 10, 15) // => 15
*/
export default _curry3(function clamp (lower, upper, value) {
return value < lower ? lower :
value > upper ? upper :
value
})
23 changes: 23 additions & 0 deletions tests/clamp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const test = require('ava')
, { clamp } = require('../dist/redash')

test('properly reports its arity (is ternary)', (t) => {
t.is(clamp.length, 3)
})

test('is curried', (t) => {
t.is(typeof clamp(1), 'function')
t.is(typeof clamp(1, 10), 'function')
})

test('restricts values to the lower bound', (t) => {
t.is(clamp(1, 10, -5), 1)
})

test('restricts values to the upper bound', (t) => {
t.is(clamp(1, 10, 15), 10)
})

test('allows values between the lower and upper bound', (t) => {
t.is(clamp(1, 10, 5), 5)
})

0 comments on commit 32b18ee

Please sign in to comment.