Skip to content

Commit

Permalink
fix: ignore undefined and null params on modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
xolott committed Sep 28, 2023
1 parent 58ba426 commit 779d8bf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/utils/getFields.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/isNil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isNil(value) {
return value === null || value === undefined;
}
22 changes: 21 additions & 1 deletion lib/utils/testing/getFields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@ describe('Unit # getFields', function () {
it('Should properly detected top level fields', function () {
// TODO
})
});

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');
})
});
3 changes: 2 additions & 1 deletion lib/utils/testing/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './getFields.test';
import './extractIdsFromSelector.test';
import './extractIdsFromSelector.test';
import './isNil.test';
13 changes: 13 additions & 0 deletions lib/utils/testing/isNil.test.js
Original file line number Diff line number Diff line change
@@ -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([]));
});
});

0 comments on commit 779d8bf

Please sign in to comment.