-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeftWalker.ts
42 lines (35 loc) · 1.07 KB
/
LeftWalker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @module js-hierarchy
*/
/**
*/
import {Walker} from './Walker';
import {ActionFunction} from '../ActionFunction';
import {Node} from '../Node';
import * as loglevel from 'loglevel';
import Bluebird = require('bluebird');
/**
* A walker, that runs all parents children from the current
* child to the left.
*/
export class LeftWalker implements Walker {
public walk(node: Node, action: ActionFunction): Bluebird<void> {
let log = loglevel.getLogger('js-hierarchy:LeftWalker');
let parent: Node = node.getParent();
let nodes: Array<Node> = [];
for (let i = parent.findChild(node) - 1; i >= 0; i--) {
nodes.push(parent.getChildren()[i]);
}
return Bluebird.reduce(
nodes,
(total, current) => {
if (log.getLevel() === log.levels.TRACE) {
log.trace(`Calling action on child: ${current.toJSON()}`);
}
return action(current)
.thenReturn([]);
},
[]
).thenReturn();
}
}