Skip to content

Commit

Permalink
In number diff check for NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
lukejagodzinski committed May 20, 2019
1 parent f31f03f commit 843afc4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .versions
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ [email protected]
[email protected]
[email protected]
[email protected]
jagi:[email protected].0
jagi:[email protected].1
[email protected]
local-test:jagi:[email protected].0
local-test:jagi:[email protected].1
[email protected]
mdg:[email protected]
[email protected]
Expand Down
64 changes: 34 additions & 30 deletions lib/modules/storage/utils/getModifier.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import _each from 'lodash/each';
import _isNumber from 'lodash/isNumber';
import _isPlainObject from 'lodash/isPlainObject';
import _omitBy from 'lodash/omitBy';
import _size from 'lodash/size';
import { EJSON } from 'meteor/ejson';
import throwParseError from '../../core/utils/throw_parse_error.js';
import rawMany from '../../fields/utils/rawMany';
import diff from './diff';
import _each from "lodash/each";
import _isNaN from "lodash/isNaN";
import _isNumber from "lodash/isNumber";
import _isPlainObject from "lodash/isPlainObject";
import _omitBy from "lodash/omitBy";
import _size from "lodash/size";
import { EJSON } from "meteor/ejson";
import throwParseError from "../../core/utils/throw_parse_error.js";
import rawMany from "../../fields/utils/rawMany";
import diff from "./diff";

const handlers = {};

handlers.onObjectDiff = function({oldDoc, newDoc, prefix, result}) {
handlers.onObjectDiff = function({ oldDoc, newDoc, prefix, result }) {
diff({
oldDoc,
newDoc,
Expand All @@ -20,7 +21,7 @@ handlers.onObjectDiff = function({oldDoc, newDoc, prefix, result}) {
});
};

handlers.onListDiff = function({oldList, newList, prefix, result}) {
handlers.onListDiff = function({ oldList, newList, prefix, result }) {
// NOTE: We need check a new array size. If its length increased or stayed the
// same then all changes can be registered using the $set modifier. If an
// array length decreased, then we should slice it. However it may not be
Expand Down Expand Up @@ -55,8 +56,7 @@ handlers.onListDiff = function({oldList, newList, prefix, result}) {
result,
...handlers
});
}
else {
} else {
result.$set[arrayPrefix] = newElement;
}
}
Expand All @@ -76,8 +76,7 @@ handlers.onListDiff = function({oldList, newList, prefix, result}) {
result,
...handlers
});
}
else {
} else {
result.$set[arrayPrefix] = newElement;
}
}
Expand Down Expand Up @@ -107,17 +106,20 @@ handlers.onListDiff = function({oldList, newList, prefix, result}) {
}
};

handlers.onScalarDiff = function({oldValue, newValue, prefix, result}) {
handlers.onScalarDiff = function({ oldValue, newValue, prefix, result }) {
if (newValue !== undefined) {
if (_isNumber(oldValue) && _isNumber(newValue)) {
if (
_isNumber(oldValue) &&
_isNumber(newValue) &&
!_isNaN(oldValue) &&
!_isNaN(newValue)
) {
result.$inc[prefix] = newValue - oldValue;
}
else {
} else {
result.$set[prefix] = newValue;
}
}
else {
result.$unset[prefix] = '';
} else {
result.$unset[prefix] = "";
}
};

Expand Down Expand Up @@ -145,14 +147,16 @@ const getModifier = function(options = {}) {
// subscribed to the publication publishing given document or we modified the
// _id of a document.
if (!oldDoc) {
throwParseError([{
'module': 'storage'
}, {
'utility': 'getModified'
throwParseError([
{
module: "storage"
},
{
utility: "getModified"
},
`Can not get a document before modifications. You are not subscribed ` +
`to the publication publishing a "${Class.getName()}" document with ` +
`the id "${newDoc._id}" or you have modified the "_id" field`
`to the publication publishing a "${Class.getName()}" document with ` +
`the id "${newDoc._id}" or you have modified the "_id" field`
]);
}

Expand Down Expand Up @@ -184,9 +188,9 @@ const getModifier = function(options = {}) {
});

// Return only non empty modifiers.
return _omitBy(result, (modifier) => {
return _omitBy(result, modifier => {
return _size(modifier) === 0;
});
};

export default getModifier;
export default getModifier;

0 comments on commit 843afc4

Please sign in to comment.