-
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.
* Update tsconfig.json * init * created sample dynamic types * parsing with a custom children property for the model is working * modified the build CI to only activate with pull requests * attempted to filter the array * working filter function, will likely need to regenerate the trees after filtering them. Suggestion: take he model, filter it, then generate the tree from there * two filtering modes working successfully * fixed the typing, and added a test for when the first node is removed under "remove children" mode
- Loading branch information
Showing
5 changed files
with
253 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import treeHandler from '../src/treeHandler'; | ||
|
||
const dataTree = { | ||
id: '1', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ id: '2', tag: 'pending', subtasks: [] }, | ||
{ | ||
id: '3', | ||
tag: 'in progress', | ||
subtasks: [ | ||
{ id: '4', tag: 'pending', subtasks: [] }, | ||
{ id: '5', tag: 'pending', subtasks: [] }, | ||
], | ||
}, | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
], | ||
}; | ||
|
||
const tree = treeHandler.parse(dataTree, { childrenProperty: 'subtasks' }); | ||
const newTree = tree.filter((node) => { | ||
return node.model.tag === 'pending'; | ||
}); | ||
console.log('new tree', newTree); |
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,163 @@ | ||
import treeHandler from '../src/index'; | ||
|
||
describe('Filter', () => { | ||
test('Merge the children to their parent position', () => { | ||
const dataTree = { | ||
id: '1', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ id: '2', tag: 'pending', subtasks: [] }, | ||
{ | ||
id: '3', | ||
tag: 'in progress', | ||
subtasks: [ | ||
{ | ||
id: '4', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ | ||
id: '6', | ||
tag: 'complete', | ||
subtasks: [ | ||
{ | ||
id: '10', | ||
tag: 'pending', | ||
subtasks: [{ id: '10', tag: 'complete', subtasks: [] }], | ||
}, | ||
], | ||
}, | ||
{ id: '7', tag: 'complete', subtasks: [] }, | ||
], | ||
}, | ||
{ id: '5', tag: 'pending', subtasks: [] }, | ||
], | ||
}, | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
], | ||
}; | ||
|
||
const treeResult = { | ||
id: '1', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ id: '2', tag: 'pending', subtasks: [] }, | ||
{ | ||
id: '4', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ | ||
id: '6', | ||
tag: 'complete', | ||
subtasks: [ | ||
{ | ||
id: '10', | ||
tag: 'pending', | ||
subtasks: [{ id: '10', tag: 'complete', subtasks: [] }], | ||
}, | ||
], | ||
}, | ||
{ id: '7', tag: 'complete', subtasks: [] }, | ||
], | ||
}, | ||
{ id: '5', tag: 'pending', subtasks: [] }, | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
], | ||
}; | ||
|
||
const tree = treeHandler.parse(dataTree, { childrenProperty: 'subtasks' }); | ||
const newTree = tree.filter( | ||
(node) => node.tag !== 'in progress', | ||
'mergeChildren' | ||
); | ||
expect(treeResult).toStrictEqual(newTree[0]?.model); | ||
}); | ||
|
||
test('Remove children (default mode)', () => { | ||
const dataTree = { | ||
id: '1', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ | ||
id: '2', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
{ id: '46', tag: 'in progress', subtasks: [] }, | ||
], | ||
}, | ||
{ | ||
id: '3', | ||
tag: 'in progress', | ||
subtasks: [ | ||
{ | ||
id: '4', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ | ||
id: '6', | ||
tag: 'complete', | ||
subtasks: [ | ||
{ | ||
id: '10', | ||
tag: 'pending', | ||
subtasks: [{ id: '11', tag: 'complete', subtasks: [] }], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: '7', | ||
tag: 'complete', | ||
subtasks: [{ id: '74', tag: 'in progress', subtasks: [] }], | ||
}, | ||
], | ||
}, | ||
{ id: '5', tag: 'pending', subtasks: [] }, | ||
], | ||
}, | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
], | ||
}; | ||
|
||
const treeResult = { | ||
id: '1', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ | ||
id: '2', | ||
tag: 'pending', | ||
subtasks: [{ id: '4', tag: 'complete', subtasks: [] }], | ||
}, | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
], | ||
}; | ||
|
||
const tree = treeHandler.parse(dataTree, { | ||
childrenProperty: 'subtasks', | ||
}); | ||
const newTree = tree.filter((node) => node.tag !== 'in progress'); | ||
expect(treeResult).toStrictEqual(newTree?.model); | ||
}); | ||
|
||
test('filter out root', () => { | ||
const dataTree = { | ||
id: '1', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ | ||
id: '2', | ||
tag: 'pending', | ||
subtasks: [ | ||
{ id: '4', tag: 'complete', subtasks: [] }, | ||
{ id: '46', tag: 'in progress', subtasks: [] }, | ||
], | ||
}, | ||
], | ||
}; | ||
const tree = treeHandler.parse(dataTree, { | ||
childrenProperty: 'subtasks', | ||
}); | ||
|
||
const newTree = tree.filter((node) => node.tag !== 'pending'); | ||
expect(newTree).toBe(undefined); | ||
}); | ||
}); |
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