toUInt31.js 476 B

123456789101112131415
  1. var MAX_INT = require('./MAX_INT');
  2. /**
  3. * "Convert" value into an 31-bit unsigned integer (since 1 bit is used for sign).
  4. * IMPORTANT: value wil wrap at 2^31, if negative will return 0.
  5. */
  6. function toUInt31(val){
  7. // we do not use lang/toNumber because of perf and also because it
  8. // doesn't break the functionality
  9. return (val <= 0)? 0 : (val > MAX_INT? ~~(val % (MAX_INT + 1)) : ~~val);
  10. }
  11. module.exports = toUInt31;