Skip to content

Commit 0face02

Browse files
committed
feat(int) add clamp
1 parent dc85ee6 commit 0face02

5 files changed

Lines changed: 149 additions & 0 deletions

File tree

src/Core__Int.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
34
import * as Pervasives from "rescript/lib/es6/pervasives.js";
5+
import * as Caml_option from "rescript/lib/es6/caml_option.js";
46
import * as Core__Array from "./Core__Array.mjs";
57

68
function fromString(radix, x) {
@@ -47,6 +49,25 @@ function range(start, end) {
4749
return rangeWithOptions(start, end, {});
4850
}
4951

52+
function clamp(min, max, value) {
53+
var value$1;
54+
if (max !== undefined) {
55+
var max$1 = Caml_option.valFromOption(max);
56+
value$1 = Caml_obj.lessthan(max$1, value) ? max$1 : value;
57+
} else {
58+
value$1 = value;
59+
}
60+
if (min === undefined) {
61+
return value$1;
62+
}
63+
var min$1 = Caml_option.valFromOption(min);
64+
if (Caml_obj.greaterthan(min$1, value$1)) {
65+
return min$1;
66+
} else {
67+
return value$1;
68+
}
69+
}
70+
5071
var Constants = {
5172
minValue: -2147483648,
5273
maxValue: 2147483647
@@ -57,5 +78,6 @@ export {
5778
fromString ,
5879
range ,
5980
rangeWithOptions ,
81+
clamp ,
6082
}
6183
/* No side effect */

src/Core__Int.res

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,14 @@ let rangeWithOptions = (start, end, options) => {
6161
}
6262

6363
let range = (start, end) => rangeWithOptions(start, end, {})
64+
65+
let clamp = (~min=?, ~max=?, value) => {
66+
let value = switch max {
67+
| Some(max) if max < value => max
68+
| _ => value
69+
}
70+
switch min {
71+
| Some(min) if min > value => min
72+
| _ => value
73+
}
74+
}

src/Core__Int.resi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,19 @@ Int.rangeWithOptions(3, 6, {step: -2}) // RangeError
322322
- Raises `RangeError` if `step == 0 && start != end`.
323323
*/
324324
let rangeWithOptions: (int, int, rangeOptions) => array<int>
325+
326+
/**
327+
`clamp(~min=?, ~max=?, value)` returns `value`, optionally bounded by `min` and `max`.
328+
329+
if `max` < `min` returns `min`.
330+
331+
## Examples
332+
333+
```rescript
334+
Int.clamp(42) == 42
335+
Int.clamp(42, ~min=50) == 50
336+
Int.clamp(42, ~max=40) == 40
337+
Int.clamp(42, ~min=50, ~max=40) == 50
338+
```
339+
*/
340+
let clamp: (~min: int=?, ~max: int=?, int) => int

test/IntTests.mjs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,96 @@ Test.run([
459459
-5
460460
]);
461461

462+
Test.run([
463+
[
464+
"IntTests.res",
465+
165,
466+
20,
467+
27
468+
],
469+
"clamp"
470+
], Core__Int.clamp(undefined, undefined, 42), eq, 42);
471+
472+
Test.run([
473+
[
474+
"IntTests.res",
475+
166,
476+
20,
477+
35
478+
],
479+
"clamp - < min"
480+
], Core__Int.clamp(50, undefined, 42), eq, 50);
481+
482+
Test.run([
483+
[
484+
"IntTests.res",
485+
167,
486+
20,
487+
35
488+
],
489+
"clamp - > min"
490+
], Core__Int.clamp(40, undefined, 42), eq, 42);
491+
492+
Test.run([
493+
[
494+
"IntTests.res",
495+
168,
496+
20,
497+
35
498+
],
499+
"clamp - < max"
500+
], Core__Int.clamp(undefined, 50, 42), eq, 42);
501+
502+
Test.run([
503+
[
504+
"IntTests.res",
505+
169,
506+
20,
507+
35
508+
],
509+
"clamp - > max"
510+
], Core__Int.clamp(undefined, 40, 42), eq, 40);
511+
512+
Test.run([
513+
[
514+
"IntTests.res",
515+
170,
516+
20,
517+
42
518+
],
519+
"clamp - < min, < max"
520+
], Core__Int.clamp(50, 60, 42), eq, 50);
521+
522+
Test.run([
523+
[
524+
"IntTests.res",
525+
171,
526+
20,
527+
42
528+
],
529+
"clamp - < min, > max"
530+
], Core__Int.clamp(50, 40, 42), eq, 50);
531+
532+
Test.run([
533+
[
534+
"IntTests.res",
535+
172,
536+
20,
537+
42
538+
],
539+
"clamp - > min, < max"
540+
], Core__Int.clamp(40, 60, 42), eq, 42);
541+
542+
Test.run([
543+
[
544+
"IntTests.res",
545+
173,
546+
20,
547+
42
548+
],
549+
"clamp - > min, > max"
550+
], Core__Int.clamp(40, 40, 42), eq, 40);
551+
462552
export {
463553
eq ,
464554
$$catch ,

test/IntTests.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,13 @@ Test.run(
161161
eq,
162162
[-3, -5],
163163
)
164+
165+
Test.run(__POS_OF__("clamp"), Int.clamp(42), eq, 42)
166+
Test.run(__POS_OF__("clamp - < min"), Int.clamp(~min=50, 42), eq, 50)
167+
Test.run(__POS_OF__("clamp - > min"), Int.clamp(~min=40, 42), eq, 42)
168+
Test.run(__POS_OF__("clamp - < max"), Int.clamp(~max=50, 42), eq, 42)
169+
Test.run(__POS_OF__("clamp - > max"), Int.clamp(~max=40, 42), eq, 40)
170+
Test.run(__POS_OF__("clamp - < min, < max"), Int.clamp(~min=50, ~max=60, 42), eq, 50)
171+
Test.run(__POS_OF__("clamp - < min, > max"), Int.clamp(~min=50, ~max=40, 42), eq, 50) // min wins
172+
Test.run(__POS_OF__("clamp - > min, < max"), Int.clamp(~min=40, ~max=60, 42), eq, 42)
173+
Test.run(__POS_OF__("clamp - > min, > max"), Int.clamp(~min=40, ~max=40, 42), eq, 40)

0 commit comments

Comments
 (0)