Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var WindowInspectorModule = {}
- /**
- *
- * @type WindowInspectorModule.WindowInspector
- * @author Tarik Curto <tcurto@opensistemas.com>
- */
- WindowInspectorModule.WindowInspector = (function () {
- WindowInspector.MAX_OBJECT_SIZE_ON_CYCLE = 500000;
- function WindowInspector() {
- this.objectList = [];
- this.objectPathList = [];
- this.objectsAddedByCycle = [];
- this.objectsPathAddedByCycle = [];
- }
- /**
- *
- * @param {Object} object
- * @returns {WindowInspector}
- */
- WindowInspector.prototype.setInspectObject = function (object) {
- this.inspectObject = object;
- /*this.objectList[0] = object;
- this.objectPathList[0] = [object.constructor.name];
- this.objectsAddedByCycle[0] = [object];
- this.objectsPathAddedByCycle[0] = [object.constructor.name];*/
- return this;
- }
- /**
- *
- * @returns {void}
- */
- WindowInspector.prototype.makeInspectCycle = function () {
- if (typeof this.inspectObject === "undefined") {
- throw new Error("Undefined object to inspect");
- }
- this.inspectCycle = this.objectsAddedByCycle.length;
- this.objectsAddedByCycle[this.inspectCycle] = [];
- this.objectsPathAddedByCycle[this.inspectCycle] = [];
- var objectListMapElement = this.objectInspectRecursive(this.inspectObject, false, [this.inspectObject.constructor.name]);
- this.pushObjectListToCycle(objectListMapElement.cycle, objectListMapElement.keyCycle);
- }
- /**
- *
- * @param {Array} objectList
- * @returns {void}
- */
- WindowInspector.prototype.pushObjectListToCycle = function (objectList, objectKeyList) {
- var _this = this;
- objectList.forEach(function (object, i) {
- _this.pushObjectToCycle(object, objectKeyList[i]);
- });
- }
- /**
- *
- * @param {Object} object
- * @returns {void}
- */
- WindowInspector.prototype.pushObjectToCycle = function (object, objectKey) {
- if (this.objectList.indexOf(object) !== -1) {
- return;
- }
- //console.debug(`Oject ${object.constructor.name} has discovered`);
- this.objectList.push(object);
- this.objectPathList.push(objectKey);
- this.objectsAddedByCycle[this.inspectCycle].push(object);
- this.objectsPathAddedByCycle[this.inspectCycle].push(objectKey);
- }
- /**
- *
- * @param {Object} object
- * @param {Boolean} isRecursive
- * @returns {{cycle: Array<Object>, keyCycle: Array<Array<String>>}}
- */
- WindowInspector.prototype.objectInspectRecursive = function (object, isRecursive, keyList) {
- if (typeof isRecursive === "undefined" || !isRecursive) {
- this.objectsOnRecursiveCycle = [];
- this.objectsKeysOnRecursiveCycle = [];
- }
- var _this = this, objectMapList = this.objetInspect(object);
- objectMapList.forEach(function (_objectMapElement) {
- if(_objectMapElement.value === _this) {
- return;
- }
- if (_this.objectsOnRecursiveCycle.indexOf(_objectMapElement.value) !== -1) {
- return;
- }
- if(_this.objectsKeysOnRecursiveCycle.length >= WindowInspector.MAX_OBJECT_SIZE_ON_CYCLE){
- return;
- }
- // Clone array object and delete first.
- var _objectMapElementPath = keyList.slice(0);
- _objectMapElementPath.push(_objectMapElement.key);
- _this.objectsKeysOnRecursiveCycle.push(_objectMapElementPath);
- _this.objectsOnRecursiveCycle.push(_objectMapElement.value);
- _this.objectInspectRecursive(_objectMapElement.value, true, _objectMapElementPath);
- });
- return {
- cycle: this.objectsOnRecursiveCycle,
- keyCycle: this.objectsKeysOnRecursiveCycle
- };
- }
- /**
- *
- * @param {Object} object
- * @returns {Array<{key, value}>}
- */
- WindowInspector.prototype.objetInspect = function (object) {
- var objectsMap = this.findObjectsByObject(object);
- return objectsMap;
- }
- /**
- *
- * @param {Object} object
- * @returns {Array<{key, value}>}
- */
- WindowInspector.prototype.findObjectsByObject = function (object) {
- var _this = this,
- objectMap = [];
- Object.keys(object).forEach(function (elementKey) {
- try {
- var element = object[elementKey];
- if (element instanceof Object && typeof element === "object") {
- objectMap.push({ key: elementKey, value: element });
- }
- } catch (e) {
- //console.debug(`Cannot acces to ${elementKey} property.`);
- }
- });
- return objectMap;
- }
- /**
- *
- * @param {Object} object
- * @param {*} value
- * @returns {void|*}
- */
- WindowInspector.keyByValue = function (object, value) {
- Object.keys(object).forEach(function (_key) {
- try {
- var _value = object[_key];
- if (value === _value) {
- return _key;
- }
- } catch (e) {
- }
- })
- return null;
- }
- return WindowInspector;
- }());
- /*
- var windowInspector = new WindowInspectorModule.WindowInspector();
- windowInspector.setInspectObject(window);
- windowInspector.makeInspectCycle();
- var _testObj = {
- 1: {
- 11: {
- 111: { 1111: {}, 1112: {} },
- 112: { 1121: {}, 1122: {} }
- },
- 12: {
- 121: { 1211: {}, 1212: {} },
- 122: { 1221: {}, 1222: {} }
- }
- }
- }
- */
Add Comment
Please, Sign In to add comment