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 dc85ee6 commit 0face02Copy full SHA for 0face02
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 fromString(radix, x) {
@@ -47,6 +49,25 @@ function range(start, end) {
47
49
return rangeWithOptions(start, end, {});
48
50
}
51
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
67
68
69
+}
70
+
71
var Constants = {
72
minValue: -2147483648,
73
maxValue: 2147483647
@@ -57,5 +78,6 @@ export {
78
fromString ,
79
range ,
80
rangeWithOptions ,
81
+ clamp ,
82
83
/* No side effect */
src/Core__Int.res
@@ -61,3 +61,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
74
src/Core__Int.resi
@@ -322,3 +322,19 @@ Int.rangeWithOptions(3, 6, {step: -2}) // RangeError
322
- Raises `RangeError` if `step == 0 && start != end`.
323
*/
324
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
@@ -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