UNPKG

mic-inspector

Version:

A react inspector which a most similar of Chorme DevTools inspector

163 lines (162 loc) 6.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNamedDescriptors = exports.sortNamedDescriptors = exports.getNameType = void 0; var types_1 = require("./types"); var _1 = require("."); var PROTO = '__proto__'; var Number = window.Number; var prototypeGetter = (Object.getOwnPropertyDescriptor(Object.prototype, PROTO) || {}).get; /** * Get the name type of the property * @param name Property name */ exports.getNameType = function (name) { // if the name type is 'symbol' if (typeof name === 'symbol') { return types_1.DescriptorNameType.Symbol; } var number = Number.parseInt(name); // if the name type is not 'number' if (Number.isNaN(number)) { return types_1.DescriptorNameType.String; } return ( // if the names are same that represents the type of number is int number.toString() === name ? types_1.DescriptorNameType.Int : types_1.DescriptorNameType.Float) | ( // if the number is 0 number === 0 ? types_1.DescriptorNameType.Zero : ( // if the number is greater than 0 number > 0 ? types_1.DescriptorNameType.Positive : types_1.DescriptorNameType.Negative)); }; /** * A method to sort the descriptors * @param descriptor1 Property descriptor * @param descriptor2 Another property descriptor */ exports.sortNamedDescriptors = function (descriptor1, descriptor2) { var n1 = descriptor1.name, v1 = descriptor1.valueType, nt1 = descriptor1.nameType; var n2 = descriptor2.name, v2 = descriptor2.valueType, nt2 = descriptor2.nameType; // if the name of the first one is index if ((nt1 & types_1.DescriptorNameType.Index) === types_1.DescriptorNameType.Index) { // if the name of the second one is index too if ((nt2 & types_1.DescriptorNameType.Index) === types_1.DescriptorNameType.Index) { return n1 - n2 < 0 ? -1 : 1; } // insert the descriptor1 before descriptor2 return -1; } // if the name of the second one is index else if ((nt2 & types_1.DescriptorNameType.Index) === types_1.DescriptorNameType.Index) { // insert the descriptor2 before descriptor1 return 1; } // if the first one is __proto__ if (n1 === PROTO) { // insert the descriptor2 before descriptor1 return 1; } // if the second one is __proto__ else if (n2 === PROTO) { // insert the descriptor1 before descriptor2 return -1; } // if the first one is enumerable if (descriptor1.enumerable) { // if the second one is 'not' enumerable if (!descriptor2.enumerable) { // insert the descriptor1 before descriptor2 return -1; } } // if the second one is enumerable else if (descriptor2.enumerable) { // insert the descriptor2 before descriptor1 return 1; } // if the first one is an accessor if ((v1 & types_1.DescriptorValueType.Accessor) === types_1.DescriptorValueType.Accessor) { // if the second one is 'not' an accessor if ((v2 & types_1.DescriptorValueType.Accessor) !== types_1.DescriptorValueType.Accessor) { // insert the descriptor2 before descriptor1 return 1; } // if the names are same if (n1 === n2) { // if the first one is a getter, then insert the descriptor1 before descriptor2 return (v1 & types_1.DescriptorValueType.Getter) === types_1.DescriptorValueType.Getter ? -1 : 1; } } // if the second one is an accessor else if ((v2 & types_1.DescriptorValueType.Accessor) === types_1.DescriptorValueType.Accessor) { // insert the descriptor1 before descriptor2 return -1; } // sorted by name return n1 > n2 ? 1 : -1; }; /** * Get the sorted named descriptors of the specific property value * @param propertyValue Property value * @param owner An object as the owner that has these descriptors */ exports.getNamedDescriptors = function (propertyValue, owner) { if (owner === void 0) { owner = propertyValue; } var prototype = Object.getPrototypeOf(propertyValue); var names = Object.getOwnPropertyNames(propertyValue); var descriptors = []; // for each names for (var _i = 0, names_1 = names; _i < names_1.length; _i++) { var name_1 = names_1[_i]; var descriptor = Object.getOwnPropertyDescriptor(propertyValue, name_1); // if no descriptor if (!descriptor) { continue; } var _a = descriptor, value = _a.value, get = _a.get, set = _a.set, enumerable = _a.enumerable, configurable = _a.configurable; // if accessor existed if (get || set) { // if getter existed if (get) { try { // if the name is not '__proto__' or the getter is not the getter of object prototype if (name_1 !== PROTO || get !== prototypeGetter) { descriptors.push(new _1.NamedDescriptor(owner, name_1, get.call(owner), types_1.DescriptorValueType.Result, enumerable, configurable)); } } catch (e) { descriptors.push(new _1.NamedDescriptor(owner, name_1, get, set, enumerable, configurable)); } descriptors.push(new _1.NamedDescriptor(owner, name_1, get, types_1.DescriptorValueType.Getter, false, true)); } if (set) { descriptors.push(new _1.NamedDescriptor(owner, name_1, set, types_1.DescriptorValueType.Setter, false, true)); } continue; } descriptors.push(new _1.NamedDescriptor(owner, name_1, value, types_1.DescriptorValueType.Normal, enumerable, configurable)); } // if prototype is existed if (prototype) { // for each descriptors of the prototype for (var _b = 0, _c = exports.getNamedDescriptors(prototype, owner); _b < _c.length; _b++) { var descriptor = _c[_b]; var name_2 = descriptor.name, valueType = descriptor.valueType; // if the name included by names or the name is '__proto__' if (names.includes(name_2) || name_2 === PROTO) { continue; } // if the value type is not DescriptorValueType.Result if ((valueType & types_1.DescriptorValueType.Result) !== types_1.DescriptorValueType.Result) { continue; } descriptors.push(descriptor); } descriptors.push(new _1.NamedDescriptor(propertyValue, PROTO, prototype, types_1.DescriptorValueType.Normal, false)); } // return a list after sort these descriptors return descriptors.sort(exports.sortNamedDescriptors); };