Skip to content

Commit 91233a1

Browse files
committed
feat(int) add clamp
1 parent 5e80610 commit 91233a1

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 equal(a, b) {
@@ -61,6 +63,25 @@ function range(start, end) {
6163
return rangeWithOptions(start, end, {});
6264
}
6365

66+
function clamp(min, max, value) {
67+
var value$1;
68+
if (max !== undefined) {
69+
var max$1 = Caml_option.valFromOption(max);
70+
value$1 = Caml_obj.lessthan(max$1, value) ? max$1 : value;
71+
} else {
72+
value$1 = value;
73+
}
74+
if (min === undefined) {
75+
return value$1;
76+
}
77+
var min$1 = Caml_option.valFromOption(min);
78+
if (Caml_obj.greaterthan(min$1, value$1)) {
79+
return min$1;
80+
} else {
81+
return value$1;
82+
}
83+
}
84+
6485
var Constants = {
6586
minValue: -2147483648,
6687
maxValue: 2147483647
@@ -73,5 +94,6 @@ export {
7394
fromString ,
7495
range ,
7596
rangeWithOptions ,
97+
clamp ,
7698
}
7799
/* No side effect */

src/Core__Int.res

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

6868
let range = (start, end) => rangeWithOptions(start, end, {})
69+
70+
let clamp = (~min=?, ~max=?, value) => {
71+
let value = switch max {
72+
| Some(max) if max < value => max
73+
| _ => value
74+
}
75+
switch min {
76+
| Some(min) if min > value => min
77+
| _ => value
78+
}
79+
}

src/Core__Int.resi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,19 @@ Int.rangeWithOptions(3, 6, {step: -2}) // RangeError
326326
- Raises `RangeError` if `step == 0 && start != end`.
327327
*/
328328
let rangeWithOptions: (int, int, rangeOptions) => array<int>
329+
330+
/**
331+
`clamp(~min=?, ~max=?, value)` returns `value`, optionally bounded by `min` and `max`.
332+
333+
if `max` < `min` returns `min`.
334+
335+
## Examples
336+
337+
```rescript
338+
Int.clamp(42) == 42
339+
Int.clamp(42, ~min=50) == 50
340+
Int.clamp(42, ~max=40) == 40
341+
Int.clamp(42, ~min=50, ~max=40) == 50
342+
```
343+
*/
344+
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)