-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:huyikai/tree-conver into main
- Loading branch information
Showing
32 changed files
with
741 additions
and
462 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { treeToArray } from 'tree-conver'; | ||
export const treeArray = [ | ||
{ | ||
id: '1', | ||
name: 'Node 1', | ||
list: [ | ||
{ | ||
id: '2', | ||
name: 'Node 2', | ||
list: [ | ||
{ | ||
id: '3', | ||
name: 'Node 3' | ||
} | ||
] | ||
}, | ||
{ | ||
id: '4', | ||
name: 'Node 4' | ||
} | ||
] | ||
} | ||
]; | ||
const calculateDepth = (node) => { | ||
let depth = 0; | ||
let parent = node; | ||
while (parent) { | ||
depth++; | ||
parent = | ||
parent['parentId'] && treeArray.find((n) => n.id === parent['parentId']); | ||
} | ||
return depth; | ||
}; | ||
export const options = { | ||
childrenKey: 'list', | ||
ignoreFields: [], | ||
addFields: [ | ||
{ | ||
fieldName: 'hasChildren', // Add a new 'field' property with a boolean value | ||
callback: (node) => Boolean(node['children']) | ||
}, | ||
{ | ||
fieldName: 'depth', // Add a new 'depth' property with the depth of each node | ||
callback: calculateDepth | ||
} | ||
], | ||
needParentId: true | ||
}; | ||
|
||
export const flatArray = treeToArray(treeArray, options); |
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,53 @@ | ||
<script setup lang="ts"> | ||
import { ref, computed } from 'vue'; | ||
import { arrayToTree, treeToArray } from 'tree-conver'; | ||
import '@/.vitepress/theme/styles/playground.css'; | ||
const props = withDefaults( | ||
defineProps<{ | ||
data: Array<any>; | ||
type?: 'tree' | 'array'; | ||
options?: any; | ||
}>(), | ||
{ type: 'array' } | ||
); | ||
const converMap = { | ||
tree: treeToArray, | ||
array: arrayToTree | ||
}; | ||
const inputData = computed(() => { | ||
return JSON.stringify(props.data); | ||
}); | ||
const inputOptions: any = ref(JSON.stringify(props.options)); | ||
const result = computed(() => { | ||
console.log(props.type) | ||
const tempOptions = inputOptions.value || JSON.stringify({}); | ||
try { | ||
const data = JSON.parse(inputData.value); | ||
const options = JSON.parse(tempOptions); | ||
if (Array.isArray(data)) { | ||
return converMap[props.type](data, options); | ||
} | ||
} catch { | ||
return; | ||
} | ||
return; | ||
}); | ||
</script> | ||
<template> | ||
<div class="playground-title">数据:</div> | ||
<textarea | ||
class="playground-input" | ||
v-model="inputData" | ||
/> | ||
<div class="playground-title">参数:</div> | ||
<textarea | ||
class="playground-input" | ||
v-model="inputOptions" | ||
/> | ||
<div class="playground-title">输出:</div> | ||
<pre | ||
class="playground-output" | ||
><code>{{ JSON.stringify(result, null, 2) }}</code></pre> | ||
</template> |
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,23 @@ | ||
.playground-input { | ||
padding: 10px; | ||
margin-bottom: 10px; | ||
width: 100%; | ||
height: 100px; | ||
font-size: 14px; | ||
border: 1px solid #3d3d3d; | ||
border-radius: 5px; | ||
outline: none; | ||
} | ||
|
||
.playground-title { | ||
font-weight: bold; | ||
margin: 10px 0 5px; | ||
} | ||
|
||
.playground-output { | ||
padding: 10px; | ||
font-size: 14px; | ||
background: #f5f5f5; | ||
line-height: 16px; | ||
box-sizing: border-box; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.