Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author [email protected] (Yurij Mikhalevich)
*/
'use strict';
const ObjectID = require('mongodb').ObjectID;
const { ObjectId } = require('mongodb');


/**
Expand All @@ -26,16 +26,19 @@ exports.prepareMetaData = meta => {
* @param {Array} optParents Object's parents
* @returns {Object} Adjusted clone of object
*/
// eslint-disable-next-line max-statements, complexity
function cloneMeta(node, optParents) {
if (!((node !== null && typeof node === 'object') || typeof node === 'function')
|| (node instanceof ObjectID) || (node instanceof Buffer)) {
if (typeof node !== 'object' || typeof node === 'function' || node === null) {
return node;
}
if (node instanceof ObjectId || node instanceof Buffer) {
return node;
}
let copy = Array.isArray(node) ? [] : {};
if (node instanceof Date) {
return new Date(node.getTime());
} else if (node instanceof Error) {
// This is needed because Error's message, name and stack isn't accessible when cycling through properties
// This is needed because Error's message, name, and stack aren't accessible when cycling through properties
copy = { message: node.message, name: node.name, stack: node.stack };
}
optParents = optParents || [];
Expand All @@ -49,7 +52,7 @@ function cloneMeta(node, optParents) {
if (newKey.includes('.') || newKey.includes('$')) {
newKey = newKey.replace(/\./g, '[dot]').replace(/\$/g, '[$]');
}
if ((value !== null && typeof value === 'object') || typeof value === 'function') {
if (typeof value === 'object' || typeof value === 'function') {
if (optParents.indexOf(value) === -1) {
copy[newKey] = cloneMeta(value, optParents);
} else {
Expand Down
Loading