ToUint8Clamp.js 522 B

12345678910111213141516171819
  1. 'use strict';
  2. var ToNumber = require('./ToNumber');
  3. var floor = require('./floor');
  4. var $isNaN = require('../helpers/isNaN');
  5. // https://ecma-international.org/ecma-262/6.0/#sec-touint8clamp
  6. module.exports = function ToUint8Clamp(argument) {
  7. var number = ToNumber(argument);
  8. if ($isNaN(number) || number <= 0) { return 0; }
  9. if (number >= 0xFF) { return 0xFF; }
  10. var f = floor(argument);
  11. if (f + 0.5 < number) { return f + 1; }
  12. if (number < f + 0.5) { return f; }
  13. if (f % 2 !== 0) { return f + 1; }
  14. return f;
  15. };