mongoose-diff-tracking
Version:
Manage Mongo Collection diff History and versions
90 lines (78 loc) • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getNestedValue = getNestedValue;
exports.hasOwnNestedProperty = hasOwnNestedProperty;
var _documentPath = require('./document-path.js');
/**
* @public
* Get the value in the object at the DocumentPath.
*/
function getNestedValue(obj, docPath, noNullAccess) {
var keys = (0, _documentPath.parseDocumentPath)(docPath);
var currentObj = obj;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
try {
currentObj = currentObj[key];
} catch (e) {
if (noNullAccess) {
throw new Error('Cannot get value by the document path: "' + docPath + '". The property "' + key + '" is not found in the upper undefined object.');
}
return undefined;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return currentObj;
}
/**
* @public
* Check if the object has the DocumentPath.
*/
function hasOwnNestedProperty(obj, docPath) {
var currentObj = obj;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _documentPath.parseDocumentPath)(docPath)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var key = _step2.value;
if (!currentObj || !currentObj.hasOwnProperty(key)) {
return false;
}
currentObj = currentObj[key];
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return true;
}