dom-primitive-bindings 1.4.1 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +86 -78
  2. package/package.json +4 -2
  3. package/src/index.js +2 -2
  4. package/src/{v3 → v5}/index.js +2 -3
  5. package/src/v5/prepare/index.js +23 -0
  6. package/src/{v4 → v5}/register/meta.js +1 -1
  7. package/src/v6/guards/actions/index.js +21 -0
  8. package/src/v6/guards/bindings/index.js +5 -0
  9. package/src/v6/guards/bindings/validateArray.js +23 -0
  10. package/src/v6/guards/bindings/validateBinding.js +22 -0
  11. package/src/{v4 → v6}/guards/index.js +8 -4
  12. package/src/{v2 → v6}/index.js +3 -4
  13. package/src/v6/prepare/index.js +23 -0
  14. package/src/{v2 → v6}/register/meta.js +1 -1
  15. package/src/v7/guards/actions/index.js +21 -0
  16. package/src/v7/guards/bindings/index.js +5 -0
  17. package/src/v7/guards/bindings/validateArray.js +23 -0
  18. package/src/v7/guards/bindings/validateBinding.js +22 -0
  19. package/src/{v3 → v7}/guards/index.js +8 -4
  20. package/src/v7/index.js +43 -0
  21. package/src/v7/prepare/index.js +23 -0
  22. package/src/{v4 → v7}/register/index.js +4 -4
  23. package/src/{v3 → v7}/register/meta.js +1 -1
  24. package/src/v7/sample/index.js +22 -0
  25. package/src/v8/dispatch/executeBinding.js +21 -0
  26. package/src/v8/dispatch/getClientData.js +8 -0
  27. package/src/v8/dispatch/getStartInput.js +10 -0
  28. package/src/v8/dispatch/index.js +15 -0
  29. package/src/v8/guards/actions/index.js +21 -0
  30. package/src/v8/guards/bindings/index.js +5 -0
  31. package/src/v8/guards/bindings/validateArray.js +23 -0
  32. package/src/v8/guards/bindings/validateBinding.js +22 -0
  33. package/src/v8/guards/container/index.js +13 -0
  34. package/src/v8/guards/container/resolveElement.js +14 -0
  35. package/src/v8/guards/index.js +19 -0
  36. package/src/v8/index.js +37 -0
  37. package/src/v8/listeners/attachListener.js +10 -0
  38. package/src/v8/listeners/index.js +12 -0
  39. package/src/v8/prepare/groupBindings.js +14 -0
  40. package/src/v8/prepare/index.js +23 -0
  41. package/src/v8/register/index.js +15 -0
  42. package/src/v8/register/meta.js +6 -0
  43. package/src/v8/sample/index.js +22 -0
  44. package/src/v2/prepare/index.js +0 -8
  45. package/src/v3/guards/bindings/index.js +0 -11
  46. package/src/v3/guards/bindings/validateArray.js +0 -10
  47. package/src/v3/prepare/index.js +0 -8
  48. package/src/v4/guards/bindings/index.js +0 -11
  49. package/src/v4/guards/bindings/validateArray.js +0 -10
  50. package/src/v4/index.js +0 -22
  51. package/src/v4/prepare/index.js +0 -8
  52. /package/src/{v2 → v5}/dispatch/executeBinding.js +0 -0
  53. /package/src/{v2 → v5}/dispatch/getClientData.js +0 -0
  54. /package/src/{v2 → v5}/dispatch/getStartInput.js +0 -0
  55. /package/src/{v2 → v5}/dispatch/index.js +0 -0
  56. /package/src/{v2 → v5}/guards/bindings/index.js +0 -0
  57. /package/src/{v2 → v5}/guards/bindings/validateArray.js +0 -0
  58. /package/src/{v2 → v5}/guards/container/index.js +0 -0
  59. /package/src/{v2 → v5}/guards/container/resolveElement.js +0 -0
  60. /package/src/{v2 → v5}/guards/index.js +0 -0
  61. /package/src/{v2 → v5}/listeners/attachListener.js +0 -0
  62. /package/src/{v2 → v5}/listeners/index.js +0 -0
  63. /package/src/{v2 → v5}/prepare/groupBindings.js +0 -0
  64. /package/src/{v2 → v5}/register/index.js +0 -0
  65. /package/src/{v3 → v6}/dispatch/executeBinding.js +0 -0
  66. /package/src/{v3 → v6}/dispatch/getClientData.js +0 -0
  67. /package/src/{v3 → v6}/dispatch/getStartInput.js +0 -0
  68. /package/src/{v3 → v6}/dispatch/index.js +0 -0
  69. /package/src/{v3 → v6}/guards/container/index.js +0 -0
  70. /package/src/{v3 → v6}/guards/container/resolveElement.js +0 -0
  71. /package/src/{v3 → v6}/listeners/attachListener.js +0 -0
  72. /package/src/{v3 → v6}/listeners/index.js +0 -0
  73. /package/src/{v3 → v6}/prepare/groupBindings.js +0 -0
  74. /package/src/{v3 → v6}/register/index.js +0 -0
  75. /package/src/{v4 → v7}/dispatch/executeBinding.js +0 -0
  76. /package/src/{v4 → v7}/dispatch/getClientData.js +0 -0
  77. /package/src/{v4 → v7}/dispatch/getStartInput.js +0 -0
  78. /package/src/{v4 → v7}/dispatch/index.js +0 -0
  79. /package/src/{v4 → v7}/guards/container/index.js +0 -0
  80. /package/src/{v4 → v7}/guards/container/resolveElement.js +0 -0
  81. /package/src/{v4 → v7}/listeners/attachListener.js +0 -0
  82. /package/src/{v4 → v7}/listeners/index.js +0 -0
  83. /package/src/{v4 → v7}/prepare/groupBindings.js +0 -0
package/README.md CHANGED
@@ -1,26 +1,31 @@
1
1
  # DOM Primitive Bindings
2
2
 
3
- > Modern, lightweight, single-listener delegated event engine for browser DOMs. Driven by declarative `bindings.json` configurations with zero DOM manipulation and zero schema extraction.
3
+ > Browser-native, single-listener container event delegation driven by declarative JSON instructions.
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/dom-primitive-bindings.svg?style=flat-square)](https://www.npmjs.com/package/dom-primitive-bindings)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT)
7
- [![Single Source of Truth](https://img.shields.io/badge/Version-v3-blue.svg?style=flat-square)]()
8
- [![Documentation](https://img.shields.io/badge/Docs-Architecture%20%26%20Stories-green.svg?style=flat-square)](./docs/index.html)
7
+ [![GitHub Pages](https://img.shields.io/badge/Docs-GitHub%20Pages-success.svg?style=flat-square)](https://keshavsoft.github.io/dom-primitive-bindings/)
9
8
 
10
9
  ---
11
10
 
12
- ## 📖 The 4-Part Event Delegation Story
13
-
14
- Explore the interactive interactive story chapters in [`docs/stories/`](./docs/index.html):
15
-
16
- 1. **[Chapter 1: The Everyday Habit & The Listener Trap](./docs/stories/01-the-listener-trap.html)**
17
- Why attaching listeners to individual elements causes memory leaks, double-trigger cascades, and dead-zones on dynamic elements.
18
- 2. **[Chapter 2: The Delegation Epiphany](./docs/stories/02-the-delegation-epiphany.html)**
19
- Harnessing browser event bubbling: 1 container listener catching bubbling events forever.
20
- 3. **[Chapter 3: The Staged Pipeline Architecture](./docs/stories/03-the-staged-pipeline.html)**
21
- Inside the decoupled 4-stage pipeline: `guards` $\to$ `prepare` $\to$ `listeners` $\to$ `dispatch`.
22
- 4. **[Chapter 4: The Joy of Declarative JSON](./docs/stories/04-declarative-json-bindings.html)**
23
- Moving from imperative JavaScript arrays to clean, serializable `bindings.json` specifications with a live interactive demo.
11
+ ## 🌐 Live Documentation & Interactive Story
12
+
13
+ Explore the complete interactive story and live samples on **[GitHub Pages](https://keshavsoft.github.io/dom-primitive-bindings/)**:
14
+
15
+ 1. **[Chapter 1: The Problem (Why?)](https://keshavsoft.github.io/dom-primitive-bindings/why/)** ([local](./docs/why/index.html))
16
+ Why attaching listeners to individual elements causes memory leaks, double-trigger cascades, and re-render dead zones.
17
+ 2. **[Chapter 2: How We Achieved It](https://keshavsoft.github.io/dom-primitive-bindings/how/)** ([local](./docs/how/index.html))
18
+ The delegation epiphany: leveraging browser event bubbling across a decoupled 4-stage pipeline (`guards` $\to$ `prepare` $\to$ `listeners` $\to$ `dispatch`).
19
+ 3. **[Chapter 3: Declarative JSON](https://keshavsoft.github.io/dom-primitive-bindings/declarative/)** ([local](./docs/declarative/index.html))
20
+ Separating intent from execution: why JSON instructions make UI code human-readable, statically guardable, and self-documenting.
21
+ 4. **[Chapter 4: The Declarative Transformation](https://keshavsoft.github.io/dom-primitive-bindings/stories/04-declarative-json-bindings.html)** ([local](./docs/stories/04-declarative-json-bindings.html))
22
+ Moving from imperative JavaScript arrays to serializable `bindings.json` specifications.
23
+ 5. **[Chapter 5: The Pure 3-File Architecture (v7)](https://keshavsoft.github.io/dom-primitive-bindings/stories/05-the-three-file-architecture.html)** ([local](./docs/stories/05-the-three-file-architecture.html))
24
+ The ultimate standard: cleanly decoupling `bindings.json` (Blueprint), `actions.js` (Behavior), and `index.js` (Wiring) at every folder level.
25
+ 6. **[Usage & Available Builds](https://keshavsoft.github.io/dom-primitive-bindings/usage/)** ([local](./docs/usage/index.html))
26
+ Available builds hosted on GitHub Pages CDN with one-click copyable script tags and ES Module imports.
27
+ 7. **[Interactive v7 Showcase](https://keshavsoft.github.io/dom-primitive-bindings/samples/v7/)** ([local](./samples/v7/index.html))
28
+ Live in-browser demonstrations: Card Grid delegation, Table with live search filtering, and Form controls.
24
29
 
25
30
  ---
26
31
 
@@ -29,120 +34,115 @@ Explore the interactive interactive story chapters in [`docs/stories/`](./docs/i
29
34
  A common mistake in UI architecture is conflating **event wiring** with **DOM manipulation** or **data extraction**:
30
35
 
31
36
  - **`dom-primitive-bindings` (This Repository):**
32
- Pure **Wiring Layer**. Attaches one container listener per event type, matches bubbling targets via `closest(selector)`, and delivers `{ event, targetElement }`. It does **not** manipulate DOM elements and does **not** extract table schemas into objects.
37
+ Pure **Wiring Layer**. Attaches one container listener per event type, matches bubbling targets via `closest(selector)`, and delivers `{ event, targetElement }`. It does **not** manipulate DOM elements and does **not** extract schemas.
33
38
  - **`dom-primitive-engine` (Sibling Repository):**
34
- Pure **Extraction Layer**. Receives the matched target element and executes declarative instruction pipelines (e.g. `closest("tr")`, `querySelectorAll("td")`, column mapping) to produce clean JSON.
39
+ Pure **Extraction Layer**. Receives the matched target element and executes declarative instruction pipelines (e.g. `closest("tr")`, `querySelectorAll("td")`) to produce clean JSON.
35
40
 
36
41
  ```text
37
42
  [ User Action in Browser ]
38
43
  │
39
44
  ▼
40
45
  ┌───────────────────────────────┐
41
- │ dom-primitive-bindings │ ◄── Wiring Layer: Catches event & matches target
46
+ │ dom-primitive-bindings │ ◄── Wiring Layer: Catches bubbling event & matches target
42
47
  │ (guards → prepare → dispatch)│
43
48
  └──────────────┬────────────────┘
44
49
  │ delivers { event, targetElement }
45
50
  ▼
46
51
  ┌───────────────────────────────┐
47
- │ Consumer / App Logic │ ◄── Extraction (dom-primitive-engine) or App Handler
52
+ │ Consumer / App Logic │ ◄── Extraction (dom-primitive-engine) or Action Handler
48
53
  └───────────────────────────────┘
49
54
  ```
50
55
 
51
56
  ---
52
57
 
53
- ## 🏗️ Staged Architecture (`src/v3/`)
58
+ ## ⚡ Quick Start & Distribution
54
59
 
55
- The engine enforces a staged pipeline where every directory contains an `index.js` and all files export a single function as `default`:
60
+ ### 1. Direct Script Tag (Zero Install via GitHub Pages CDN)
56
61
 
57
- ```text
58
- src/v3/
59
- ├── guards/ # Validates container & bindings array
60
- │ ├── container/ # Resolves 1st input (container ID or DOM element)
61
- │ ├── bindings/ # Filters and validates 2nd input (bindings array)
62
- │ └── index.js
63
- ├── prepare/ # Groups bindings by event type into Map
64
- │ ├── groupBindings.js
65
- │ └── index.js
66
- ├── listeners/ # Attaches single container listener per event type
67
- │ ├── attachListener.js
68
- │ └── index.js
69
- ├── dispatch/ # Handles event bubbling, target matching & execution
70
- │ ├── getStartInput.js
71
- │ ├── getClientData.js
72
- │ ├── executeBinding.js
73
- │ └── index.js
74
- ├── register/ # Exposes module onto window.ks.domPrimitiveBindings
75
- │ ├── meta.js
76
- │ └── index.js
77
- └── index.js # Orchestrator
62
+ ```html
63
+ <script type="module" src="https://keshavsoft.github.io/dom-primitive-bindings/dist/v7/min.js"></script>
78
64
  ```
79
65
 
80
- ### Key Design Disciplines
81
- 1. **Zero Comments:** The folder hierarchy and descriptive file names convey the full narrative without inline code comments.
82
- 2. **Folder Discipline:** Every folder contains an `index.js` public gateway.
83
- 3. **Single Function Default Export:** No default export objects; every file exports a single function.
84
- 4. **Declarative JSON:** Bindings are defined in serializable `bindings.json` files.
66
+ Or always use the latest build:
85
67
 
86
- ---
68
+ ```html
69
+ <script type="module" src="https://keshavsoft.github.io/dom-primitive-bindings/dist/min.js"></script>
70
+ ```
71
+
72
+ ### 2. ES Module Import in JavaScript
87
73
 
88
- ## 🚀 Interactive Browser Samples (`samples/v3/`)
74
+ ```javascript
75
+ import { bindEvents } from "https://keshavsoft.github.io/dom-primitive-bindings/dist/v7/min.js";
76
+ ```
89
77
 
90
- Start the Vite development server to view and interact with real browser DOM events:
78
+ ### 3. NPM Package
91
79
 
92
80
  ```bash
93
- npm install
94
- npm run dev
81
+ npm install dom-primitive-bindings
95
82
  ```
96
83
 
97
- Open in Chrome or Edge:
98
- - **`samples/v3/tableWithSearch/`** — Declarative table row clicks and live Enter-key search input loaded from `bindings.json`.
99
- - **`samples/v3/cardGrid/`** — Declarative card button and card body click bindings loaded from `bindings.json`.
100
- - **`samples/v3/formEvents/`** — Declarative input, change, and button click bindings loaded from `bindings.json`.
101
-
102
84
  ---
103
85
 
104
- ## 📦 Usage
86
+ ## 📦 The 3-File Standard Usage Example (v7)
105
87
 
106
- ### 1. Declare `bindings.json`
88
+ ### 1. Declare `bindings.json` (The Blueprint)
107
89
 
108
90
  ```json
109
91
  [
110
92
  {
111
93
  "event": "click",
112
- "selector": ".btn-action"
94
+ "selector": "#rakaBtn",
95
+ "action": "loadRaka"
113
96
  },
114
97
  {
115
- "event": "keydown",
116
- "selector": "input[type='search']"
98
+ "event": "click",
99
+ "selector": "#buildBtn",
100
+ "action": "buildOutput"
117
101
  }
118
102
  ]
119
103
  ```
120
104
 
121
- ### 2. Run with `bindEvents`
105
+ ### 2. Declare `actions.js` (The Behaviors)
122
106
 
123
107
  ```javascript
124
- import bindings from "./bindings.json" with { type: "json" };
125
- import { bindEvents } from "dom-primitive-bindings";
108
+ export const actions = {
109
+ loadRaka: (event, matchElement) => {
110
+ console.log("Loading Raka columns into editor...");
111
+ },
112
+ buildOutput: (event, matchElement) => {
113
+ console.log("Transforming and rendering output...");
114
+ }
115
+ };
126
116
 
127
- bindEvents("container-id", bindings);
117
+ export default actions;
128
118
  ```
129
119
 
130
- Or via global browser script:
120
+ ### 3. Delegate with `index.js` (The Orchestrator)
131
121
 
132
- ```html
133
- <script type="module" src="./docs/dist/v3/min.js"></script>
134
- <script type="module">
135
- import bindings from "./bindings.json" with { type: "json" };
136
- window.ks.domPrimitiveBindings.bindEvents("container-id", bindings);
137
- </script>
122
+ ```javascript
123
+ import bindings from "./bindings.json" with { type: "json" };
124
+ import actions from "./actions.js";
125
+ import { bindEvents } from "dom-primitive-bindings";
126
+
127
+ // Single container listener intercepts bubbling events and dispatches to actions
128
+ bindEvents("app-container", bindings, actions);
138
129
  ```
139
130
 
140
131
  ---
141
132
 
142
- ## 📚 Technical Specifications
133
+ ## 🏗️ Staged Pipeline Architecture
134
+
135
+ The engine enforces a decoupled pipeline where every directory contains an `index.js` public gateway:
143
136
 
144
- - **[Architecture & Design Story](./docs/architecture.md)**: Deep dive into the 4 internal stages and data flow.
145
- - **[Binding Specification Schema](./docs/binding-spec.md)**: Complete property reference for `bindings.json`.
137
+ ```text
138
+ src/v7/
139
+ ├── guards/ # Validates container, bindings array, and actions object
140
+ ├── prepare/ # Groups bindings by event type into Map & wires action mappings
141
+ ├── listeners/ # Attaches single container listener per event type
142
+ ├── dispatch/ # Handles event bubbling, target matching (closest) & execution
143
+ ├── register/ # Exposes module onto globalThis.ks.domPrimitiveBindings
144
+ └── index.js # Orchestrator
145
+ ```
146
146
 
147
147
  ---
148
148
 
@@ -152,12 +152,20 @@ Or via global browser script:
152
152
  # Run unit tests
153
153
  npm test
154
154
 
155
- # Build production bundle to docs/dist/v3/min.js
156
- npm run build
155
+ # Build production bundle using build-cli
156
+ npx @keshavsoft/build-cli build
157
157
  ```
158
158
 
159
159
  ---
160
160
 
161
+ ## 🔗 Useful Links
162
+
163
+ - **Live Documentation**: [https://keshavsoft.github.io/dom-primitive-bindings/](https://keshavsoft.github.io/dom-primitive-bindings/)
164
+ - **Copyable Script Links**: [https://keshavsoft.github.io/dom-primitive-bindings/usage/](https://keshavsoft.github.io/dom-primitive-bindings/usage/)
165
+ - **NPM Package**: [https://www.npmjs.com/package/dom-primitive-bindings](https://www.npmjs.com/package/dom-primitive-bindings)
166
+
167
+ ---
168
+
161
169
  ## 📄 License
162
170
 
163
171
  MIT © KeshavSoft
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dom-primitive-bindings",
3
- "version": "1.4.1",
3
+ "version": "1.8.1",
4
4
  "description": "Declarative, instruction-driven DOM delegated event bindings",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,6 +22,8 @@
22
22
  "README.md",
23
23
  "LICENSE"
24
24
  ],
25
- "scripts": {},
25
+ "scripts": {
26
+ "build": "npx @keshavsoft/build-cli"
27
+ },
26
28
  "devDependencies": {}
27
29
  }
package/src/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from "./v4/index.js";
1
+ export * from "./v7/index.js";
2
2
 
3
- export { default } from "./v4/index.js";
3
+ export { default } from "./v7/index.js";
@@ -3,12 +3,12 @@ import runGuards from "./guards/index.js";
3
3
  import prepareBindings from "./prepare/index.js";
4
4
  import attachListeners from "./listeners/index.js";
5
5
 
6
- const bindEvents = (containerOrId, bindings = []) => {
6
+ const bindEvents = (containerOrId, bindings = [], actions = {}) => {
7
7
  const guarded = runGuards(containerOrId, bindings);
8
8
  if (!guarded) return null;
9
9
 
10
10
  const { container, bindings: validBindings } = guarded;
11
- const eventGroups = prepareBindings(validBindings);
11
+ const eventGroups = prepareBindings(validBindings, actions);
12
12
 
13
13
  attachListeners(container, eventGroups);
14
14
 
@@ -19,4 +19,3 @@ export { bindEvents };
19
19
  export default bindEvents;
20
20
 
21
21
  registerGlobal(bindEvents);
22
-
@@ -0,0 +1,23 @@
1
+ import groupBindings from "./groupBindings.js";
2
+
3
+ const prepareBindings = (bindings, actions = {}) => {
4
+ const hasActions = actions && typeof actions === "object" && Object.keys(actions).length > 0;
5
+
6
+ const configuredBindings = hasActions
7
+ ? bindings.map((binding) => {
8
+ if (binding.action && typeof actions[binding.action] === "function" && typeof binding.onExtract !== "function") {
9
+ return {
10
+ ...binding,
11
+ onExtract: (result, event, matchElement) => {
12
+ actions[binding.action](event, matchElement, result);
13
+ }
14
+ };
15
+ }
16
+ return binding;
17
+ })
18
+ : bindings;
19
+
20
+ return groupBindings(configuredBindings);
21
+ };
22
+
23
+ export default prepareBindings;
@@ -1,6 +1,6 @@
1
1
  export const meta = {
2
2
  name: "dom-primitive-bindings",
3
- version: "3.0.0"
3
+ version: "5.0.0"
4
4
  };
5
5
 
6
6
  export default meta;
@@ -0,0 +1,21 @@
1
+ const isPlainObject = (value) => {
2
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
3
+ const prototype = Object.getPrototypeOf(value);
4
+ return prototype === Object.prototype || prototype === null;
5
+ };
6
+
7
+ const checkActions = (actions = {}) => {
8
+ if (!isPlainObject(actions)) {
9
+ throw new TypeError("actions must be a plain, flat object.");
10
+ }
11
+
12
+ for (const [name, action] of Object.entries(actions)) {
13
+ if (typeof action !== "function") {
14
+ throw new TypeError(`actions.${name} must be a function.`);
15
+ }
16
+ }
17
+
18
+ return actions;
19
+ };
20
+
21
+ export default checkActions;
@@ -0,0 +1,5 @@
1
+ import validateArray from "./validateArray.js";
2
+
3
+ const checkBindings = (bindings) => validateArray(bindings);
4
+
5
+ export default checkBindings;
@@ -0,0 +1,23 @@
1
+ import validateBinding from "./validateBinding.js";
2
+
3
+ const validateArray = (bindings) => {
4
+ if (!Array.isArray(bindings)) {
5
+ throw new TypeError("bindings must be an array.");
6
+ }
7
+
8
+ if (bindings.length === 0) {
9
+ throw new TypeError("bindings must contain at least one binding.");
10
+ }
11
+
12
+ bindings.forEach((binding, index) => {
13
+ if (!validateBinding(binding)) {
14
+ throw new TypeError(
15
+ `bindings[${index}] must be an object with exactly three non-empty string properties: event, selector, and action.`
16
+ );
17
+ }
18
+ });
19
+
20
+ return bindings;
21
+ };
22
+
23
+ export default validateArray;
@@ -0,0 +1,22 @@
1
+ const bindingKeys = ["event", "selector", "action"];
2
+
3
+ const isPlainObject = (value) => {
4
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
5
+ const prototype = Object.getPrototypeOf(value);
6
+ return prototype === Object.prototype || prototype === null;
7
+ };
8
+
9
+ const validateBinding = (binding) => {
10
+ if (!isPlainObject(binding)) return false;
11
+
12
+ const keys = Object.keys(binding);
13
+ if (keys.length !== bindingKeys.length) return false;
14
+ if (!bindingKeys.every((key) => Object.hasOwn(binding, key))) return false;
15
+
16
+ return bindingKeys.every((key) => (
17
+ typeof binding[key] === "string" && binding[key].trim().length > 0
18
+ ));
19
+ };
20
+
21
+ export { bindingKeys, isPlainObject };
22
+ export default validateBinding;
@@ -1,15 +1,19 @@
1
1
  import checkContainer from "./container/index.js";
2
2
  import checkBindings from "./bindings/index.js";
3
+ import checkActions from "./actions/index.js";
3
4
 
4
- const runGuards = (containerOrId, bindings) => {
5
+ const runGuards = (containerOrId, bindings, actions = {}) => {
5
6
  const container = checkContainer(containerOrId);
6
7
  if (!container) return null;
7
8
 
8
9
  const validBindings = checkBindings(bindings);
9
- if (!validBindings) return null;
10
+ const validActions = checkActions(actions);
10
11
 
11
- return { container, bindings: validBindings };
12
+ return {
13
+ container,
14
+ bindings: validBindings,
15
+ actions: validActions
16
+ };
12
17
  };
13
18
 
14
19
  export default runGuards;
15
-
@@ -3,12 +3,12 @@ import runGuards from "./guards/index.js";
3
3
  import prepareBindings from "./prepare/index.js";
4
4
  import attachListeners from "./listeners/index.js";
5
5
 
6
- const bindEvents = (containerOrId, bindings = []) => {
7
- const guarded = runGuards(containerOrId, bindings);
6
+ const bindEvents = (containerOrId, bindings = [], actions = {}) => {
7
+ const guarded = runGuards(containerOrId, bindings, actions);
8
8
  if (!guarded) return null;
9
9
 
10
10
  const { container, bindings: validBindings } = guarded;
11
- const eventGroups = prepareBindings(validBindings);
11
+ const eventGroups = prepareBindings(validBindings, guarded.actions);
12
12
 
13
13
  attachListeners(container, eventGroups);
14
14
 
@@ -19,4 +19,3 @@ export { bindEvents };
19
19
  export default bindEvents;
20
20
 
21
21
  registerGlobal(bindEvents);
22
-
@@ -0,0 +1,23 @@
1
+ import groupBindings from "./groupBindings.js";
2
+
3
+ const prepareBindings = (bindings, actions = {}) => {
4
+ const hasActions = actions && typeof actions === "object" && Object.keys(actions).length > 0;
5
+
6
+ const configuredBindings = hasActions
7
+ ? bindings.map((binding) => {
8
+ if (binding.action && typeof actions[binding.action] === "function" && typeof binding.onExtract !== "function") {
9
+ return {
10
+ ...binding,
11
+ onExtract: (result, event, matchElement) => {
12
+ actions[binding.action](event, matchElement, result);
13
+ }
14
+ };
15
+ }
16
+ return binding;
17
+ })
18
+ : bindings;
19
+
20
+ return groupBindings(configuredBindings);
21
+ };
22
+
23
+ export default prepareBindings;
@@ -1,6 +1,6 @@
1
1
  export const meta = {
2
2
  name: "dom-primitive-bindings",
3
- version: "2.0.0"
3
+ version: "5.0.0"
4
4
  };
5
5
 
6
6
  export default meta;
@@ -0,0 +1,21 @@
1
+ const isPlainObject = (value) => {
2
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
3
+ const prototype = Object.getPrototypeOf(value);
4
+ return prototype === Object.prototype || prototype === null;
5
+ };
6
+
7
+ const checkActions = (actions = {}) => {
8
+ if (!isPlainObject(actions)) {
9
+ throw new TypeError("actions must be a plain, flat object.");
10
+ }
11
+
12
+ for (const [name, action] of Object.entries(actions)) {
13
+ if (typeof action !== "function") {
14
+ throw new TypeError(`actions.${name} must be a function.`);
15
+ }
16
+ }
17
+
18
+ return actions;
19
+ };
20
+
21
+ export default checkActions;
@@ -0,0 +1,5 @@
1
+ import validateArray from "./validateArray.js";
2
+
3
+ const checkBindings = (bindings) => validateArray(bindings);
4
+
5
+ export default checkBindings;
@@ -0,0 +1,23 @@
1
+ import validateBinding from "./validateBinding.js";
2
+
3
+ const validateArray = (bindings) => {
4
+ if (!Array.isArray(bindings)) {
5
+ throw new TypeError("bindings must be an array.");
6
+ }
7
+
8
+ if (bindings.length === 0) {
9
+ throw new TypeError("bindings must contain at least one binding.");
10
+ }
11
+
12
+ bindings.forEach((binding, index) => {
13
+ if (!validateBinding(binding)) {
14
+ throw new TypeError(
15
+ `bindings[${index}] must be an object with exactly three non-empty string properties: event, selector, and action.`
16
+ );
17
+ }
18
+ });
19
+
20
+ return bindings;
21
+ };
22
+
23
+ export default validateArray;
@@ -0,0 +1,22 @@
1
+ const bindingKeys = ["event", "selector", "action"];
2
+
3
+ const isPlainObject = (value) => {
4
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
5
+ const prototype = Object.getPrototypeOf(value);
6
+ return prototype === Object.prototype || prototype === null;
7
+ };
8
+
9
+ const validateBinding = (binding) => {
10
+ if (!isPlainObject(binding)) return false;
11
+
12
+ const keys = Object.keys(binding);
13
+ if (keys.length !== bindingKeys.length) return false;
14
+ if (!bindingKeys.every((key) => Object.hasOwn(binding, key))) return false;
15
+
16
+ return bindingKeys.every((key) => (
17
+ typeof binding[key] === "string" && binding[key].trim().length > 0
18
+ ));
19
+ };
20
+
21
+ export { bindingKeys, isPlainObject };
22
+ export default validateBinding;
@@ -1,15 +1,19 @@
1
1
  import checkContainer from "./container/index.js";
2
2
  import checkBindings from "./bindings/index.js";
3
+ import checkActions from "./actions/index.js";
3
4
 
4
- const runGuards = (containerOrId, bindings) => {
5
+ const runGuards = (containerOrId, bindings, actions = {}) => {
5
6
  const container = checkContainer(containerOrId);
6
7
  if (!container) return null;
7
8
 
8
9
  const validBindings = checkBindings(bindings);
9
- if (!validBindings) return null;
10
+ const validActions = checkActions(actions);
10
11
 
11
- return { container, bindings: validBindings };
12
+ return {
13
+ container,
14
+ bindings: validBindings,
15
+ actions: validActions
16
+ };
12
17
  };
13
18
 
14
19
  export default runGuards;
15
-
@@ -0,0 +1,43 @@
1
+ import registerGlobal from "./register/index.js";
2
+ import runGuards from "./guards/index.js";
3
+ import prepareBindings from "./prepare/index.js";
4
+ import attachListeners from "./listeners/index.js";
5
+ import sample, {
6
+ sampleContainer,
7
+ sampleBindings,
8
+ sampleActions
9
+ } from "./sample/index.js";
10
+
11
+ const check = (containerOrId, bindings = [], actions = {}) =>
12
+ runGuards(containerOrId, bindings, actions);
13
+
14
+ const bindEvents = (containerOrId, bindings = [], actions = {}) => {
15
+ const guarded = check(containerOrId, bindings, actions);
16
+ if (!guarded) return null;
17
+
18
+ const { container, bindings: validBindings } = guarded;
19
+ const eventGroups = prepareBindings(validBindings, guarded.actions);
20
+
21
+ attachListeners(container, eventGroups);
22
+
23
+ return container;
24
+ };
25
+
26
+ export {
27
+ bindEvents,
28
+ check,
29
+ sample,
30
+ sampleContainer,
31
+ sampleBindings,
32
+ sampleActions
33
+ };
34
+ export default bindEvents;
35
+
36
+ registerGlobal({
37
+ bindEvents,
38
+ check,
39
+ sample,
40
+ sampleContainer,
41
+ sampleBindings,
42
+ sampleActions
43
+ });
@@ -0,0 +1,23 @@
1
+ import groupBindings from "./groupBindings.js";
2
+
3
+ const prepareBindings = (bindings, actions = {}) => {
4
+ const hasActions = actions && typeof actions === "object" && Object.keys(actions).length > 0;
5
+
6
+ const configuredBindings = hasActions
7
+ ? bindings.map((binding) => {
8
+ if (binding.action && typeof actions[binding.action] === "function" && typeof binding.onExtract !== "function") {
9
+ return {
10
+ ...binding,
11
+ onExtract: (result, event, matchElement) => {
12
+ actions[binding.action](event, matchElement, result);
13
+ }
14
+ };
15
+ }
16
+ return binding;
17
+ })
18
+ : bindings;
19
+
20
+ return groupBindings(configuredBindings);
21
+ };
22
+
23
+ export default prepareBindings;