@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
29 lines (28 loc) • 811 B
JavaScript
import { ensureMinMax } from '@augment-vir/core';
/**
* If the given value is outside the given min/max bounds, instead of clamping the number (as the
* `clamp` function does), this function wraps the value around to the next bound (inclusive).
*
* @category Number
* @category Package : @augment-vir/common
* @example
*
* ```ts
* import {wrapNumber} from '@augment-vir/common';
*
* wrapNumber({min: 0, max: 100, value: 101}); // 0
* wrapNumber({min: 0, max: 100, value: -1}); // 100
* ```
*
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export function wrapNumber(value, minMax) {
const { min, max } = ensureMinMax(minMax);
if (value > max) {
return min;
}
else if (value < min) {
return max;
}
return value;
}