utils.js 2.5 KB

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