UNPKG

4.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.findAll = exports.existsOne = exports.findOne = exports.findOneChild = exports.find = exports.filter = void 0;
4var domhandler_1 = require("domhandler");
5/**
6 * Search a node and its children for nodes passing a test function.
7 *
8 * @param test Function to test nodes on.
9 * @param node Node to search. Will be included in the result set if it matches.
10 * @param recurse Also consider child nodes.
11 * @param limit Maximum number of nodes to return.
12 * @returns All nodes passing `test`.
13 */
14function filter(test, node, recurse, limit) {
15 if (recurse === void 0) { recurse = true; }
16 if (limit === void 0) { limit = Infinity; }
17 if (!Array.isArray(node))
18 node = [node];
19 return find(test, node, recurse, limit);
20}
21exports.filter = filter;
22/**
23 * Search an array of node and its children for nodes passing a test function.
24 *
25 * @param test Function to test nodes on.
26 * @param nodes Array of nodes to search.
27 * @param recurse Also consider child nodes.
28 * @param limit Maximum number of nodes to return.
29 * @returns All nodes passing `test`.
30 */
31function find(test, nodes, recurse, limit) {
32 var result = [];
33 for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
34 var elem = nodes_1[_i];
35 if (test(elem)) {
36 result.push(elem);
37 if (--limit <= 0)
38 break;
39 }
40 if (recurse && (0, domhandler_1.hasChildren)(elem) && elem.children.length > 0) {
41 var children = find(test, elem.children, recurse, limit);
42 result.push.apply(result, children);
43 limit -= children.length;
44 if (limit <= 0)
45 break;
46 }
47 }
48 return result;
49}
50exports.find = find;
51/**
52 * Finds the first element inside of an array that matches a test function.
53 *
54 * @param test Function to test nodes on.
55 * @param nodes Array of nodes to search.
56 * @returns The first node in the array that passes `test`.
57 */
58function findOneChild(test, nodes) {
59 return nodes.find(test);
60}
61exports.findOneChild = findOneChild;
62/**
63 * Finds one element in a tree that passes a test.
64 *
65 * @param test Function to test nodes on.
66 * @param nodes Array of nodes to search.
67 * @param recurse Also consider child nodes.
68 * @returns The first child node that passes `test`.
69 */
70function findOne(test, nodes, recurse) {
71 if (recurse === void 0) { recurse = true; }
72 var elem = null;
73 for (var i = 0; i < nodes.length && !elem; i++) {
74 var checked = nodes[i];
75 if (!(0, domhandler_1.isTag)(checked)) {
76 continue;
77 }
78 else if (test(checked)) {
79 elem = checked;
80 }
81 else if (recurse && checked.children.length > 0) {
82 elem = findOne(test, checked.children);
83 }
84 }
85 return elem;
86}
87exports.findOne = findOne;
88/**
89 * @param test Function to test nodes on.
90 * @param nodes Array of nodes to search.
91 * @returns Whether a tree of nodes contains at least one node passing a test.
92 */
93function existsOne(test, nodes) {
94 return nodes.some(function (checked) {
95 return (0, domhandler_1.isTag)(checked) &&
96 (test(checked) ||
97 (checked.children.length > 0 &&
98 existsOne(test, checked.children)));
99 });
100}
101exports.existsOne = existsOne;
102/**
103 * Search and array of nodes and its children for nodes passing a test function.
104 *
105 * Same as `find`, only with less options, leading to reduced complexity.
106 *
107 * @param test Function to test nodes on.
108 * @param nodes Array of nodes to search.
109 * @returns All nodes passing `test`.
110 */
111function findAll(test, nodes) {
112 var _a;
113 var result = [];
114 var stack = nodes.filter(domhandler_1.isTag);
115 var elem;
116 while ((elem = stack.shift())) {
117 var children = (_a = elem.children) === null || _a === void 0 ? void 0 : _a.filter(domhandler_1.isTag);
118 if (children && children.length > 0) {
119 stack.unshift.apply(stack, children);
120 }
121 if (test(elem))
122 result.push(elem);
123 }
124 return result;
125}
126exports.findAll = findAll;