ToInt16.js 536 B

123456789101112131415161718192021
  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-toint16
  7. var two16 = 0x10000; // Math.pow(2, 16);
  8. module.exports = function ToInt16(argument) {
  9. var number = ToNumber(argument);
  10. if (!isFinite(number) || number === 0) {
  11. return 0;
  12. }
  13. var int = truncate(number);
  14. var int16bit = modulo(int, two16);
  15. return int16bit >= 0x8000 ? int16bit - two16 : int16bit;
  16. };