Prechádzať zdrojové kódy

feat: getSelectorMap()

Sv443 1 rok pred
rodič
commit
e37456c0d5
2 zmenil súbory, kde vykonal 41 pridanie a 2 odobranie
  1. 41 0
      README.md
  2. 0 2
      lib/onSelector.ts

+ 41 - 0
README.md

@@ -9,6 +9,7 @@ Contains builtin TypeScript declarations.
 - [Features](#features)
   - [onSelector()](#onselector) - call a listener once a selector is found in the DOM
   - [initOnSelector()](#initonselector) - needs to be called once to be able to use `onSelector()`
+  - [getSelectorMap()](#getselectormap) - returns all currently registered selectors, listeners and options
   - [autoPlural()](#autoplural) - automatically pluralize a string
   - [clamp()](#clamp) - clamp a number between a min and max value
   - [pauseFor()](#pausefor) - pause the execution of a function for a given amount of time
@@ -140,6 +141,46 @@ document.addEventListener("DOMContentLoaded", () => {
 
 <br>
 
+### getSelectorMap()
+Usage: `getSelectorMap(): Map<string, OnSelectorOptions[]>`  
+  
+Returns a Map of all currently registered selectors and their options, including listener function.  
+Since multiple listeners can be registered for the same selector, the value of the Map is an array of `OnSelectorOptions` objects.  
+  
+<details><summary><b>Example - click to view</b></summary>
+
+```ts
+document.addEventListener("DOMContentLoaded", initOnSelector);
+
+onSelector<HTMLDivElement>("div", {
+  listener: (elements) => void 0,
+  all: true,
+  continuous: true,
+});
+
+onSelector<HTMLDivElement>("div", {
+  listener: (elements) => void 0,
+});
+
+const selectorMap = getSelectorMap();
+// Map(1) {
+//   "div" => [
+//     {
+//       listener: (elements) => void 0,
+//       all: true,
+//       continuous: true,
+//     },
+//     {
+//       listener: (elements) => void 0,
+//     },
+//   ]
+// }
+```
+
+</details>
+
+<br>
+
 ### autoPlural()
 Usage: `autoPlural(str: string, num: number | Array | NodeList): string`  
   

+ 0 - 2
lib/onSelector.ts

@@ -52,8 +52,6 @@ function checkSelectorExists<TElem extends Element = HTMLElement>(selector: stri
     }
   });
 
-  console.info("##-- opts", options, "\n##-- deleteIndices", deleteIndices, "\n##-- selectorMap", selectorMap.size, selectorMap);
-
   if(deleteIndices.length > 0) {
     const newOptsArray = options.filter((_, i) => !deleteIndices.includes(i));
     if(newOptsArray.length === 0)