ToInt8.js 486 B

12345678910111213141516171819
  1. 'use strict';
  2. var modulo = require('./modulo');
  3. var ToNumber = require('./ToNumber');
  4. var truncate = require('./truncate');
  5. var isFinite = require('../helpers/isFinite');
  6. // https://262.ecma-international.org/14.0/#sec-toint8
  7. module.exports = function ToInt8(argument) {
  8. var number = ToNumber(argument);
  9. if (!isFinite(number) || number === 0) {
  10. return 0;
  11. }
  12. var int = truncate(number);
  13. var int8bit = modulo(int, 0x100);
  14. return int8bit >= 0x80 ? int8bit - 0x100 : int8bit;
  15. };