ToUint16.js 582 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-touint16
  7. var two16 = 0x10000; // Math.pow(2, 16)
  8. module.exports = function ToUint16(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 === 0 ? 0 : int16bit; // in the spec, these are math values, so we filter out -0 here
  16. };