main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | import { pathOr } from 'ramda';
|
---|
2 | import { isFunction, noop } from 'ramda-adjunct';
|
---|
3 | import { visit, PredicateVisitor } from "./visitor.mjs";
|
---|
4 | import { isElement } from "../predicates/index.mjs";
|
---|
5 | export class CallbackVisitor extends PredicateVisitor {
|
---|
6 | callback;
|
---|
7 | constructor({
|
---|
8 | callback = noop,
|
---|
9 | ...rest
|
---|
10 | } = {}) {
|
---|
11 | super({
|
---|
12 | ...rest
|
---|
13 | });
|
---|
14 | this.callback = callback;
|
---|
15 | }
|
---|
16 | enter(element) {
|
---|
17 | if (this.predicate(element)) {
|
---|
18 | this.callback(element);
|
---|
19 | return this.returnOnTrue;
|
---|
20 | }
|
---|
21 | return this.returnOnFalse;
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | // executes the callback on this element and all descendants
|
---|
26 | const traverse = (options, element) => {
|
---|
27 | let callback;
|
---|
28 | let predicate;
|
---|
29 | if (isFunction(options)) {
|
---|
30 | callback = options;
|
---|
31 | predicate = isElement;
|
---|
32 | } else {
|
---|
33 | callback = pathOr(noop, ['callback'], options);
|
---|
34 | predicate = pathOr(isElement, ['predicate'], options);
|
---|
35 | }
|
---|
36 | const visitor = new CallbackVisitor({
|
---|
37 | callback,
|
---|
38 | predicate
|
---|
39 | });
|
---|
40 |
|
---|
41 | // @ts-ignore
|
---|
42 | visit(element, visitor);
|
---|
43 | };
|
---|
44 | export default traverse; |
---|
Note:
See
TracBrowser
for help on using the repository browser.