ToUint8.js 578 B

1234567891011121314151617181920
  1. 'use strict';
  2. var ToNumber = require('./ToNumber');
  3. var $isNaN = require('../helpers/isNaN');
  4. var $isFinite = require('../helpers/isFinite');
  5. var $sign = require('../helpers/sign');
  6. var abs = require('./abs');
  7. var floor = require('./floor');
  8. var modulo = require('./modulo');
  9. // https://ecma-international.org/ecma-262/6.0/#sec-touint8
  10. module.exports = function ToUint8(argument) {
  11. var number = ToNumber(argument);
  12. if ($isNaN(number) || number === 0 || !$isFinite(number)) { return 0; }
  13. var posInt = $sign(number) * floor(abs(number));
  14. return modulo(posInt, 0x100);
  15. };