lib/ext_json.js
function stringify(value, replacer, space, options) {
  if (space != null && typeof space === 'object') (options = space), (space = 0);
  if (replacer != null && typeof replacer === 'object')
    (options = replacer), (replacer = null), (space = 0);
  options = Object.assign({}, { relaxed: true }, options);

  const doc = Array.isArray(value)
    ? serializeArray(value, options)
    : serializeDocument(value, options);

  return JSON.stringify(doc, replacer, space);
}
function stringify(value, replacer, space, options) {
  if (space != null && typeof space === 'object') (options = space), (space = 0);
  if (replacer != null && typeof replacer === 'object')
    (options = replacer), (replacer = null), (space = 0);
  options = Object.assign({}, { relaxed: true }, options);

  const doc = Array.isArray(value)
    ? serializeArray(value, options)
    : serializeDocument(value, options);

  return JSON.stringify(doc, replacer, space);
}
Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
function is specified or optionally including only the specified properties if a replacer array
is specified.

@param {object} value The value to convert to extended JSON
@param {function|array} [replacer] A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string
@param {string|number} [space] A String or Number object that's used to insert white space into the output JSON string for readability purposes.
@param {object} [options] Optional settings
@param {boolean} [options.relaxed=true] Enabled Extended JSON's `relaxed` mode
@returns {string}

@example
const EJSON = require('mongodb-extjson');
const Int32 = require('mongodb').Int32;
const doc = { int32: new Int32(10) };

// prints '{"int32":{"$numberInt":"10"}}'
console.log(EJSON.stringify(doc, { relaxed: false }));

// prints '{"int32":10}'
console.log(EJSON.stringify(doc));