From 779d8bfc80bd8dca93488151edf7a33bd85b36e7 Mon Sep 17 00:00:00 2001 From: Jose Truyol Date: Thu, 28 Sep 2023 13:50:55 -0700 Subject: [PATCH] fix: ignore undefined and null params on modifiers --- lib/utils/getFields.js | 4 +++- lib/utils/isNil.js | 3 +++ lib/utils/testing/getFields.test.js | 22 +++++++++++++++++++++- lib/utils/testing/index.js | 3 ++- lib/utils/testing/isNil.test.js | 13 +++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 lib/utils/isNil.js create mode 100644 lib/utils/testing/isNil.test.js diff --git a/lib/utils/getFields.js b/lib/utils/getFields.js index 2457c0c7..da8de2ea 100644 --- a/lib/utils/getFields.js +++ b/lib/utils/getFields.js @@ -1,3 +1,5 @@ +import isNil from "./isNil"; + /** * Taken from: https://github.com/Meteor-Community-Packages/meteor-collection-hooks/blob/master/collection-hooks.js#L194 and modified. * @param mutator @@ -7,7 +9,7 @@ export default function getFields(mutator) { var fields = []; var topLevelFields = []; - Object.entries(mutator).forEach(function ([op, params]) { + Object.entries(mutator).filter(([_, params]) => !isNil(params)).forEach(function ([op, params]) { if (op[0] == '$') { Object.keys(params).forEach(function (field) { // record the field we are trying to change diff --git a/lib/utils/isNil.js b/lib/utils/isNil.js new file mode 100644 index 00000000..ce87ea8b --- /dev/null +++ b/lib/utils/isNil.js @@ -0,0 +1,3 @@ +export default function isNil(value) { + return value === null || value === undefined; +} diff --git a/lib/utils/testing/getFields.test.js b/lib/utils/testing/getFields.test.js index 9c024ab7..134c4458 100644 --- a/lib/utils/testing/getFields.test.js +++ b/lib/utils/testing/getFields.test.js @@ -37,4 +37,24 @@ describe('Unit # getFields', function () { it('Should properly detected top level fields', function () { // TODO }) -}); \ No newline at end of file + + it('Should ignore null or undefined params', function () { + let fields = run({ + $set: { + a: { + d: 1 + }, + 'profile.test': 1 + }, + $inc: { + b: 1 + }, + $addToSet: undefined + }).fields; + + assert.lengthOf(fields, 3); + assert.include(fields, 'a'); + assert.include(fields, 'b'); + assert.include(fields, 'profile.test'); + }) +}); diff --git a/lib/utils/testing/index.js b/lib/utils/testing/index.js index e60c2018..7a4f35c0 100644 --- a/lib/utils/testing/index.js +++ b/lib/utils/testing/index.js @@ -1,2 +1,3 @@ import './getFields.test'; -import './extractIdsFromSelector.test'; \ No newline at end of file +import './extractIdsFromSelector.test'; +import './isNil.test'; diff --git a/lib/utils/testing/isNil.test.js b/lib/utils/testing/isNil.test.js new file mode 100644 index 00000000..595c1280 --- /dev/null +++ b/lib/utils/testing/isNil.test.js @@ -0,0 +1,13 @@ +import { assert } from 'chai'; +import run from '../isNil'; + +describe('Unit # isNil', function () { + it('Should work', function () { + assert.isTrue(run(null)); + assert.isTrue(run(undefined)); + assert.isFalse(run(1)); + assert.isFalse(run('1')); + assert.isFalse(run({})); + assert.isFalse(run([])); + }); +});