@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
106 lines • 4.74 kB
JavaScript
/**
* 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
*
* 2023-08-10: As of now, passing in obj = null will cause this to crash :|
*
* @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
* @param consoleLog - true is default due to being used before. Set to false to not show in console if it is not neccessary.
*/
export function checkDeepProperty(obj, keys, errReturnType, consoleLog = true) {
let returnValue = null;
if (!keys || keys.length === 0) {
return obj;
}
else if (obj === null || obj === undefined) {
if (errReturnType === 'Actual') {
returnValue = obj;
}
else if (errReturnType === 'EmptyString') {
returnValue = '';
}
else {
returnValue = `obj = ${obj}`;
}
}
else {
let isUndefined = false;
let lastTestedKey = 'obj';
let subObject = obj;
keys.map((key, index) => {
let isLastKey = index === keys.length - 1 ? true : false;
if (isUndefined === false) {
// If checkDeepProperty crashes here, may be because obj was passed in as null
subObject = subObject[key];
lastTestedKey += '.' + key;
if (subObject === undefined) {
isUndefined = true;
if (errReturnType === 'Actual') {
returnValue = undefined;
}
else if (errReturnType === 'EmptyString') {
returnValue = '';
}
else if (errReturnType === 'FullError') {
returnValue = `${lastTestedKey} = undef`;
}
else if (errReturnType === 'ShortError') {
returnValue = `...${key} = undef`;
}
if (consoleLog === true)
console.log('Object Error: checkDeepProperty ~ 39: ', `${lastTestedKey} = undef`);
}
else if (subObject === null) {
isUndefined = true;
if (errReturnType === 'Actual') {
returnValue = null;
}
else if (errReturnType === 'EmptyString') {
returnValue = '';
}
else if (errReturnType === 'FullError') {
returnValue = `${lastTestedKey} = undef`;
}
else if (errReturnType === 'ShortError') {
returnValue = `...${key} = undef`;
}
if (consoleLog === true)
console.log('Object Error: checkDeepProperty ~ 48: ', `${lastTestedKey} = null`);
}
else {
if (isLastKey) { //This is the actual value to test.
returnValue = subObject;
}
}
}
});
return returnValue;
}
}
export function getDeepValuesStringArray(obj, deepProps) {
const DeepValuesArray = deepProps.map((key) => { return `${checkDeepProperty(obj, key.split('.'), 'EmptyString', false)}`; });
return DeepValuesArray;
}
export function getDeepValuesArray(obj, deepProps) {
const DeepValuesArray = deepProps.map((key) => { return checkDeepProperty(obj, key.split('.'), 'EmptyString', false); });
return DeepValuesArray;
}
export function getDeepPropUsingDotNotation(obj, deepPropsStr, errReturnType) {
/**
* ChatGPT Explanation:
This regular expression will match and remove any leading or trailing double curly braces along with any surrounding whitespace.
(^\s*\{{2}|\}{2}\s*$):
^: asserts the position at the start of the string.
\s*: matches any whitespace characters (0 or more).
\{{2}: matches exactly two opening curly braces ({{).
|: OR operator.
\}{2}: matches exactly two closing curly braces (}}).
\s*$: matches any whitespace characters (0 or more) followed by the end of the string.
*/
const keys = deepPropsStr ? deepPropsStr.replace(/(^\s*\{{2}|\}{2}\s*$)/g, '').split('.') : [];
const value = checkDeepProperty(obj, keys, errReturnType);
return value;
}
//# sourceMappingURL=deep.js.map