Skip to content

Commit

Permalink
Bounded: add clamp, reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 21, 2022
1 parent bacab92 commit ed90b56
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
29 changes: 29 additions & 0 deletions docs/modules/Bounded.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Added in v2.0.0
- [~~boundedNumber~~](#boundednumber)
- [type classes](#type-classes)
- [Bounded (interface)](#bounded-interface)
- [utils](#utils)
- [clamp](#clamp)
- [reverse](#reverse)

---

Expand Down Expand Up @@ -53,3 +56,29 @@ export interface Bounded<A> extends Ord<A> {
```

Added in v2.0.0

# utils

## clamp

Clamp a value between bottom and top values.

**Signature**

```ts
export declare const clamp: <T>(B: Bounded<T>) => (a: T) => T
```
Added in v2.12.0
## reverse
Reverses the Ord of a bound and swaps top and bottom values.
**Signature**
```ts
export declare const reverse: <T>(B: Bounded<T>) => Bounded<T>
```
Added in v2.12.0
36 changes: 33 additions & 3 deletions src/Bounded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*
* @since 2.0.0
*/
import { Ord, ordNumber } from './Ord'
import * as O from './Ord'

import Ord = O.Ord

// -------------------------------------------------------------------------------------
// model
Expand All @@ -22,6 +24,34 @@ export interface Bounded<A> extends Ord<A> {
readonly bottom: A
}

// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------

/**
* Clamp a value between bottom and top values.
*
* @category utils
* @since 2.12.0
*/
export const clamp = <T>(B: Bounded<T>) => O.clamp(B)(B.bottom, B.top)

/**
* Reverses the Ord of a bound and swaps top and bottom values.
*
* @category utils
* @since 2.12.0
*/
export const reverse = <T>(B: Bounded<T>): Bounded<T> => {
const R = O.reverse(B)
return {
equals: R.equals,
compare: R.compare,
top: B.bottom,
bottom: B.top
}
}

// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
Expand All @@ -36,8 +66,8 @@ export interface Bounded<A> extends Ord<A> {
* @deprecated
*/
export const boundedNumber: Bounded<number> = {
equals: ordNumber.equals,
compare: ordNumber.compare,
equals: O.ordNumber.equals,
compare: O.ordNumber.compare,
top: Infinity,
bottom: -Infinity
}
27 changes: 27 additions & 0 deletions test/Bounded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as _ from '../src/Bounded'
import * as N from '../src/number'
import * as U from './util'

describe('Bounded', () => {
it('clamp', () => {
const B: _.Bounded<number> = {
...N.Ord,
top: 10,
bottom: 0
}
const clamp = _.clamp(B)
U.deepStrictEqual(clamp(5), 5)
U.deepStrictEqual(clamp(-1), 0)
U.deepStrictEqual(clamp(11), 10)
})

it('reverse', () => {
const B: _.Bounded<number> = _.reverse({
...N.Ord,
top: 10,
bottom: 0
})
U.deepStrictEqual(B.top, 0)
U.deepStrictEqual(B.bottom, 10)
})
})

0 comments on commit ed90b56

Please sign in to comment.