mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-04-19 08:51:03 +00:00
65 lines
1.4 KiB
Svelte
65 lines
1.4 KiB
Svelte
|
<script>
|
||
|
import Grid from './grid.svelte';
|
||
|
|
||
|
export let data = [];
|
||
|
export let key = '_id';
|
||
|
export let showHeaders = false;
|
||
|
export let contained = true;
|
||
|
|
||
|
console.log(data);
|
||
|
|
||
|
let items = [];
|
||
|
|
||
|
$: if (data) {
|
||
|
items = [];
|
||
|
|
||
|
if (Array.isArray(data)) {
|
||
|
for (const item of data) {
|
||
|
const _item = {};
|
||
|
_item.key = item[key];
|
||
|
_item.children = dissectObject(item);
|
||
|
items = [ ...items, _item ];
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
items = dissectObject(data);
|
||
|
console.log(items);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function getType(value) {
|
||
|
if (Array.isArray(value)) {
|
||
|
return `array (${value.length} item${value.length === 1 ? '' : 's'})`;
|
||
|
}
|
||
|
else if (new Date(value).toString() !== 'Invalid Date') {
|
||
|
return 'date';
|
||
|
}
|
||
|
else if (typeof value === 'object') {
|
||
|
const keys = Object.keys(value);
|
||
|
return `object (${keys.length} item${keys.length === 1 ? '' : 's'})`;
|
||
|
}
|
||
|
else {
|
||
|
return typeof value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function dissectObject(object) {
|
||
|
return Object.entries(object).map(([ key, value ]) => {
|
||
|
const type = getType(value);
|
||
|
const child = { key, value, type };
|
||
|
|
||
|
if (type.startsWith('object')) {
|
||
|
child.children = dissectObject(value);
|
||
|
}
|
||
|
|
||
|
return child;
|
||
|
});
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<Grid columns={[
|
||
|
{ key: 'key', label: 'Key' },
|
||
|
{ key: 'value', label: 'Value' },
|
||
|
{ key: 'type', label: 'Type' },
|
||
|
]} {items} {showHeaders} {contained} key="key" />
|