All files / mongoose/lib/cast int32.js

23.07% Statements 3/13
0% Branches 0/11
0% Functions 0/1
23.07% Lines 3/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37    1x 1x                       1x                                          
'use strict';
 
const isBsonType = require('../helpers/isBsonType');
const assert = require('assert');
 
/**
 * Given a value, cast it to a Int32, or throw an `Error` if the value
 * cannot be casted. `null` and `undefined` are considered valid.
 *
 * @param {Any} value
 * @return {Number}
 * @throws {Error} if `value` does not represent an integer, or is outside the bounds of an 32-bit integer.
 * @api private
 */
 
module.exports = function castInt32(val) {
  if (val == null) {
    return val;
  }
  if (val === '') {
    return null;
  }
 
  const coercedVal = isBsonType(val, 'Long') ? val.toNumber() : Number(val);
 
  const INT32_MAX = 0x7FFFFFFF;
  const INT32_MIN = -0x80000000;
 
  if (coercedVal === (coercedVal | 0) &&
      coercedVal >= INT32_MIN &&
      coercedVal <= INT32_MAX
  ) {
    return coercedVal;
  }
  assert.ok(false);
};