Skip to content

Commit

Permalink
Merge pull request #15 from riexn/feature-forEach
Browse files Browse the repository at this point in the history
added forEach functionality
  • Loading branch information
riexn authored Nov 20, 2020
2 parents b3a5194 + 32b83c3 commit 1e5b714
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ tree.moveToSibling({
tree.flatten();
```

### Loop across every node in the tree

```js
tree.forEach((node) => {console.log(node)})
```

# Credits

Heavily inspired by [tree-model](https://www.npmjs.com/package/tree-model)
8 changes: 8 additions & 0 deletions src/TreeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TreeModel,
MoveToSiblingProps,
MoveUnderParentProps,
ForEachFunction,
} from './types';
import { clamp } from './utils';
import treeHandler from './treeHandler';
Expand Down Expand Up @@ -203,4 +204,11 @@ export class TreeNode<T extends TreeModel> {
}
return this.parent.children.indexOf(this);
}

public forEach(func: ForEachFunction<T>): void {
func(this);
this.children.map((child) => {
child.forEach(func);
});
}
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface PredicateFunction<T extends TreeModel> {
(node: TreeNode<T>): boolean;
}

export interface ForEachFunction<T extends TreeModel> {
(node: TreeNode<T>): void;
}

export interface MoveUnderParentProps<T extends TreeModel> {
node: PredicateFunction<T>;
toParent: PredicateFunction<T>;
Expand Down
89 changes: 89 additions & 0 deletions test/forEach.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import treeHandler from '../src/index';

describe('forEach nodes traverser', () => {
test('loop across all nodes, and modify their properties', () => {
const dataTree: any = {
id: 'A',
children: [
{ id: 'B', children: [] },
{
id: 'C',
children: [{ id: 'K', children: [{ id: 'J', children: [] }] }],
},
,
],
};

const dataTreeLooped: any = {
id: 'A',
title: 'ABC',
children: [
{ id: 'B', title: 'ABC', children: [] },
{
id: 'C',
title: 'ABC',
children: [
{
id: 'K',
title: 'ABC',
children: [{ id: 'J', title: 'ABC', children: [] }],
},
],
},
,
],
};

const tree = treeHandler.parse(dataTree);
tree.forEach((node) => {
node.model = { ...node.model, title: 'ABC' };
});

expect(tree.model).toStrictEqual(dataTreeLooped);
});

test('loop across all nodes, and modify their properties based on a condition', () => {
const dataTree: any = {
id: 'A',
children: [
{ id: 'B', children: [] },
{
id: 'C',
children: [{ id: 'K', children: [{ id: 'J', children: [] }] }],
},
,
],
};

const dataTreeLooped: any = {
id: 'A',
title: 'ABC',
children: [
{ id: 'B', title: 'ABC', children: [] },
{
id: 'C',
title: 'ABC',
children: [
{
id: 'K',
title: 'This is K',
children: [{ id: 'J', title: 'ABC', children: [] }],
},
],
},
,
],
};

const tree = treeHandler.parse(dataTree);
tree.forEach((node) => {
if (node.model.id === 'K') {
node.model = { title: 'This is K' };
} else {
node.model = { title: 'ABC' };
}
});

expect(tree.model).toStrictEqual(dataTreeLooped);
});
});

0 comments on commit 1e5b714

Please sign in to comment.