/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// MAIN //

/**
* Applies a regular expression filter to a provide package tree.
*
* @private
* @param {ObjectArray} tree - package tree to filter
* @param {RegExp} filter - filter to apply
* @returns {(ObjectArray|null)} filtered tree
*/
function filterTree( tree, filter ) {
	var node;
	var pkg;
	var out;
	var tmp;
	var o;
	var i;

	out = [];
	for ( i = 0; i < tree.length; i++ ) {
		node = tree[ i ];

		// Check if the current package satisfies the filter...
		if ( filter.test( node.name ) ) {
			// We found a match! We don't need to recurse any further...
			out.push( node );
			continue;
		}
		// Check if the current package is a namespace (i.e., has children), and, if so, we need to continue descending down the tree to see if any child nodes satisfy the filter...
		if ( node.children ) {
			tmp = filterTree( node.children, filter );

			// If we were able to resolve packages satisfying the filter, we need to copy the current (pruned) node in order to avoid mutation (we're modifying the `children` property)...
			if ( tmp ) {
				o = {
					'key': node.key,
					'name': node.name,
					'children': tmp
				}
				out.push( o );
				continue;
			}
		}
	}
	if ( out.length === 0 ) {
		return null;
	}
	return out;
}


// EXPORTS //

export default filterTree;

@@ -801,16 +801,105 @@
 o apply%0A
+* @param %7BArrayLikeObject%7D %5Bout%5D - output array for storing the list of matched packages%0A
 * @retur
@@ -972,20 +972,39 @@
 , filter
- ) %7B
+, out ) %7B%0A%09var matches;
 %0A%09var no
@@ -1021,18 +1021,8 @@
 kg;%0A
-%09var out;%0A
 %09var
@@ -1045,19 +1045,23 @@
 ar i;%0A%0A%09
-out
+matches
  = %5B%5D;%0A%09
@@ -1277,16 +1277,58 @@
 ther...%0A
+%09%09%09matches.push( node );%0A%09%09%09if ( out ) %7B%0A%09
 %09%09%09out.p
@@ -1332,27 +1332,37 @@
 t.push( node
- );
+.name );%0A%09%09%09%7D
 %0A%09%09%09continue
@@ -1610,16 +1610,21 @@
 , filter
+, out
  );%0A%0A%09%09%09
@@ -1906,22 +1906,77 @@
 %09%09%09%09
-out.push( o );
+matches.push( o );%0A%09%09%09%09if ( out ) %7B%0A%09%09%09%09%09out.push( node.name );%0A%09%09%09%09%7D
 %0A%09%09%09
@@ -2004,19 +2004,23 @@
 %7D%0A%09if ( 
-out
+matches
 .length 
@@ -2055,19 +2055,23 @@
 %09return 
-out
+matches
 ;%0A%7D%0A%0A%0A//

Add support for capturing the list of matches during filtering