Skip to content

Commit

Permalink
simplify isPlainObject check
Browse files Browse the repository at this point in the history
  • Loading branch information
Freezystem committed Feb 5, 2025
1 parent 41d815b commit 4eb9a95
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 1 addition & 5 deletions packages/moleculer-db/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ function flatten(obj, prefix = "") {
const value = obj[key];

// Check if value is a plain object (not array, not null, and constructor is Object)
const isPlainObject =
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
value.constructor === Object;
const isPlainObject = typeof value === "object" && value && value.constructor === Object;

// If it's a plain object, flatten it recursively
// Otherwise, keep the value as is (handles primitives, null, undefined, arrays, dates, ObjectId, etc.)
Expand Down
4 changes: 4 additions & 0 deletions packages/moleculer-db/test/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (process.versions.node.split(".")[0] < 14) {
const date = new Date("2024-01-01");
const obj = {
name: "John",
active: true,
address: {
street: "Main St",
location: {
Expand All @@ -25,6 +26,7 @@ if (process.versions.node.split(".")[0] < 14) {
account: {
createdAt: date,
identifier: uuid,
isSync: false,
settings: {
theme: null,
language: undefined
Expand All @@ -35,13 +37,15 @@ if (process.versions.node.split(".")[0] < 14) {

const expected = {
"name": "John",
"active": true,
"address.street": "Main St",
"address.location.city": "Boston",
"address.location.country": "USA",
"account.createdAt": date,
"account.identifier": uuid,
"account.settings.theme": null,
"account.settings.language": undefined,
"account.isSync": false,
"scores": [85, 90, 95],
};

Expand Down

0 comments on commit 4eb9a95

Please sign in to comment.