Skip to content

Commit 65ba312

Browse files
committed
refactor(packages/stdlib): add comments for math utils
1 parent e9b8b0c commit 65ba312

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

packages/stdlib/src/math/clamp/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@
77
* @returns {number} The clamped number
88
*/
99
export function clamp(value: number, min: number, max: number): number {
10+
// The clamp function takes a value, a minimum, and a maximum as parameters.
11+
// It ensures that the value falls within the range defined by the minimum and maximum values.
12+
// If the value is less than the minimum, it returns the minimum value.
13+
// If the value is greater than the maximum, it returns the maximum value.
14+
// Otherwise, it returns the original value.
1015
return Math.min(Math.max(value, min), max);
1116
}

packages/stdlib/src/math/mapRange/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ import { clamp } from "../clamp";
1111
* @returns {number} The mapped value
1212
*/
1313
export function mapRange(value: number, in_min: number, in_max: number, out_min: number, out_max: number): number {
14+
// Zero input range means invalid input, so return lowest output range value
1415
if (in_min === in_max)
1516
return out_min;
1617

17-
return (clamp(value, in_min, in_max) - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
18+
// To ensure the value is within the input range, clamp it
19+
const clampedValue = clamp(value, in_min, in_max);
20+
21+
// Finally, map the value from the input range to the output range
22+
return (clampedValue - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
1823
}

0 commit comments

Comments
 (0)