ToUint8Clamp.js 603 B

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