-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils.ts
129 lines (116 loc) · 3.26 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { type AnyNode, cloneNode, Document } from 'domhandler';
import type { Cheerio } from './cheerio.js';
/**
* Check if the DOM element is a tag.
*
* `isTag(type)` includes `<script>` and `<style>` tags.
*
* @private
* @category Utils
* @param type - The DOM node to check.
* @returns Whether the node is a tag.
*/
export { isTag } from 'domhandler';
/**
* Checks if an object is a Cheerio instance.
*
* @category Utils
* @param maybeCheerio - The object to check.
* @returns Whether the object is a Cheerio instance.
*/
export function isCheerio<T>(maybeCheerio: any): maybeCheerio is Cheerio<T> {
return maybeCheerio.cheerio != null;
}
/**
* Convert a string to camel case notation.
*
* @private
* @category Utils
* @param str - The string to be converted.
* @returns String in camel case notation.
*/
export function camelCase(str: string): string {
return str.replace(/[_.-](\w|$)/g, (_, x) => x.toUpperCase());
}
/**
* Convert a string from camel case to "CSS case", where word boundaries are
* described by hyphens ("-") and all characters are lower-case.
*
* @private
* @category Utils
* @param str - The string to be converted.
* @returns String in "CSS case".
*/
export function cssCase(str: string): string {
return str.replace(/[A-Z]/g, '-$&').toLowerCase();
}
/**
* Iterate over each DOM element without creating intermediary Cheerio
* instances.
*
* This is indented for use internally to avoid otherwise unnecessary memory
* pressure introduced by _make.
*
* @category Utils
* @param array - The array to iterate over.
* @param fn - Function to call.
* @returns The original instance.
*/
export function domEach<
T extends AnyNode,
Arr extends ArrayLike<T> = Cheerio<T>
>(array: Arr, fn: (elem: T, index: number) => void): Arr {
const len = array.length;
for (let i = 0; i < len; i++) fn(array[i], i);
return array;
}
/**
* Create a deep copy of the given DOM structure. Sets the parents of the copies
* of the passed nodes to `null`.
*
* @private
* @category Utils
* @param dom - The domhandler-compliant DOM structure.
* @returns - The cloned DOM.
*/
export function cloneDom<T extends AnyNode>(dom: T | T[]): T[] {
const clone =
'length' in dom
? (Array.prototype.map.call(dom, (el) => cloneNode(el, true)) as T[])
: [cloneNode(dom, true)];
// Add a root node around the cloned nodes
const root = new Document(clone);
clone.forEach((node) => {
node.parent = root;
});
return clone;
}
const enum CharacterCodes {
LowerA = 97,
LowerZ = 122,
UpperA = 65,
UpperZ = 90,
Exclamation = 33,
}
/**
* Check if string is HTML.
*
* Tests for a `<` within a string, immediate followed by a letter and
* eventually followed by a `>`.
*
* @private
* @category Utils
* @param str - The string to check.
* @returns Indicates if `str` is HTML.
*/
export function isHtml(str: string): boolean {
const tagStart = str.indexOf('<');
if (tagStart < 0 || tagStart > str.length - 3) return false;
const tagChar = str.charCodeAt(tagStart + 1);
return (
((tagChar >= CharacterCodes.LowerA && tagChar <= CharacterCodes.LowerZ) ||
(tagChar >= CharacterCodes.UpperA && tagChar <= CharacterCodes.UpperZ) ||
tagChar === CharacterCodes.Exclamation) &&
str.includes('>', tagStart + 2)
);
}