-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export interface JsonTreeTheme { | ||
node: { | ||
label: string; | ||
value: string; | ||
count: string; | ||
}; | ||
} | ||
|
||
const baseTheme: JsonTreeTheme = { | ||
node: { | ||
label: 'font-mono opacity-70', | ||
value: '', | ||
count: '' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './JsonTree'; | ||
export * from './JsonTreeNode'; | ||
export * from './JsonTreeTheme'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
function getDataType(data: any) { | ||
if (data === null || data === undefined) { | ||
return 'nil'; | ||
} | ||
|
||
if (data instanceof Date) { | ||
return 'date'; | ||
} | ||
|
||
if (Array.isArray(data)) { | ||
return 'array'; | ||
} | ||
|
||
if (data != null && data.constructor.name === 'Object') { | ||
return 'object'; | ||
} | ||
|
||
if (typeof data === 'string') { | ||
return 'string'; | ||
} | ||
|
||
if (typeof data === 'number') { | ||
return 'number'; | ||
} | ||
|
||
if (typeof data === 'boolean') { | ||
return 'boolean'; | ||
} | ||
|
||
return 'unknown'; | ||
} | ||
|
||
export interface ParseJsonInputs { | ||
data: any; | ||
id?: string; | ||
label?: string; | ||
index?: number; | ||
showEmpty?: boolean; | ||
} | ||
|
||
export interface JsonTreeData { | ||
type: string; | ||
id: string; | ||
data: any; | ||
label: string; | ||
index?: number; | ||
} | ||
|
||
export function parseJsonTree({ | ||
id = 'root', | ||
data, | ||
index, | ||
label, | ||
showEmpty = true | ||
}: ParseJsonInputs): JsonTreeData { | ||
const type = getDataType(data); | ||
|
||
if (type === 'object') { | ||
const keys = Object.keys(data); | ||
const result = keys.reduce((parsedItems, key, idx) => { | ||
const value = data[key]; | ||
const childParsed = parseJsonTree({ | ||
data: value, | ||
id: `${id}.${key}`, | ||
index: idx, | ||
label: key | ||
}); | ||
if (showEmpty || (!showEmpty && childParsed !== null)) { | ||
parsedItems.push(childParsed); | ||
} | ||
return parsedItems; | ||
}, []); | ||
|
||
return { | ||
type, | ||
id, | ||
data: result, | ||
label: index !== undefined ? `${index}` : 'root', | ||
index | ||
}; | ||
} else if (type === 'array') { | ||
const result = data.map((item, idx) => | ||
parseJsonTree({ data: item, id: `${id}[${idx}]`, index: idx }) | ||
); | ||
|
||
return { | ||
type, | ||
id, | ||
data: result, | ||
label, | ||
index | ||
}; | ||
} else { | ||
return { | ||
type, | ||
id, | ||
data, | ||
label, | ||
index | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './Tree'; | ||
export * from './TreeNode'; | ||
export * from './TreeTheme'; | ||
export * from './JsonTree'; |