Guest User

Untitled

a guest
Feb 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. var WindowInspectorModule = {}
  2.  
  3. /**
  4. *
  5. * @type WindowInspectorModule.WindowInspector
  6. * @author Tarik Curto <tcurto@opensistemas.com>
  7. */
  8. WindowInspectorModule.WindowInspector = (function () {
  9.  
  10.  
  11. WindowInspector.MAX_OBJECT_SIZE_ON_CYCLE = 500000;
  12.  
  13. function WindowInspector() {
  14.  
  15. this.objectList = [];
  16. this.objectPathList = [];
  17. this.objectsAddedByCycle = [];
  18. this.objectsPathAddedByCycle = [];
  19. }
  20.  
  21. /**
  22. *
  23. * @param {Object} object
  24. * @returns {WindowInspector}
  25. */
  26. WindowInspector.prototype.setInspectObject = function (object) {
  27.  
  28. this.inspectObject = object;
  29. /*this.objectList[0] = object;
  30. this.objectPathList[0] = [object.constructor.name];
  31. this.objectsAddedByCycle[0] = [object];
  32. this.objectsPathAddedByCycle[0] = [object.constructor.name];*/
  33. return this;
  34. }
  35.  
  36. /**
  37. *
  38. * @returns {void}
  39. */
  40. WindowInspector.prototype.makeInspectCycle = function () {
  41.  
  42. if (typeof this.inspectObject === "undefined") {
  43. throw new Error("Undefined object to inspect");
  44. }
  45.  
  46. this.inspectCycle = this.objectsAddedByCycle.length;
  47. this.objectsAddedByCycle[this.inspectCycle] = [];
  48. this.objectsPathAddedByCycle[this.inspectCycle] = [];
  49.  
  50. var objectListMapElement = this.objectInspectRecursive(this.inspectObject, false, [this.inspectObject.constructor.name]);
  51. this.pushObjectListToCycle(objectListMapElement.cycle, objectListMapElement.keyCycle);
  52. }
  53.  
  54. /**
  55. *
  56. * @param {Array} objectList
  57. * @returns {void}
  58. */
  59. WindowInspector.prototype.pushObjectListToCycle = function (objectList, objectKeyList) {
  60.  
  61. var _this = this;
  62. objectList.forEach(function (object, i) {
  63. _this.pushObjectToCycle(object, objectKeyList[i]);
  64. });
  65. }
  66.  
  67. /**
  68. *
  69. * @param {Object} object
  70. * @returns {void}
  71. */
  72. WindowInspector.prototype.pushObjectToCycle = function (object, objectKey) {
  73.  
  74. if (this.objectList.indexOf(object) !== -1) {
  75. return;
  76. }
  77.  
  78. //console.debug(`Oject ${object.constructor.name} has discovered`);
  79.  
  80. this.objectList.push(object);
  81. this.objectPathList.push(objectKey);
  82. this.objectsAddedByCycle[this.inspectCycle].push(object);
  83. this.objectsPathAddedByCycle[this.inspectCycle].push(objectKey);
  84. }
  85.  
  86. /**
  87. *
  88. * @param {Object} object
  89. * @param {Boolean} isRecursive
  90. * @returns {{cycle: Array<Object>, keyCycle: Array<Array<String>>}}
  91. */
  92. WindowInspector.prototype.objectInspectRecursive = function (object, isRecursive, keyList) {
  93.  
  94. if (typeof isRecursive === "undefined" || !isRecursive) {
  95. this.objectsOnRecursiveCycle = [];
  96. this.objectsKeysOnRecursiveCycle = [];
  97. }
  98.  
  99. var _this = this, objectMapList = this.objetInspect(object);
  100.  
  101. objectMapList.forEach(function (_objectMapElement) {
  102.  
  103. if(_objectMapElement.value === _this) {
  104. return;
  105. }
  106.  
  107. if (_this.objectsOnRecursiveCycle.indexOf(_objectMapElement.value) !== -1) {
  108. return;
  109. }
  110.  
  111. if(_this.objectsKeysOnRecursiveCycle.length >= WindowInspector.MAX_OBJECT_SIZE_ON_CYCLE){
  112. return;
  113. }
  114.  
  115. // Clone array object and delete first.
  116. var _objectMapElementPath = keyList.slice(0);
  117. _objectMapElementPath.push(_objectMapElement.key);
  118.  
  119. _this.objectsKeysOnRecursiveCycle.push(_objectMapElementPath);
  120. _this.objectsOnRecursiveCycle.push(_objectMapElement.value);
  121.  
  122. _this.objectInspectRecursive(_objectMapElement.value, true, _objectMapElementPath);
  123. });
  124.  
  125. return {
  126. cycle: this.objectsOnRecursiveCycle,
  127. keyCycle: this.objectsKeysOnRecursiveCycle
  128. };
  129. }
  130.  
  131. /**
  132. *
  133. * @param {Object} object
  134. * @returns {Array<{key, value}>}
  135. */
  136. WindowInspector.prototype.objetInspect = function (object) {
  137.  
  138. var objectsMap = this.findObjectsByObject(object);
  139. return objectsMap;
  140. }
  141.  
  142. /**
  143. *
  144. * @param {Object} object
  145. * @returns {Array<{key, value}>}
  146. */
  147. WindowInspector.prototype.findObjectsByObject = function (object) {
  148.  
  149. var _this = this,
  150. objectMap = [];
  151. Object.keys(object).forEach(function (elementKey) {
  152. try {
  153.  
  154. var element = object[elementKey];
  155. if (element instanceof Object && typeof element === "object") {
  156. objectMap.push({ key: elementKey, value: element });
  157. }
  158.  
  159. } catch (e) {
  160.  
  161. //console.debug(`Cannot acces to ${elementKey} property.`);
  162. }
  163. });
  164.  
  165. return objectMap;
  166. }
  167.  
  168. /**
  169. *
  170. * @param {Object} object
  171. * @param {*} value
  172. * @returns {void|*}
  173. */
  174. WindowInspector.keyByValue = function (object, value) {
  175. Object.keys(object).forEach(function (_key) {
  176. try {
  177. var _value = object[_key];
  178. if (value === _value) {
  179. return _key;
  180. }
  181. } catch (e) {
  182.  
  183. }
  184. })
  185. return null;
  186. }
  187.  
  188. return WindowInspector;
  189.  
  190. }());
  191.  
  192. /*
  193. var windowInspector = new WindowInspectorModule.WindowInspector();
  194. windowInspector.setInspectObject(window);
  195. windowInspector.makeInspectCycle();
  196.  
  197. var _testObj = {
  198. 1: {
  199. 11: {
  200. 111: { 1111: {}, 1112: {} },
  201. 112: { 1121: {}, 1122: {} }
  202. },
  203. 12: {
  204. 121: { 1211: {}, 1212: {} },
  205. 122: { 1221: {}, 1222: {} }
  206. }
  207. }
  208. }
  209. */
Add Comment
Please, Sign In to add comment