Skip to content

Commit 76072fc

Browse files
committed
feat(iclamp): add iclamp function
1 parent 8f5ac60 commit 76072fc

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

.idea/dictionaries/djc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
i32,
44
iadd,
55
iand,
6+
iclamp,
67
icmp,
78
idiv,
89
ieq,
@@ -292,3 +293,11 @@ test("ior", t => {
292293
t.is(ior(0x100000001, 0), 1);
293294
t.is(ior(1, 0x100000012), 1);
294295
});
296+
297+
test("iclamp", t => {
298+
t.is(iclamp(5, 0, 10), 5);
299+
t.is(iclamp(5, 2, 10), 5);
300+
t.is(iclamp(2, 5, 10), 5);
301+
t.is(iclamp(5, -5, 10), 5);
302+
t.is(iclamp(5, -10, -5), -5);
303+
});

index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,7 @@ export function ior(a: number, b: number): number {
147147
// eslint-disable-next-line @typescript-eslint/no-explicit-any
148148
return ((a | 0 || b | 0) as any) | 0;
149149
}
150+
151+
export function iclamp(n: number, min: number, max: number): number {
152+
return i32(Math.max(Math.min(i32(n), i32(max)), i32(min)));
153+
}

0 commit comments

Comments
 (0)