@dhyasama/totem-merge 6.17.0 → 6.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/helpers.js +18 -3
  2. package/package.json +1 -1
package/lib/helpers.js CHANGED
@@ -24,17 +24,32 @@ var mergeObjects = function mergeObjects(target, source, doNotMergeKeys) {
24
24
  if (source == 'undefined') return;
25
25
  else if (source == 'MongooseDocument { undefined }') return;
26
26
 
27
- var jsonSource = JSON.parse(JSON.stringify(source));
27
+ // Safely convert potential Mongoose documents to plain objects without
28
+ // invoking virtual getters, which can throw during serialization
29
+ var toPlainObject = function toPlainObject(value) {
30
+ if (!value) return value;
31
+ if (typeof value.toObject === 'function') {
32
+ try {
33
+ return value.toObject({ virtuals: false, getters: false, depopulate: true, minimize: false });
34
+ }
35
+ catch (e) {
36
+ // fall through to JSON stringify fallback below
37
+ }
38
+ }
39
+ try { return JSON.parse(JSON.stringify(value)); } catch (e) { return {}; }
40
+ };
41
+
42
+ var jsonSource = toPlainObject(source);
28
43
 
29
44
  for (var key in jsonSource) {
30
45
 
31
46
  if (doNotMergeKeys.indexOf(key) >= 0) continue;
32
47
 
33
- var sourceItem = source[key];
48
+ var sourceItem = jsonSource[key];
34
49
  if (!sourceItem) continue;
35
50
 
36
51
  var type = typeOf(target[key]);
37
- if (type == 'null') type = typeOf(source[key]);
52
+ if (type == 'null') type = typeOf(sourceItem);
38
53
 
39
54
  if (type == 'object') {
40
55
  mergeObjects(target[key], sourceItem, doNotMergeKeys);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhyasama/totem-merge",
3
- "version": "6.17.0",
3
+ "version": "6.18.0",
4
4
  "description": "Org and person merging package",
5
5
  "main": "index.js",
6
6
  "scripts": {