-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
30 lines (23 loc) · 868 Bytes
/
index.js
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
class Cash {
length = 0;
constructor(selector, context) {
selector = typeof selector === "string" ? selector.trim() : "";
if (selector.length === 0) return;
selector = this.selector = typeof context === "string" && context.trim().length > 0 ? `${context.trim()} ${selector}` : selector;
context = this.context = context instanceof HTMLElement ? context : document;
const nodes = context.querySelectorAll(selector);
this.length = nodes.length;
this.forEach = nodes.forEach;
for (const [index, node] of nodes.entries()) {
this[index] = node;
}
}
}
/**
* @param {string} selector Any valid CSS selector.
* @param {string|HTMLElement} context Optional search context.
* @returns {Cash} An instance of the Cash class
*/
export default function CashCash(selector, context) {
return new Cash(selector, context);
}