@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
69 lines • 2.97 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkDeepProperty = void 0;
/**
* checkDeepProperty - Originally built for Pivot Tiles banner component
* Checks for sub-sub-property of object by going one layer at a time to avoid undefined error
*
* @param obj - parent object you want to check
* @param keys - key array to check (parent.check.this keys are ['check','this'] )
* @param errReturnType - what to do when there is an error
*/
function checkDeepProperty(obj, keys, errReturnType) {
if (!keys || keys.length === 0) {
return obj;
}
else {
var returnValue_1 = null;
var isUndefined_1 = false;
var lastTestedKey_1 = 'obj';
var subObject_1 = obj;
keys.map(function (key, index) {
var isLastKey = index === keys.length - 1 ? true : false;
if (isUndefined_1 === false) {
subObject_1 = subObject_1[key];
lastTestedKey_1 += '.' + key;
if (subObject_1 === undefined) {
isUndefined_1 = true;
if (errReturnType === 'Actual') {
returnValue_1 = undefined;
}
else if (errReturnType === 'EmptyString') {
returnValue_1 = '';
}
else if (errReturnType === 'FullError') {
returnValue_1 = "".concat(lastTestedKey_1, " = undef");
}
else if (errReturnType === 'ShortError') {
returnValue_1 = "...".concat(key, " = undef");
}
console.log('Object Error: ~ 106: ', "".concat(lastTestedKey_1, " = undef"));
}
else if (subObject_1 === null) {
isUndefined_1 = true;
if (errReturnType === 'Actual') {
returnValue_1 = null;
}
else if (errReturnType === 'EmptyString') {
returnValue_1 = '';
}
else if (errReturnType === 'FullError') {
returnValue_1 = "".concat(lastTestedKey_1, " = undef");
}
else if (errReturnType === 'ShortError') {
returnValue_1 = "...".concat(key, " = undef");
}
console.log('Object Error: ~ 106: ', "".concat(lastTestedKey_1, " = null"));
}
else {
if (isLastKey) { //This is the actual value to test.
returnValue_1 = subObject_1;
}
}
}
});
return returnValue_1;
}
}
exports.checkDeepProperty = checkDeepProperty;
//# sourceMappingURL=properties.js.map
;