math-scale.js 602 B

12345678910111213
  1. // `Math.scale` method implementation
  2. // https://rwaldron.github.io/proposal-math-extensions/
  3. module.exports = Math.scale || function scale(x, inLow, inHigh, outLow, outHigh) {
  4. var nx = +x;
  5. var nInLow = +inLow;
  6. var nInHigh = +inHigh;
  7. var nOutLow = +outLow;
  8. var nOutHigh = +outHigh;
  9. // eslint-disable-next-line no-self-compare -- NaN check
  10. if (nx != nx || nInLow != nInLow || nInHigh != nInHigh || nOutLow != nOutLow || nOutHigh != nOutHigh) return NaN;
  11. if (nx === Infinity || nx === -Infinity) return nx;
  12. return (nx - nInLow) * (nOutHigh - nOutLow) / (nInHigh - nInLow) + nOutLow;
  13. };