querying.js 4.3 KB

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