forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
186 lines (160 loc) · 4.2 KB
/
Copy pathobject.js
File metadata and controls
186 lines (160 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// Dependencies
const PropTypes = require("prop-types");
const { wrapRender, ellipsisElement } = require("./rep-utils");
const PropRep = require("./prop-rep");
const { MODE } = require("./constants");
const dom = require("react-dom-factories");
const { span } = dom;
const DEFAULT_TITLE = "Object";
/**
* Renders an object. An object is represented by a list of its
* properties enclosed in curly brackets.
*/
ObjectRep.propTypes = {
object: PropTypes.object.isRequired,
// @TODO Change this to Object.values when supported in Node's version of V8
mode: PropTypes.oneOf(Object.keys(MODE).map(key => MODE[key])),
title: PropTypes.string
};
function ObjectRep(props) {
const object = props.object;
const propsArray = safePropIterator(props, object);
if (props.mode === MODE.TINY) {
const tinyModeItems = [];
if (getTitle(props, object) !== DEFAULT_TITLE) {
tinyModeItems.push(getTitleElement(props, object));
} else {
tinyModeItems.push(
span(
{
className: "objectLeftBrace"
},
"{"
),
propsArray.length > 0 ? ellipsisElement : null,
span(
{
className: "objectRightBrace"
},
"}"
)
);
}
return span({ className: "objectBox objectBox-object" }, ...tinyModeItems);
}
return span(
{ className: "objectBox objectBox-object" },
getTitleElement(props, object),
span(
{
className: "objectLeftBrace"
},
" { "
),
...propsArray,
span(
{
className: "objectRightBrace"
},
" }"
)
);
}
function getTitleElement(props, object) {
return span({ className: "objectTitle" }, getTitle(props, object));
}
function getTitle(props, object) {
return props.title || DEFAULT_TITLE;
}
function safePropIterator(props, object, max) {
max = typeof max === "undefined" ? 3 : max;
try {
return propIterator(props, object, max);
} catch (err) {
console.error(err);
}
return [];
}
function propIterator(props, object, max) {
// Work around https://bugzilla.mozilla.org/show_bug.cgi?id=945377
if (Object.prototype.toString.call(object) === "[object Generator]") {
object = Object.getPrototypeOf(object);
}
const elements = [];
const unimportantProperties = [];
let propertiesNumber = 0;
const propertiesNames = Object.keys(object);
const pushPropRep = (name, value) => {
elements.push(
PropRep({
...props,
key: name,
mode: MODE.TINY,
name,
object: value,
equal: ": "
})
);
propertiesNumber++;
if (propertiesNumber < propertiesNames.length) {
elements.push(", ");
}
};
try {
for (const name of propertiesNames) {
if (propertiesNumber >= max) {
break;
}
let value;
try {
value = object[name];
} catch (exc) {
continue;
}
// Object members with non-empty values are preferred since it gives the
// user a better overview of the object.
if (isInterestingProp(value)) {
pushPropRep(name, value);
} else {
// If the property is not important, put its name on an array for later
// use.
unimportantProperties.push(name);
}
}
} catch (err) {
console.error(err);
}
if (propertiesNumber < max) {
for (const name of unimportantProperties) {
if (propertiesNumber >= max) {
break;
}
let value;
try {
value = object[name];
} catch (exc) {
continue;
}
pushPropRep(name, value);
}
}
if (propertiesNumber < propertiesNames.length) {
elements.push(ellipsisElement);
}
return elements;
}
function isInterestingProp(value) {
const type = typeof value;
return type == "boolean" || type == "number" || (type == "string" && value);
}
function supportsObject(object, noGrip = false) {
return noGrip;
}
// Exports from this module
module.exports = {
rep: wrapRender(ObjectRep),
supportsObject
};