-
Notifications
You must be signed in to change notification settings - Fork 7
/
clamp.ts
58 lines (48 loc) · 1.46 KB
/
clamp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { PH } from './utils/types.ts';
// @types
type Clamp_1<T> = (min: T) => T;
type Clamp_2<T> = (max: T) => T;
type Clamp_3<T> = (value: T) => T;
type Clamp_2_3<T> =
& ((max: T) => Clamp_3<T>)
& ((max: PH, value: T) => Clamp_2<T>)
& ((max: T, value: T) => T);
type Clamp_1_3<T> =
& ((min: T) => Clamp_3<T>)
& ((min: PH, value: T) => Clamp_1<T>)
& ((min: T, value: T) => T);
type Clamp_1_2<T> =
& ((min: T) => Clamp_2<T>)
& ((min: PH, max: T) => Clamp_1<T>)
& ((min: T, max: T) => T);
type Clamp =
& (<T>(min: T) => Clamp_2_3<T>)
& (<T>(min: PH, max: T) => Clamp_1_3<T>)
& (<T>(min: PH, max: PH, value: T) => Clamp_1_2<T>)
& (<T>(min: T, max: T) => Clamp_3<T>)
& (<T>(min: T, max: PH, value: T) => Clamp_2<T>)
& (<T>(min: PH, max: T, value: T) => Clamp_1<T>)
& (<T>(min: T, max: T, value: T) => T);
function _clamp<T extends number | string>(
min: T,
max: T,
value: T,
): T {
if (min > max) {
throw new Error(
'Minimum value must be smaller than Maximum value',
);
}
return value < min ? min : value > max ? max : value;
}
/**
* Restricts `value` between `min` and `max`.
* Returns `min` if `value < min`, `max` if `value > max`, `value` otherwise
*
* Fae.clamp(1, 10, -5) // => 1
* Fae.clamp(1, 10, 15) // => 10
* Fae.clamp(1, 10, 4) // => 4
*/
export const clamp = curryN(3, _clamp) as Clamp;