|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.9 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const TREE = Symbol();
|
|---|
| 4 | const ROOT = Symbol();
|
|---|
| 5 | const NEXT = Symbol();
|
|---|
| 6 | const ITERATE_FUNC = Symbol();
|
|---|
| 7 |
|
|---|
| 8 | class TreeIterator {
|
|---|
| 9 | constructor(tree, root, firstResult, iterateFunction) {
|
|---|
| 10 | this[TREE] = tree;
|
|---|
| 11 | this[ROOT] = root;
|
|---|
| 12 | this[NEXT] = firstResult;
|
|---|
| 13 | this[ITERATE_FUNC] = iterateFunction;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | next() {
|
|---|
| 17 | const tree = this[TREE];
|
|---|
| 18 | const iterateFunc = this[ITERATE_FUNC];
|
|---|
| 19 | const root = this[ROOT];
|
|---|
| 20 |
|
|---|
| 21 | if (!this[NEXT]) {
|
|---|
| 22 | return {
|
|---|
| 23 | done: true,
|
|---|
| 24 | value: root,
|
|---|
| 25 | };
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | const value = this[NEXT];
|
|---|
| 29 |
|
|---|
| 30 | if (iterateFunc === 1) {
|
|---|
| 31 | this[NEXT] = tree._node(value).previousSibling;
|
|---|
| 32 | }
|
|---|
| 33 | else if (iterateFunc === 2) {
|
|---|
| 34 | this[NEXT] = tree._node(value).nextSibling;
|
|---|
| 35 | }
|
|---|
| 36 | else if (iterateFunc === 3) {
|
|---|
| 37 | this[NEXT] = tree._node(value).parent;
|
|---|
| 38 | }
|
|---|
| 39 | else if (iterateFunc === 4) {
|
|---|
| 40 | this[NEXT] = tree.preceding(value, {root: root});
|
|---|
| 41 | }
|
|---|
| 42 | else /* if (iterateFunc === 5)*/ {
|
|---|
| 43 | this[NEXT] = tree.following(value, {root: root});
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | return {
|
|---|
| 47 | done: false,
|
|---|
| 48 | value: value,
|
|---|
| 49 | };
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | Object.defineProperty(TreeIterator.prototype, Symbol.iterator, {
|
|---|
| 54 | value: function() {
|
|---|
| 55 | return this;
|
|---|
| 56 | },
|
|---|
| 57 | writable: false,
|
|---|
| 58 | });
|
|---|
| 59 |
|
|---|
| 60 | TreeIterator.PREV = 1;
|
|---|
| 61 | TreeIterator.NEXT = 2;
|
|---|
| 62 | TreeIterator.PARENT = 3;
|
|---|
| 63 | TreeIterator.PRECEDING = 4;
|
|---|
| 64 | TreeIterator.FOLLOWING = 5;
|
|---|
| 65 |
|
|---|
| 66 | Object.freeze(TreeIterator);
|
|---|
| 67 | Object.freeze(TreeIterator.prototype);
|
|---|
| 68 |
|
|---|
| 69 | module.exports = TreeIterator;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.