From 4647c998ea4036ec49e724dc575d17ec6ab51b8f Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 22 Oct 2021 14:28:27 -0500 Subject: [PATCH] feat: fix Debug component (#1633) --- packages/astro/components/Debug.astro | 302 +++----------------------- packages/astro/components/index.js | 4 +- 2 files changed, 27 insertions(+), 279 deletions(-) diff --git a/packages/astro/components/Debug.astro b/packages/astro/components/Debug.astro index b09f870a7..c25ce0f0e 100644 --- a/packages/astro/components/Debug.astro +++ b/packages/astro/components/Debug.astro @@ -1,144 +1,6 @@ --- const key = Object.keys(Astro.props)[0]; const value = Astro.props[key]; - -const getType = (node: unknown) => { - if (Array.isArray(node)) return 'array'; - if (node === null) return 'null'; - if (typeof node === 'object') { - if ((node as any).then) return 'promise'; - } - return typeof node; -}; - -const getSummary = (node: any, key: string, className: string) => { - const type = getType(node); - let value; - let open; - let close; - - if (type === 'function') { - return <> - {(key || !key && key === 0) && <>{key}:} - {node.name}() - - } - - if (type === 'promise') { - return <> - {(key || !key && key === 0) && <>{key}:} - Promise - - } - - if (type === 'array') { - value = node.length; - open = <>Array{'['}; - close = ']'; - } else if (type === 'object') { - const keys = Object.keys(node); - if (keys.length === 0) { - value = 'Empty'; - } else if (keys.length > 3) { - value = '…'; - } else { - value = keys.slice(0, 3).join(','); - } - open = '{'; - close = '}'; - }; - - return <> - {key && <>{key}: } - {open && {open}} - - {value} - {close && {close}} - - ; -}; - -const Details = ({ node, key, children, class: className }) => { - const type = getType(node); - const props = {}; - - if (type === 'array' || type === 'object') { - props['data-char'] = type === 'array' ? ']' : '}' - props.open = !key && type === 'object' ? '' : undefined; - } - - return ( -
- - {children} -
- ); -} - -const Summary = ({ node, key, class: className }) => { - return ( - {getSummary(node, key, className)} - ); -} - -const Empty = Symbol('Empty'); - -const KeyValue = ({ key, value, dim, class: className }) => { - let type = key === '__proto__' ? 'prototype' : getType(value); - if (type === 'null') { - value = 'null'; - } else if (type === 'undefined') { - value = 'undefined'; - } else if (value === Empty) { - type = 'empty'; - value = 'Empty'; - } else { - value = JSON.stringify(value); - } - - return ( -
- {(key || !key && key === 0) && <>{key}:} - - {value} - -
- ) -} - -const Node = ({ node, key, class: className, ...props }) => { - const type = getType(node); - className = className.replace(/debug-value/g, ''); - - if (type === 'array' || type === 'object') { - let children = []; - if (type === 'array' && node.length > 0 && Object.entries(node).length === 0) { - children = Array.from({ length: node.length }, (_, key) => ); - } else { - children = Object.entries(node).map(([key, value]) => ); - } - return ( -
- ); - } else if (type === 'function') { - return ( -
- - - - }/> - ); - } else if (type === 'promise') { - return ( -
- } /> - ); - } - - return ; -} ---
@@ -146,21 +8,32 @@ const Node = ({ node, key, class: className, ...props }) => {

Debug "{key}"

-
- -
+
{JSON.stringify(value, null, 2)}
- diff --git a/packages/astro/components/index.js b/packages/astro/components/index.js index 625448249..de48280ec 100644 --- a/packages/astro/components/index.js +++ b/packages/astro/components/index.js @@ -1,4 +1,4 @@ //export { default as Code } from './Code.astro'; -//export { default as Debug } from './Debug.astro'; +export { default as Debug } from './Debug.astro'; export { default as Markdown } from './Markdown.astro'; -export { default as Prism } from './Prism.astro'; \ No newline at end of file +export { default as Prism } from './Prism.astro';