forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
163 lines (144 loc) · 3.22 KB
/
Copy patharray.js
File metadata and controls
163 lines (144 loc) · 3.22 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
/* 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 dom = require("react-dom-factories");
const PropTypes = require("prop-types");
const { wrapRender } = require("./rep-utils");
const { MODE } = require("./constants");
const { span } = dom;
const ModePropType = PropTypes.oneOf(
// @TODO Change this to Object.values when supported in Node's version of V8
Object.keys(MODE).map(key => MODE[key])
);
/**
* Renders an array. The array is enclosed by left and right bracket
* and the max number of rendered items depends on the current mode.
*/
ArrayRep.propTypes = {
mode: ModePropType,
object: PropTypes.array.isRequired
};
function ArrayRep(props) {
const { object, mode = MODE.SHORT } = props;
let items;
let brackets;
const needSpace = function(space) {
return space ? { left: "[ ", right: " ]" } : { left: "[", right: "]" };
};
if (mode === MODE.TINY) {
const isEmpty = object.length === 0;
if (isEmpty) {
items = [];
} else {
items = [
span(
{
className: "more-ellipsis",
title: "more…"
},
"…"
)
];
}
brackets = needSpace(false);
} else {
items = arrayIterator(props, object, maxLengthMap.get(mode));
brackets = needSpace(items.length > 0);
}
return span(
{
className: "objectBox objectBox-array"
},
span(
{
className: "arrayLeftBracket"
},
brackets.left
),
...items,
span(
{
className: "arrayRightBracket"
},
brackets.right
)
);
}
function arrayIterator(props, array, max) {
const items = [];
for (let i = 0; i < array.length && i < max; i++) {
const config = {
mode: MODE.TINY,
delim: i == array.length - 1 ? "" : ", "
};
let item;
try {
item = ItemRep({
...props,
...config,
object: array[i]
});
} catch (exc) {
item = ItemRep({
...props,
...config,
object: exc
});
}
items.push(item);
}
if (array.length > max) {
items.push(
span(
{
className: "more-ellipsis",
title: "more…"
},
"…"
)
);
}
return items;
}
/**
* Renders array item. Individual values are separated by a comma.
*/
ItemRep.propTypes = {
object: PropTypes.any.isRequired,
delim: PropTypes.string.isRequired,
mode: ModePropType
};
function ItemRep(props) {
const { Rep } = require("./rep");
const { object, delim, mode } = props;
return span(
{},
Rep({
...props,
object: object,
mode: mode
}),
delim
);
}
function getLength(object) {
return object.length;
}
function supportsObject(object) {
return (
Array.isArray(object) ||
Object.prototype.toString.call(object) === "[object Arguments]"
);
}
const maxLengthMap = new Map();
maxLengthMap.set(MODE.SHORT, 3);
maxLengthMap.set(MODE.LONG, 10);
// Exports from this module
module.exports = {
rep: wrapRender(ArrayRep),
supportsObject,
maxLengthMap,
getLength,
ModePropType
};