Skip to content

Commit 8ab6bd1

Browse files
Merge pull request #89 from studiometa/feature/math-clamp-utils
Add clamp math functions
2 parents 30b5640 + d101e07 commit 8ab6bd1

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

src/utils/math/clamp.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Clamp a value in a given range.
3+
* @param {number} value
4+
* @param {number} min
5+
* @param {number} max
6+
* @return {number}
7+
*/
8+
export default function clamp(value, min, max) {
9+
/* eslint-disable no-nested-ternary */
10+
return min < max
11+
? value < min
12+
? min
13+
: value > max
14+
? max
15+
: value
16+
: value < max
17+
? max
18+
: value > min
19+
? min
20+
: value;
21+
/* eslint-enable no-nested-ternary */
22+
}

src/utils/math/clamp01.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import clamp from './clamp';
2+
3+
/**
4+
* Clamp a value in the 0–1 range.
5+
* @param {number} value
6+
* @return {number}
7+
*/
8+
export default function clamp01(value) {
9+
return clamp(value, 0, 1);
10+
}

src/utils/math/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import clamp from './clamp';
2+
import clamp01 from './clamp01';
13
import damp from './damp';
24
import lerp from './lerp';
35
import map from './map';
46
import round from './round';
57

6-
export { damp, lerp, map, round };
8+
export { clamp, clamp01, damp, lerp, map, round };

tests/utils/math/clamp.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import clamp from '~/utils/math/clamp';
2+
3+
describe('clamp method', () => {
4+
it('should clamp a value between the given range', () => {
5+
expect(clamp(0, 0, 10)).toBe(0);
6+
expect(clamp(-5, 0, 10)).toBe(0);
7+
expect(clamp(15, 0, 10)).toBe(10);
8+
expect(clamp(5, 0, 10)).toBe(5);
9+
expect(clamp(5, 10, 0)).toBe(5);
10+
expect(clamp(-5, 10, 0)).toBe(0);
11+
expect(clamp(15, 10, 0)).toBe(10);
12+
});
13+
});

tests/utils/math/clamp01.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import clamp01 from '~/utils/math/clamp01';
2+
3+
describe('clamp01 method', () => {
4+
it('should clamp a value between 0 and 1', () => {
5+
expect(clamp01(0)).toBe(0);
6+
expect(clamp01(0.5)).toBe(0.5);
7+
expect(clamp01(1)).toBe(1);
8+
expect(clamp01(-1)).toBe(0);
9+
expect(clamp01(2)).toBe(1);
10+
});
11+
});

tests/utils/math/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import * as math from '~/utils/math';
22

33
describe('~/utils/math exports', () => {
44
it('should export all scripts', () => {
5-
expect(Object.keys(math)).toEqual(['damp', 'lerp', 'map', 'round']);
5+
expect(Object.keys(math)).toEqual(['clamp', 'clamp01', 'damp', 'lerp', 'map', 'round']);
66
});
77
});

0 commit comments

Comments
 (0)