@augment-vir/common 31.20.0 → 31.21.0
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.
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { type MinMax } from '@augment-vir/core';
|
|
1
|
+
import { type MinMax, type PartialWithUndefined } from '@augment-vir/core';
|
|
2
2
|
/**
|
|
3
3
|
* If the given value is outside the given min/max bounds, instead of clamping the number (as the
|
|
4
4
|
* `clamp` function does), this function wraps the value around to the next bound (inclusive).
|
|
5
5
|
*
|
|
6
|
+
* If `takeOverflow` is set to `true`, then any excess after wrapping is continually iterated over
|
|
7
|
+
* and wrapped again, potentially resulting in wrapping through the min/max bounds multiple times.
|
|
8
|
+
*
|
|
6
9
|
* @category Number
|
|
7
10
|
* @category Package : @augment-vir/common
|
|
8
11
|
* @example
|
|
@@ -10,10 +13,19 @@ import { type MinMax } from '@augment-vir/core';
|
|
|
10
13
|
* ```ts
|
|
11
14
|
* import {wrapNumber} from '@augment-vir/common';
|
|
12
15
|
*
|
|
13
|
-
* wrapNumber({min: 0, max: 100
|
|
14
|
-
* wrapNumber({min: 0, max: 100
|
|
16
|
+
* wrapNumber(101, {min: 0, max: 100}); // 0
|
|
17
|
+
* wrapNumber(-1, {min: 0, max: 100}); // 100
|
|
18
|
+
* wrapNumber(-7, {min: 0, max: 3, takeOverflow: true}); // 1
|
|
19
|
+
* wrapNumber(13, {min: 0, max: 3, takeOverflow: true}); // 1
|
|
15
20
|
* ```
|
|
16
21
|
*
|
|
17
22
|
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
23
|
*/
|
|
19
|
-
export declare function wrapNumber(value: number, minMax: Readonly<MinMax>
|
|
24
|
+
export declare function wrapNumber(value: number, minMax: Readonly<MinMax> & PartialWithUndefined<{
|
|
25
|
+
/**
|
|
26
|
+
* If `true`, the overflow of the wrap is added to the bound wrapped to.
|
|
27
|
+
*
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
takeOverflow: boolean;
|
|
31
|
+
}>): number;
|
|
@@ -3,6 +3,9 @@ import { ensureMinMax } from '@augment-vir/core';
|
|
|
3
3
|
* If the given value is outside the given min/max bounds, instead of clamping the number (as the
|
|
4
4
|
* `clamp` function does), this function wraps the value around to the next bound (inclusive).
|
|
5
5
|
*
|
|
6
|
+
* If `takeOverflow` is set to `true`, then any excess after wrapping is continually iterated over
|
|
7
|
+
* and wrapped again, potentially resulting in wrapping through the min/max bounds multiple times.
|
|
8
|
+
*
|
|
6
9
|
* @category Number
|
|
7
10
|
* @category Package : @augment-vir/common
|
|
8
11
|
* @example
|
|
@@ -10,19 +13,33 @@ import { ensureMinMax } from '@augment-vir/core';
|
|
|
10
13
|
* ```ts
|
|
11
14
|
* import {wrapNumber} from '@augment-vir/common';
|
|
12
15
|
*
|
|
13
|
-
* wrapNumber({min: 0, max: 100
|
|
14
|
-
* wrapNumber({min: 0, max: 100
|
|
16
|
+
* wrapNumber(101, {min: 0, max: 100}); // 0
|
|
17
|
+
* wrapNumber(-1, {min: 0, max: 100}); // 100
|
|
18
|
+
* wrapNumber(-7, {min: 0, max: 3, takeOverflow: true}); // 1
|
|
19
|
+
* wrapNumber(13, {min: 0, max: 3, takeOverflow: true}); // 1
|
|
15
20
|
* ```
|
|
16
21
|
*
|
|
17
22
|
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
|
|
18
23
|
*/
|
|
19
24
|
export function wrapNumber(value, minMax) {
|
|
20
25
|
const { min, max } = ensureMinMax(minMax);
|
|
21
|
-
if (
|
|
22
|
-
|
|
26
|
+
if (minMax.takeOverflow) {
|
|
27
|
+
const diff = max - min + 1;
|
|
28
|
+
const offset = (value - min) % diff;
|
|
29
|
+
if (offset < 0) {
|
|
30
|
+
return min + diff + offset;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return min + offset;
|
|
34
|
+
}
|
|
23
35
|
}
|
|
24
|
-
else
|
|
25
|
-
|
|
36
|
+
else {
|
|
37
|
+
if (value > max) {
|
|
38
|
+
return min;
|
|
39
|
+
}
|
|
40
|
+
else if (value < min) {
|
|
41
|
+
return max;
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
26
44
|
}
|
|
27
|
-
return value;
|
|
28
45
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@augment-vir/common",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.21.0",
|
|
4
4
|
"description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"augment",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"test:web": "virmator --no-deps test web"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@augment-vir/assert": "^31.
|
|
44
|
-
"@augment-vir/core": "^31.
|
|
43
|
+
"@augment-vir/assert": "^31.21.0",
|
|
44
|
+
"@augment-vir/core": "^31.21.0",
|
|
45
45
|
"@date-vir/duration": "^7.3.1",
|
|
46
46
|
"ansi-styles": "^6.2.1",
|
|
47
47
|
"json5": "^2.2.3",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@web/dev-server-esbuild": "^1.0.4",
|
|
53
|
-
"@web/test-runner": "^0.20.
|
|
53
|
+
"@web/test-runner": "^0.20.2",
|
|
54
54
|
"@web/test-runner-commands": "^0.9.0",
|
|
55
55
|
"@web/test-runner-playwright": "^0.11.0",
|
|
56
56
|
"@web/test-runner-visual-regression": "^0.10.0",
|