clarity-pattern-parser 11.4.2 → 11.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -128,16 +128,42 @@ class Node {
128
128
  }
129
129
  return null;
130
130
  }
131
- find(predicate) {
132
- return this.findAll(predicate)[0] || null;
131
+ find(predicate, breadthFirst = false) {
132
+ let match = null;
133
+ if (breadthFirst) {
134
+ this.walkBreadthFirst(n => {
135
+ if (predicate(n)) {
136
+ match = n;
137
+ return false;
138
+ }
139
+ });
140
+ }
141
+ else {
142
+ this.walkUp(n => {
143
+ if (predicate(n)) {
144
+ match = n;
145
+ return false;
146
+ }
147
+ });
148
+ }
149
+ return match;
133
150
  }
134
- findAll(predicate) {
151
+ findAll(predicate, breadthFirst = false) {
135
152
  const matches = [];
136
- this.walkUp(n => {
137
- if (predicate(n)) {
138
- matches.push(n);
139
- }
140
- });
153
+ if (breadthFirst) {
154
+ this.walkBreadthFirst(n => {
155
+ if (predicate(n)) {
156
+ matches.push(n);
157
+ }
158
+ });
159
+ }
160
+ else {
161
+ this.walkUp(n => {
162
+ if (predicate(n)) {
163
+ matches.push(n);
164
+ }
165
+ });
166
+ }
141
167
  return matches;
142
168
  }
143
169
  findRoot() {
@@ -160,22 +186,26 @@ class Node {
160
186
  return null;
161
187
  }
162
188
  walkUp(callback) {
189
+ var _a;
163
190
  const childrenCopy = this._children.slice();
164
- childrenCopy.forEach(c => c.walkUp(callback));
165
- callback(this);
191
+ const result = childrenCopy.every(c => c.walkUp(callback));
192
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && result;
166
193
  }
167
194
  walkDown(callback) {
195
+ var _a;
168
196
  const childrenCopy = this._children.slice();
169
- callback(this);
170
- childrenCopy.forEach(c => c.walkDown(callback));
197
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && childrenCopy.every(c => c.walkDown(callback));
171
198
  }
172
199
  walkBreadthFirst(callback) {
173
200
  const queue = [this];
174
201
  while (queue.length > 0) {
175
202
  const current = queue.shift();
176
- callback(current);
203
+ if (callback(current) === false) {
204
+ return false;
205
+ }
177
206
  queue.push(...current.children);
178
207
  }
208
+ return true;
179
209
  }
180
210
  transform(visitors) {
181
211
  const childrenCopy = this._children.slice();