Mongoose source code analysis (type conversion part)

Mongoose source code

The bottom layer of Mongoose is written in javaScript. javaScript has only one Number type, Number, which can be integer or decimal, so Mongoose is the same

number section:

This part is the process of number converting different types into number

Mongoose is a further optimized encapsulation of the Node's native MongoDB module

The conversion of Mongoose data type and MongoDB data type is completed in the Node's native MongoDB module

Source code:

'use strict';
​
const assert = require('assert');
​
/*!
 * Given a value, cast it to a number, or throw a `CastError` if the value
 * cannot be casted. `null` and `undefined` are considered valid.
 *
 * @param {Any} value
 * @param {String} [path] optional the path to set on the CastError
 * @return {Boolean|null|undefined}
 * @throws {Error} if `value` is not one of the allowed values
 * @api private
 */
​
module.exports = function castNumber(val) {
  if (val == null) {
    return val;
  }
  if (val === '') {
    return null;
  }
​
  if (typeof val === 'string' || typeof val === 'boolean') {
    val = Number(val);
  }
​
  assert.ok(!isNaN(val));
  if (val instanceof Number) {
    return val.valueOf();
  }
  if (typeof val === 'number') {
    return val;
  }
  if (!Array.isArray(val) && typeof val.valueOf === 'function') {
    return Number(val.valueOf());
  }
  if (val.toString && !Array.isArray(val) && val.toString() == Number(val)) {
    return Number(val);
  }
​
  assert.ok(false);
};
  1. The NaN attribute represents a non numeric value and is used to indicate that a value is not a number. isNaN() function to determine whether a value is a NaN value.

  2. The Number() function is a function built in JavaScript. It can convert the value of an object into a number. If it cannot be converted, it returns NaN.

  3. assert. OK (expression, string), assertion, used for verification. If the expression result is false, the following error will be reported and the program will be interrupted. If it is true, no event will occur.

  4. Typeof + variable, which returns the type of pair quantity; instanceof can also return type, but it can return the type of reference object, typeof can't

  5. valueOf(), which returns the original value of the Object, belongs to the Object object. Since all objects inherit Object, they all have, similar to toString()

  6. Array.isArray(), check whether the passed value is of type array

analysis:

if (val == null) {
    return val;
  }

If the value passed in is null, return null directly and exit the function

if (val === '') {
    return null;
  }

If it is an empty string, it also directly returns null and exits the function

if (typeof val === 'string' || typeof val === 'boolean') {
    val = Number(val);
  }

If the type of the incoming value is string or boolean, use the Number() function to convert it to a number type

assert.ok(!isNaN(val));

Judge whether the converted parameter val is a numeric type through assertion. If not, an error will be reported

if (val instanceof Number) {
    return val.valueOf();
  }

If val is a reference object and the type is Number, the original value of val is returned

f (typeof val === 'number') {
    return val;
  }

If Val is Number, return val

if (!Array.isArray(val) && typeof val.valueOf === 'function') {
    return Number(val.valueOf());
  }

If val is not an array and the type of val's original value is a function, return the digitization of val's original value

  if (val.toString && !Array.isArray(val) && val.toString() == Number(val)) {
    return Number(val);
  }

If val can be converted into a string and is not an array, and the value after string conversion is equal to that after digitization, the digitized val is returned

Keywords: Javascript MongoDB Mongoose source code

Added by JovanLo on Thu, 23 Dec 2021 09:52:48 +0200