All files / mongoose/lib/cast bigint.js

21.42% Statements 3/14
0% Branches 0/12
0% Functions 0/1
21.42% Lines 3/14

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 assert = require('assert');
const { Long } = require('bson');
 
/**
 * Given a value, cast it to a BigInt, 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` is not one of the allowed values
 * @api private
 */
 
module.exports = function castBigInt(val) {
  if (val == null) {
    return val;
  }
  if (val === '') {
    return null;
  }
  if (typeof val === 'bigint') {
    return val;
  }
 
  if (val instanceof Long) {
    return val.toBigInt();
  }
 
  if (typeof val === 'string' || typeof val === 'number') {
    return BigInt(val);
  }
 
  assert.ok(false);
};