Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore undefined and null params on modifiers #399

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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([]));
});
});
Loading