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
| const styles = {
list: `list-style-type: none; padding: 0; margin: 0;`,
listItem: `padding: 0 0 0 1em;`,
name: `color: #43a047; font-style: italic`,
proto: `color: #4347a0; font-style: italic`,
function: `color: #4271ae`,
string: `color: #C41A16`,
number: `color: #1C00CF`,
boolean: `color: #AA0D91`,
property: `color: #881391`,
null: `color: #808080`
},
span = (value, style) => ["span", {style}, value],
format = (x, config) => {
if (x === null || x === undefined) return span(String(x), styles.null);
if (typeof x === "boolean") return span(x, styles.boolean);
if (typeof x === "number") return span(x, styles.number);
if (typeof x === "string") return span(`"${x}"`, styles.string);
if (typeof x === "function") return span(x.name || x.toString(), styles.function);
return x ? ['object', {object: x, config}] : null
},
formatBody = (o, config) => {
return [ 'ol', {style: styles.list}, '{', ...Object.keys(o).map(prop => {
return ['li', {style: styles.listItem}, span(prop, styles.property), ': ', format(o[prop], config)]
}),
['li', {style: styles.listItem}, span("[Prototype]", styles.proto), ': ', format(Object.getPrototypeOf(o), config)],
'}'];
}
const Name = Symbol("Name");
if (typeof window !== "undefined") {
const NamedObjectFormatter = {
header(o) {
if (o && o[Name]) return span(o[Name], styles.name)
},
hasBody(o) { return o instanceof Object },
body(o) { return formatBody(o, {expanded: true}) }
};
window.devtoolsFormatters = (window.devtoolsFormatters || []).concat(NamedObjectFormatter);
} |
Partager