@ptolemy2002/js-math-utils 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +29 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ Clamps a number between a minimum and maximum value.
|
|
|
35
35
|
|
|
36
36
|
### wrapNumber
|
|
37
37
|
#### Description
|
|
38
|
-
Wraps a number between a minimum and maximum value,
|
|
38
|
+
Wraps a number between a minimum and maximum value, inclusive on the maximum side (so `min - 1` gets converted to `max` and `max + 1` gets converted to `min`).
|
|
39
39
|
|
|
40
40
|
#### Parameters
|
|
41
41
|
- `n` (`number`): The number to be wrapped.
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ export type ClampOptions = {
|
|
|
3
3
|
max?: number;
|
|
4
4
|
};
|
|
5
5
|
export declare function clamp(value: number, { min, max }?: ClampOptions): number;
|
|
6
|
-
export declare function wrapNumber(
|
|
6
|
+
export declare function wrapNumber(value: number, min: number, max: number): number;
|
package/dist/index.js
CHANGED
|
@@ -9,14 +9,33 @@ function clamp(value, { min = null, max = null } = {}) {
|
|
|
9
9
|
return max;
|
|
10
10
|
return value;
|
|
11
11
|
}
|
|
12
|
-
function wrapNumber(
|
|
13
|
-
|
|
14
|
-
if (
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
function wrapNumber(value, min, max) {
|
|
13
|
+
// Handle edge case: if min equals max, return that value
|
|
14
|
+
if (min === max) {
|
|
15
|
+
return min;
|
|
16
|
+
}
|
|
17
|
+
// Calculate the range size
|
|
18
|
+
const rangeSize = max - min;
|
|
19
|
+
// If value is already within range (inclusive), return as-is
|
|
20
|
+
if (value >= min && value <= max) {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
// Normalize the value by shifting it relative to min
|
|
24
|
+
const normalizedValue = value - min;
|
|
25
|
+
// Calculate the wrapped position within the range
|
|
26
|
+
// Use modulo to find where the value falls in the repeating pattern
|
|
27
|
+
// The +1 is to ensure that the range is inclusive of both min and max
|
|
28
|
+
let wrappedValue = normalizedValue % rangeSize;
|
|
29
|
+
// Handle negative values by ensuring the result is positive
|
|
30
|
+
if (wrappedValue < 0) {
|
|
31
|
+
wrappedValue += rangeSize;
|
|
32
|
+
}
|
|
33
|
+
// If wrappedValue is 0 and the value approaches from the left,
|
|
34
|
+
// we need to return the max value so that we can remain
|
|
35
|
+
// inclusive of the max boundary
|
|
36
|
+
if (wrappedValue === 0 && value > max) {
|
|
37
|
+
return max;
|
|
38
|
+
}
|
|
39
|
+
// Shift back to the original range and return
|
|
40
|
+
return wrappedValue + min;
|
|
22
41
|
}
|