Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
satin-ui / src / utils / util.math.js
Size: Mime:
import math from 'mathjs';

export function roundUpTo(_value, _increment = 1) {
	// return Math.ceil(value / increment) * increment;
	let value = math.bignumber(_value);
	let increment = math.bignumber(_increment);

	return math.number(
		math.multiply(
			math.ceil(
				math.divide(
					value,
					increment
				)
			),
			increment
		)
	);
}

export function roundDownTo(_value, _increment = 1) {
	// return Math.floor(value / increment) * increment;
	let value = math.bignumber(_value);
	let increment = math.bignumber(_increment);

	return math.number(
		math.multiply(
			math.floor(
				math.divide(
					value,
					increment
				)
			),
			increment
		)
	);
}

export function constrain(_value, min = -Infinity, max = Infinity) {
	let value = _value;

	if (max === min) {
		return max;
	} else if (max < min) {
		throw new Error('Max must be larger than min');
	}

	if (min !== -Infinity) {
		value = Math.max(value, min);
	}
	if (max !== Infinity) {
		value = Math.min(value, max);
	}

	return value;
}