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.esm.js CHANGED
@@ -124,16 +124,42 @@ class Node {
124
124
  }
125
125
  return null;
126
126
  }
127
- find(predicate) {
128
- return this.findAll(predicate)[0] || null;
127
+ find(predicate, breadthFirst = false) {
128
+ let match = null;
129
+ if (breadthFirst) {
130
+ this.walkBreadthFirst(n => {
131
+ if (predicate(n)) {
132
+ match = n;
133
+ return false;
134
+ }
135
+ });
136
+ }
137
+ else {
138
+ this.walkUp(n => {
139
+ if (predicate(n)) {
140
+ match = n;
141
+ return false;
142
+ }
143
+ });
144
+ }
145
+ return match;
129
146
  }
130
- findAll(predicate) {
147
+ findAll(predicate, breadthFirst = false) {
131
148
  const matches = [];
132
- this.walkUp(n => {
133
- if (predicate(n)) {
134
- matches.push(n);
135
- }
136
- });
149
+ if (breadthFirst) {
150
+ this.walkBreadthFirst(n => {
151
+ if (predicate(n)) {
152
+ matches.push(n);
153
+ }
154
+ });
155
+ }
156
+ else {
157
+ this.walkUp(n => {
158
+ if (predicate(n)) {
159
+ matches.push(n);
160
+ }
161
+ });
162
+ }
137
163
  return matches;
138
164
  }
139
165
  findRoot() {
@@ -156,22 +182,26 @@ class Node {
156
182
  return null;
157
183
  }
158
184
  walkUp(callback) {
185
+ var _a;
159
186
  const childrenCopy = this._children.slice();
160
- childrenCopy.forEach(c => c.walkUp(callback));
161
- callback(this);
187
+ const result = childrenCopy.every(c => c.walkUp(callback));
188
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && result;
162
189
  }
163
190
  walkDown(callback) {
191
+ var _a;
164
192
  const childrenCopy = this._children.slice();
165
- callback(this);
166
- childrenCopy.forEach(c => c.walkDown(callback));
193
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && childrenCopy.every(c => c.walkDown(callback));
167
194
  }
168
195
  walkBreadthFirst(callback) {
169
196
  const queue = [this];
170
197
  while (queue.length > 0) {
171
198
  const current = queue.shift();
172
- callback(current);
199
+ if (callback(current) === false) {
200
+ return false;
201
+ }
173
202
  queue.push(...current.children);
174
203
  }
204
+ return true;
175
205
  }
176
206
  transform(visitors) {
177
207
  const childrenCopy = this._children.slice();