We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5e80610 commit 91233a1Copy full SHA for 91233a1
5 files changed
src/Core__Int.mjs
@@ -1,6 +1,8 @@
1
// Generated by ReScript, PLEASE EDIT WITH CARE
2
3
+import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
4
import * as Pervasives from "rescript/lib/es6/pervasives.js";
5
+import * as Caml_option from "rescript/lib/es6/caml_option.js";
6
import * as Core__Array from "./Core__Array.mjs";
7
8
function equal(a, b) {
@@ -61,6 +63,25 @@ function range(start, end) {
61
63
return rangeWithOptions(start, end, {});
62
64
}
65
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
81
82
83
+}
84
+
85
var Constants = {
86
minValue: -2147483648,
87
maxValue: 2147483647
@@ -73,5 +94,6 @@ export {
94
fromString ,
95
range ,
96
rangeWithOptions ,
97
+ clamp ,
98
99
/* No side effect */
src/Core__Int.res
@@ -66,3 +66,14 @@ let rangeWithOptions = (start, end, options) => {
let range = (start, end) => rangeWithOptions(start, end, {})
+let clamp = (~min=?, ~max=?, value) => {
+ let value = switch max {
+ | Some(max) if max < value => max
+ | _ => value
+ switch min {
+ | Some(min) if min > value => min
src/Core__Int.resi
@@ -326,3 +326,19 @@ Int.rangeWithOptions(3, 6, {step: -2}) // RangeError
326
- Raises `RangeError` if `step == 0 && start != end`.
327
*/
328
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
@@ -459,6 +459,96 @@ Test.run([
459
-5
460
]);
461
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
473
474
475
+ 166,
476
477
+ 35
478
479
+ "clamp - < min"
480
+ ], Core__Int.clamp(50, undefined, 42), eq, 50);
481
482
483
484
485
+ 167,
486
487
488
489
+ "clamp - > min"
490
+ ], Core__Int.clamp(40, undefined, 42), eq, 42);
491
492
493
494
495
+ 168,
496
497
498
499
+ "clamp - < max"
500
+ ], Core__Int.clamp(undefined, 50, 42), eq, 42);
501
502
503
504
505
+ 169,
506
507
508
509
+ "clamp - > max"
510
+ ], Core__Int.clamp(undefined, 40, 42), eq, 40);
511
512
513
514
515
+ 170,
516
517
+ 42
518
519
+ "clamp - < min, < max"
520
+ ], Core__Int.clamp(50, 60, 42), eq, 50);
521
522
523
524
525
+ 171,
526
527
528
529
+ "clamp - < min, > max"
530
+ ], Core__Int.clamp(50, 40, 42), eq, 50);
531
532
533
534
535
+ 172,
536
537
538
539
+ "clamp - > min, < max"
540
+ ], Core__Int.clamp(40, 60, 42), eq, 42);
541
542
543
544
545
+ 173,
546
547
548
549
+ "clamp - > min, > max"
550
+ ], Core__Int.clamp(40, 40, 42), eq, 40);
551
552
export {
553
eq ,
554
$$catch ,
test/IntTests.res
@@ -161,3 +161,13 @@ Test.run(
161
eq,
162
[-3, -5],
163
)
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