utils.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isCheerio = isCheerio;
  4. exports.camelCase = camelCase;
  5. exports.cssCase = cssCase;
  6. exports.domEach = domEach;
  7. exports.isHtml = isHtml;
  8. /**
  9. * Checks if an object is a Cheerio instance.
  10. *
  11. * @category Utils
  12. * @param maybeCheerio - The object to check.
  13. * @returns Whether the object is a Cheerio instance.
  14. */
  15. function isCheerio(maybeCheerio) {
  16. return maybeCheerio.cheerio != null;
  17. }
  18. /**
  19. * Convert a string to camel case notation.
  20. *
  21. * @private
  22. * @category Utils
  23. * @param str - The string to be converted.
  24. * @returns String in camel case notation.
  25. */
  26. function camelCase(str) {
  27. return str.replace(/[._-](\w|$)/g, (_, x) => x.toUpperCase());
  28. }
  29. /**
  30. * Convert a string from camel case to "CSS case", where word boundaries are
  31. * described by hyphens ("-") and all characters are lower-case.
  32. *
  33. * @private
  34. * @category Utils
  35. * @param str - The string to be converted.
  36. * @returns String in "CSS case".
  37. */
  38. function cssCase(str) {
  39. return str.replace(/[A-Z]/g, '-$&').toLowerCase();
  40. }
  41. /**
  42. * Iterate over each DOM element without creating intermediary Cheerio
  43. * instances.
  44. *
  45. * This is indented for use internally to avoid otherwise unnecessary memory
  46. * pressure introduced by _make.
  47. *
  48. * @category Utils
  49. * @param array - The array to iterate over.
  50. * @param fn - Function to call.
  51. * @returns The original instance.
  52. */
  53. function domEach(array, fn) {
  54. const len = array.length;
  55. for (let i = 0; i < len; i++)
  56. fn(array[i], i);
  57. return array;
  58. }
  59. var CharacterCodes;
  60. (function (CharacterCodes) {
  61. CharacterCodes[CharacterCodes["LowerA"] = 97] = "LowerA";
  62. CharacterCodes[CharacterCodes["LowerZ"] = 122] = "LowerZ";
  63. CharacterCodes[CharacterCodes["UpperA"] = 65] = "UpperA";
  64. CharacterCodes[CharacterCodes["UpperZ"] = 90] = "UpperZ";
  65. CharacterCodes[CharacterCodes["Exclamation"] = 33] = "Exclamation";
  66. })(CharacterCodes || (CharacterCodes = {}));
  67. /**
  68. * Check if string is HTML.
  69. *
  70. * Tests for a `<` within a string, immediate followed by a letter and
  71. * eventually followed by a `>`.
  72. *
  73. * @private
  74. * @category Utils
  75. * @param str - The string to check.
  76. * @returns Indicates if `str` is HTML.
  77. */
  78. function isHtml(str) {
  79. const tagStart = str.indexOf('<');
  80. if (tagStart < 0 || tagStart > str.length - 3)
  81. return false;
  82. const tagChar = str.charCodeAt(tagStart + 1);
  83. return (((tagChar >= CharacterCodes.LowerA && tagChar <= CharacterCodes.LowerZ) ||
  84. (tagChar >= CharacterCodes.UpperA && tagChar <= CharacterCodes.UpperZ) ||
  85. tagChar === CharacterCodes.Exclamation) &&
  86. str.includes('>', tagStart + 2));
  87. }
  88. //# sourceMappingURL=utils.js.map