Skip to content

Instantly share code, notes, and snippets.

@dblanchardDev
Last active March 24, 2023 15:14
Show Gist options
  • Save dblanchardDev/bebf181e6635b7fc6165c0d5a64e6e20 to your computer and use it in GitHub Desktop.
Save dblanchardDev/bebf181e6635b7fc6165c0d5a64e6e20 to your computer and use it in GitHub Desktop.
Inspect the properties of an ArcGIS Maps SDK for JavaScript object and optionally print them out to the console.
// Maximum depth objects will be converted
const MAX_DEPTH = 2;
/**
* Check whether an object is likely an ArcGIS Maps SDK object.
* @param {object} obj object or class
* @returns {boolean}
*/
function isMapsSDKObject(obj) {
return typeof obj == "object" && obj !== null && "__accessor__" in obj && "declaredClass" in obj;
}
/**
* Extract properties from an ArcGIS Maps SDK object to create a simplified object.
* @param {object} obj ArcGIS Maps SDK object
* @returns {object} the simplified object listing properties.
*/
function convert(obj, depth=1) {
// Check for required properties
if (!isMapsSDKObject(obj)) {
throw new TypeError("Not an ArcGIS Maps SDK Object");
}
// Extract properties into simple object.
let simple = {
"__esri_class__": obj.declaredClass,
};
for (let propertyName of obj.__accessor__.properties.keys()) {
let value = obj[propertyName];
if (isMapsSDKObject(value)) {
if (depth < MAX_DEPTH) {
value = convert(value, depth + 1);
}
else {
value = `>> ArcGIS Maps SDK ${value.declaredClass} instance <<`;
}
}
simple[propertyName] = value;
}
return simple;
}
/**
* Inspect the properties of an ArcGIS Maps SDK object.
* @param {object} obj ArcGIS Maps SDK Object
* @param {boolean} printout Whether to printout results to the browser's console.
* @returns {object} object listing the properties of the object.
*/
export default function inspect(obj, printout=true) {
let converted = convert(obj);
/** Prinout to console */
if (printout) {
// eslint-disable-next-line no-console
console.table(new Map(Object.entries(converted)));
}
return converted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment