utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isHtml = exports.cloneDom = exports.domEach = exports.cssCase = exports.camelCase = exports.isCheerio = exports.isTag = void 0;
  4. var htmlparser2_1 = require("htmlparser2");
  5. var domhandler_1 = require("domhandler");
  6. /**
  7. * Check if the DOM element is a tag.
  8. *
  9. * `isTag(type)` includes `<script>` and `<style>` tags.
  10. *
  11. * @private
  12. * @category Utils
  13. * @param type - DOM node to check.
  14. * @returns Whether the node is a tag.
  15. */
  16. exports.isTag = htmlparser2_1.DomUtils.isTag;
  17. /**
  18. * Checks if an object is a Cheerio instance.
  19. *
  20. * @category Utils
  21. * @param maybeCheerio - The object to check.
  22. * @returns Whether the object is a Cheerio instance.
  23. */
  24. function isCheerio(maybeCheerio) {
  25. return maybeCheerio.cheerio != null;
  26. }
  27. exports.isCheerio = isCheerio;
  28. /**
  29. * Convert a string to camel case notation.
  30. *
  31. * @private
  32. * @category Utils
  33. * @param str - String to be converted.
  34. * @returns String in camel case notation.
  35. */
  36. function camelCase(str) {
  37. return str.replace(/[_.-](\w|$)/g, function (_, x) { return x.toUpperCase(); });
  38. }
  39. exports.camelCase = camelCase;
  40. /**
  41. * Convert a string from camel case to "CSS case", where word boundaries are
  42. * described by hyphens ("-") and all characters are lower-case.
  43. *
  44. * @private
  45. * @category Utils
  46. * @param str - String to be converted.
  47. * @returns String in "CSS case".
  48. */
  49. function cssCase(str) {
  50. return str.replace(/[A-Z]/g, '-$&').toLowerCase();
  51. }
  52. exports.cssCase = cssCase;
  53. /**
  54. * Iterate over each DOM element without creating intermediary Cheerio instances.
  55. *
  56. * This is indented for use internally to avoid otherwise unnecessary memory
  57. * pressure introduced by _make.
  58. *
  59. * @category Utils
  60. * @param array - Array to iterate over.
  61. * @param fn - Function to call.
  62. * @returns The original instance.
  63. */
  64. function domEach(array, fn) {
  65. var len = array.length;
  66. for (var i = 0; i < len; i++)
  67. fn(array[i], i);
  68. return array;
  69. }
  70. exports.domEach = domEach;
  71. /**
  72. * Create a deep copy of the given DOM structure. Sets the parents of the copies
  73. * of the passed nodes to `null`.
  74. *
  75. * @private
  76. * @category Utils
  77. * @param dom - The htmlparser2-compliant DOM structure.
  78. * @returns - The cloned DOM.
  79. */
  80. function cloneDom(dom) {
  81. var clone = 'length' in dom
  82. ? Array.prototype.map.call(dom, function (el) { return domhandler_1.cloneNode(el, true); })
  83. : [domhandler_1.cloneNode(dom, true)];
  84. // Add a root node around the cloned nodes
  85. var root = new domhandler_1.Document(clone);
  86. clone.forEach(function (node) {
  87. node.parent = root;
  88. });
  89. return clone;
  90. }
  91. exports.cloneDom = cloneDom;
  92. /**
  93. * A simple way to check for HTML strings. Tests for a `<` within a string,
  94. * immediate followed by a letter and eventually followed by a `>`.
  95. *
  96. * @private
  97. */
  98. var quickExpr = /<[a-zA-Z][^]*>/;
  99. /**
  100. * Check if string is HTML.
  101. *
  102. * @private
  103. * @category Utils
  104. * @param str - String to check.
  105. * @returns Indicates if `str` is HTML.
  106. */
  107. function isHtml(str) {
  108. // Run the regex
  109. return quickExpr.test(str);
  110. }
  111. exports.isHtml = isHtml;