Skip to content

Commit

Permalink
refactor: collections
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Sep 23, 2024
1 parent b10ebe3 commit 81a2ead
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 46 deletions.
8 changes: 8 additions & 0 deletions packages/utilities/collection/src/list-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ export class ListCollection<T extends CollectionItem = CollectionItem> {

this.items.splice(toIndex, 0, removed)
}

json() {
return {
size: this.size,
first: this.firstValue,
last: this.lastValue,
}
}
}

const match = (label: string | null, query: string) => {
Expand Down
13 changes: 13 additions & 0 deletions packages/utilities/collection/src/tree-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ export class TreeNode {
parentNode?.insertAfter(target.value, node)
}

json(): any {
const json: any = {
value: this.value,
}
if (this.hasChildNodes()) {
json.children = this.children.map((child) => child.json())
}
if (this.parentNode) {
json.parent = this.parentNode.value
}
return json
}

walk(options: TreeWalkerOptions = {}): TreeWalker {
return new TreeWalker(this, options)
}
Expand Down
241 changes: 196 additions & 45 deletions packages/utilities/collection/tests/tree-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,62 @@ describe("tree collection", () => {
rootNode.insertChild(new TreeNode({ value: "child3" }), "branch1")

expect(rootNode).toMatchInlineSnapshot(`
{
TreeNode {
"children": [
{
TreeNode {
"children": [
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child3",
},
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-1",
},
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-2",
},
],
"parent": "root",
"data": undefined,
"expanded": true,
"parentNode": [Circular],
"selected": false,
"value": "branch1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child2",
},
],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
}
`)
Expand All @@ -80,26 +107,59 @@ describe("tree collection", () => {

expect(rootNode.children).toMatchInlineSnapshot(`
[
{
TreeNode {
"children": [
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-1",
},
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1",
},
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-2",
},
],
"parent": "root",
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "branch1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "child2",
},
]
Expand All @@ -111,26 +171,73 @@ describe("tree collection", () => {

expect(rootNode.children).toMatchInlineSnapshot(`
[
{
TreeNode {
"children": [
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-2",
},
],
"parent": "root",
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "branch1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "child1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "child1-1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "child2",
},
]
Expand All @@ -142,30 +249,74 @@ describe("tree collection", () => {

expect(rootNode.children).toMatchInlineSnapshot(`
[
{
TreeNode {
"children": [
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-1",
},
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-2",
},
{
"parent": "branch1",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": [Circular],
"selected": false,
"value": "child1-3",
},
],
"parent": "root",
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "branch1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "child1",
},
{
"parent": "root",
TreeNode {
"children": [],
"data": undefined,
"expanded": false,
"parentNode": TreeNode {
"children": [Circular],
"data": undefined,
"expanded": false,
"parentNode": null,
"selected": false,
"value": "root",
},
"selected": false,
"value": "child2",
},
]
Expand Down
6 changes: 5 additions & 1 deletion packages/utilities/stringify-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function stringifyState(state: Dict, omit?: string[]) {
return value.toString()
}

if (value?.toJSON) {
if (typeof value?.json === "function") {
return value.json()
}

if (typeof value?.toJSON === "function") {
return value.toJSON()
}

Expand Down

0 comments on commit 81a2ead

Please sign in to comment.