Files
Paradise/tgui/packages/common/math.js
AffectedArc07 58aa86cb9f TGUI-V3 (#13310)
* I need that gitignore file

* Temp Commit - DOES NOT COMPILE

* THE SHIT WORKS

* Readme change

* Disposal Unit --> TGUI

* mmmm yes CI which may not actually work

* New GitIgnore

* ITS TGUI-NEXT BABY

* Doc update

* CI tweak

* Chmod

* And again

* *sigh*

* Lets appreciate the irony of me failing CI stages

* 0/1 --> True/False

* Fixes some update nonsense

* CI Update

* Lets try this

* What about this maybe

* NVM is hurting me

* I swear to god

* A little bit of validation in my life

* V3 BABYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY

* Fixes

* Fixes NaN appearing for a few frames when UIs open

* Fixes + Crew Monitor

* Corn + Steel + Mochi Fixes

* Forgot this

* Fixes from stylemistake

* Code Change

* Adds logout proc

* Offline implications + Resizeable crew monitor

* Change locate() to locateUID()

* Change div --> box
2020-06-30 03:51:36 -04:00

85 lines
2.0 KiB
JavaScript

/**
* Limits a number to the range between 'min' and 'max'.
*/
export const clamp = (value, min, max) => {
return value < min ? min : value > max ? max : value;
};
/**
* Limits a number between 0 and 1.
*/
export const clamp01 = value => {
return value < 0 ? 0 : value > 1 ? 1 : value;
};
/**
* Scales a number to fit into the range between min and max.
*/
export const scale = (value, min, max) => {
return (value - min) / (max - min);
};
/**
* Robust number rounding.
*
* Adapted from Locutus, see: http://locutus.io/php/math/round/
*
* @param {number} value
* @param {number} precision
* @return {number}
*/
export const round = (value, precision) => {
if (!value || isNaN(value)) {
return value;
}
// helper variables
let m, f, isHalf, sgn;
// making sure precision is integer
precision |= 0;
m = Math.pow(10, precision);
value *= m;
// sign of the number
sgn = (value > 0) | -(value < 0);
// isHalf = value % 1 === 0.5 * sgn;
isHalf = Math.abs(value % 1) >= 0.4999999999854481;
f = Math.floor(value);
if (isHalf) {
// rounds .5 away from zero
value = f + (sgn > 0);
}
return (isHalf ? value : Math.round(value)) / m;
};
/**
* Returns a string representing a number in fixed point notation.
*/
export const toFixed = (value, fractionDigits = 0) => {
return Number(value).toFixed(Math.max(fractionDigits, 0));
};
/**
* Checks whether a value is within the provided range.
*
* Range is an array of two numbers, for example: [0, 15].
*/
export const inRange = (value, range) => {
return range
&& value >= range[0]
&& value <= range[1];
};
/**
* Walks over the object with ranges, comparing value against every range,
* and returns the key of the first matching range.
*
* Range is an array of two numbers, for example: [0, 15].
*/
export const keyOfMatchingRange = (value, ranges) => {
for (let rangeName of Object.keys(ranges)) {
const range = ranges[rangeName];
if (inRange(value, range)) {
return rangeName;
}
}
};