dom-primitive-bindings 1.4.1 → 1.7.3

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 (64) hide show
  1. package/README.md +153 -163
  2. package/package.json +1 -1
  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 +19 -15
  12. package/src/{v2 → v6}/index.js +21 -22
  13. package/src/v6/prepare/index.js +23 -0
  14. package/src/{v2 → v6}/register/meta.js +6 -6
  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 +19 -15
  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 +18 -18
  23. package/src/{v3 → v7}/register/meta.js +6 -6
  24. package/src/v7/sample/index.js +22 -0
  25. package/src/v2/prepare/index.js +0 -8
  26. package/src/v3/guards/bindings/index.js +0 -11
  27. package/src/v3/guards/bindings/validateArray.js +0 -10
  28. package/src/v3/prepare/index.js +0 -8
  29. package/src/v4/guards/bindings/index.js +0 -11
  30. package/src/v4/guards/bindings/validateArray.js +0 -10
  31. package/src/v4/index.js +0 -22
  32. package/src/v4/prepare/index.js +0 -8
  33. /package/src/{v2 → v5}/dispatch/executeBinding.js +0 -0
  34. /package/src/{v2 → v5}/dispatch/getClientData.js +0 -0
  35. /package/src/{v2 → v5}/dispatch/getStartInput.js +0 -0
  36. /package/src/{v2 → v5}/dispatch/index.js +0 -0
  37. /package/src/{v2 → v5}/guards/bindings/index.js +0 -0
  38. /package/src/{v2 → v5}/guards/bindings/validateArray.js +0 -0
  39. /package/src/{v2 → v5}/guards/container/index.js +0 -0
  40. /package/src/{v2 → v5}/guards/container/resolveElement.js +0 -0
  41. /package/src/{v2 → v5}/guards/index.js +0 -0
  42. /package/src/{v2 → v5}/listeners/attachListener.js +0 -0
  43. /package/src/{v2 → v5}/listeners/index.js +0 -0
  44. /package/src/{v2 → v5}/prepare/groupBindings.js +0 -0
  45. /package/src/{v2 → v5}/register/index.js +0 -0
  46. /package/src/{v3 → v6}/dispatch/executeBinding.js +0 -0
  47. /package/src/{v3 → v6}/dispatch/getClientData.js +0 -0
  48. /package/src/{v3 → v6}/dispatch/getStartInput.js +0 -0
  49. /package/src/{v3 → v6}/dispatch/index.js +0 -0
  50. /package/src/{v3 → v6}/guards/container/index.js +0 -0
  51. /package/src/{v3 → v6}/guards/container/resolveElement.js +0 -0
  52. /package/src/{v3 → v6}/listeners/attachListener.js +0 -0
  53. /package/src/{v3 → v6}/listeners/index.js +0 -0
  54. /package/src/{v3 → v6}/prepare/groupBindings.js +0 -0
  55. /package/src/{v3 → v6}/register/index.js +0 -0
  56. /package/src/{v4 → v7}/dispatch/executeBinding.js +0 -0
  57. /package/src/{v4 → v7}/dispatch/getClientData.js +0 -0
  58. /package/src/{v4 → v7}/dispatch/getStartInput.js +0 -0
  59. /package/src/{v4 → v7}/dispatch/index.js +0 -0
  60. /package/src/{v4 → v7}/guards/container/index.js +0 -0
  61. /package/src/{v4 → v7}/guards/container/resolveElement.js +0 -0
  62. /package/src/{v4 → v7}/listeners/attachListener.js +0 -0
  63. /package/src/{v4 → v7}/listeners/index.js +0 -0
  64. /package/src/{v4 → v7}/prepare/groupBindings.js +0 -0
package/README.md CHANGED
@@ -1,163 +1,153 @@
1
- # DOM Primitive Bindings
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.
4
-
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
- [![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)
9
-
10
- ---
11
-
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.
24
-
25
- ---
26
-
27
- ## 🎯 The Philosophy: The Wiring Layer
28
-
29
- A common mistake in UI architecture is conflating **event wiring** with **DOM manipulation** or **data extraction**:
30
-
31
- - **`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.
33
- - **`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.
35
-
36
- ```text
37
- [ User Action in Browser ]
38
- │
39
- ▼
40
- ┌───────────────────────────────┐
41
- │ dom-primitive-bindings │ ◄── Wiring Layer: Catches event & matches target
42
- │ (guards → prepare → dispatch)│
43
- └──────────────┬────────────────┘
44
- │ delivers { event, targetElement }
45
- ▼
46
- ┌───────────────────────────────┐
47
- │ Consumer / App Logic │ ◄── Extraction (dom-primitive-engine) or App Handler
48
- └───────────────────────────────┘
49
- ```
50
-
51
- ---
52
-
53
- ## 🏗️ Staged Architecture (`src/v3/`)
54
-
55
- The engine enforces a staged pipeline where every directory contains an `index.js` and all files export a single function as `default`:
56
-
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
78
- ```
79
-
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.
85
-
86
- ---
87
-
88
- ## 🚀 Interactive Browser Samples (`samples/v3/`)
89
-
90
- Start the Vite development server to view and interact with real browser DOM events:
91
-
92
- ```bash
93
- npm install
94
- npm run dev
95
- ```
96
-
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
- ---
103
-
104
- ## 📦 Usage
105
-
106
- ### 1. Declare `bindings.json`
107
-
108
- ```json
109
- [
110
- {
111
- "event": "click",
112
- "selector": ".btn-action"
113
- },
114
- {
115
- "event": "keydown",
116
- "selector": "input[type='search']"
117
- }
118
- ]
119
- ```
120
-
121
- ### 2. Run with `bindEvents`
122
-
123
- ```javascript
124
- import bindings from "./bindings.json" with { type: "json" };
125
- import { bindEvents } from "dom-primitive-bindings";
126
-
127
- bindEvents("container-id", bindings);
128
- ```
129
-
130
- Or via global browser script:
131
-
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>
138
- ```
139
-
140
- ---
141
-
142
- ## 📚 Technical Specifications
143
-
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`.
146
-
147
- ---
148
-
149
- ## 🧪 Testing & Build
150
-
151
- ```bash
152
- # Run unit tests
153
- npm test
154
-
155
- # Build production bundle to docs/dist/v3/min.js
156
- npm run build
157
- ```
158
-
159
- ---
160
-
161
- ## 📄 License
162
-
163
- MIT © KeshavSoft
1
+ # DOM Primitive Bindings
2
+
3
+ > Browser-native, single-listener container event delegation driven by declarative JSON instructions.
4
+
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
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT)
7
+ [![GitHub Pages](https://img.shields.io/badge/Docs-GitHub%20Pages-success.svg?style=flat-square)](https://keshavsoft.github.io/dom-primitive-bindings/)
8
+
9
+ ---
10
+
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. **[Usage & Available Builds](https://keshavsoft.github.io/dom-primitive-bindings/usage/)** ([local](./docs/usage/index.html))
22
+ Available builds hosted on GitHub Pages CDN with one-click copyable script tags and ES Module imports.
23
+
24
+ ---
25
+
26
+ ## 🎯 The Philosophy: The Wiring Layer
27
+
28
+ A common mistake in UI architecture is conflating **event wiring** with **DOM manipulation** or **data extraction**:
29
+
30
+ - **`dom-primitive-bindings` (This Repository):**
31
+ 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.
32
+ - **`dom-primitive-engine` (Sibling Repository):**
33
+ Pure **Extraction Layer**. Receives the matched target element and executes declarative instruction pipelines (e.g. `closest("tr")`, `querySelectorAll("td")`) to produce clean JSON.
34
+
35
+ ```text
36
+ [ User Action in Browser ]
37
+ │
38
+ ▼
39
+ ┌───────────────────────────────┐
40
+ │ dom-primitive-bindings │ ◄── Wiring Layer: Catches bubbling event & matches target
41
+ │ (guards → prepare → dispatch)│
42
+ └──────────────┬────────────────┘
43
+ │ delivers { event, targetElement }
44
+ ▼
45
+ ┌───────────────────────────────┐
46
+ │ Consumer / App Logic │ ◄── Extraction (dom-primitive-engine) or Action Handler
47
+ └───────────────────────────────┘
48
+ ```
49
+
50
+ ---
51
+
52
+ ## ⚡ Quick Start & Distribution
53
+
54
+ ### 1. Direct Script Tag (Zero Install via GitHub Pages CDN)
55
+
56
+ ```html
57
+ <script type="module" src="https://keshavsoft.github.io/dom-primitive-bindings/dist/v4/min.js"></script>
58
+ ```
59
+
60
+ Or always use the latest build:
61
+
62
+ ```html
63
+ <script type="module" src="https://keshavsoft.github.io/dom-primitive-bindings/dist/min.js"></script>
64
+ ```
65
+
66
+ ### 2. ES Module Import in JavaScript
67
+
68
+ ```javascript
69
+ import { bindEvents } from "https://keshavsoft.github.io/dom-primitive-bindings/dist/v4/min.js";
70
+ ```
71
+
72
+ ### 3. NPM Package
73
+
74
+ ```bash
75
+ npm install dom-primitive-bindings
76
+ ```
77
+
78
+ ---
79
+
80
+ ## 📦 Usage Example
81
+
82
+ ### 1. Declare `bindings.json`
83
+
84
+ ```json
85
+ [
86
+ {
87
+ "event": "click",
88
+ "selector": "#rakaBtn",
89
+ "narration": "Load and populate Raka columns JSON into editor"
90
+ },
91
+ {
92
+ "event": "click",
93
+ "selector": "#templateBtn",
94
+ "narration": "Load and populate template JSON into editor"
95
+ },
96
+ {
97
+ "event": "click",
98
+ "selector": "#buildBtn",
99
+ "narration": "Transform columns with template and display output in editor"
100
+ }
101
+ ]
102
+ ```
103
+
104
+ ### 2. Delegate with `bindEvents`
105
+
106
+ ```javascript
107
+ import { bindEvents } from "https://keshavsoft.github.io/dom-primitive-bindings/dist/v4/min.js";
108
+ import bindings from "./bindings.json" with { type: "json" };
109
+
110
+ bindEvents("app-container", bindings);
111
+ ```
112
+
113
+ ---
114
+
115
+ ## 🏗️ Staged Pipeline Architecture
116
+
117
+ The engine enforces a decoupled pipeline where every directory contains an `index.js` public gateway:
118
+
119
+ ```text
120
+ src/v4/
121
+ ├── guards/ # Validates container & bindings array before execution
122
+ ├── prepare/ # Groups bindings by event type into Map
123
+ ├── listeners/ # Attaches single container listener per event type
124
+ ├── dispatch/ # Handles event bubbling, target matching (closest) & execution
125
+ ├── register/ # Exposes module onto window.ks.domPrimitiveBindings
126
+ └── index.js # Orchestrator
127
+ ```
128
+
129
+ ---
130
+
131
+ ## 🧪 Testing & Build
132
+
133
+ ```bash
134
+ # Run unit tests
135
+ npm test
136
+
137
+ # Build production bundle using build-cli
138
+ npx @keshavsoft/build-cli build
139
+ ```
140
+
141
+ ---
142
+
143
+ ## 🔗 Useful Links
144
+
145
+ - **Live Documentation**: [https://keshavsoft.github.io/dom-primitive-bindings/](https://keshavsoft.github.io/dom-primitive-bindings/)
146
+ - **Copyable Script Links**: [https://keshavsoft.github.io/dom-primitive-bindings/usage/](https://keshavsoft.github.io/dom-primitive-bindings/usage/)
147
+ - **NPM Package**: [https://www.npmjs.com/package/dom-primitive-bindings](https://www.npmjs.com/package/dom-primitive-bindings)
148
+
149
+ ---
150
+
151
+ ## 📄 License
152
+
153
+ 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.7.3",
4
4
  "description": "Declarative, instruction-driven DOM delegated event bindings",
5
5
  "type": "module",
6
6
  "license": "MIT",
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
- import checkContainer from "./container/index.js";
2
- import checkBindings from "./bindings/index.js";
3
-
4
- const runGuards = (containerOrId, bindings) => {
5
- const container = checkContainer(containerOrId);
6
- if (!container) return null;
7
-
8
- const validBindings = checkBindings(bindings);
9
- if (!validBindings) return null;
10
-
11
- return { container, bindings: validBindings };
12
- };
13
-
14
- export default runGuards;
15
-
1
+ import checkContainer from "./container/index.js";
2
+ import checkBindings from "./bindings/index.js";
3
+ import checkActions from "./actions/index.js";
4
+
5
+ const runGuards = (containerOrId, bindings, actions = {}) => {
6
+ const container = checkContainer(containerOrId);
7
+ if (!container) return null;
8
+
9
+ const validBindings = checkBindings(bindings);
10
+ const validActions = checkActions(actions);
11
+
12
+ return {
13
+ container,
14
+ bindings: validBindings,
15
+ actions: validActions
16
+ };
17
+ };
18
+
19
+ export default runGuards;
@@ -1,22 +1,21 @@
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
-
6
- const bindEvents = (containerOrId, bindings = []) => {
7
- const guarded = runGuards(containerOrId, bindings);
8
- if (!guarded) return null;
9
-
10
- const { container, bindings: validBindings } = guarded;
11
- const eventGroups = prepareBindings(validBindings);
12
-
13
- attachListeners(container, eventGroups);
14
-
15
- return container;
16
- };
17
-
18
- export { bindEvents };
19
- export default bindEvents;
20
-
21
- registerGlobal(bindEvents);
22
-
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
+
6
+ const bindEvents = (containerOrId, bindings = [], actions = {}) => {
7
+ const guarded = runGuards(containerOrId, bindings, actions);
8
+ if (!guarded) return null;
9
+
10
+ const { container, bindings: validBindings } = guarded;
11
+ const eventGroups = prepareBindings(validBindings, guarded.actions);
12
+
13
+ attachListeners(container, eventGroups);
14
+
15
+ return container;
16
+ };
17
+
18
+ export { bindEvents };
19
+ export default bindEvents;
20
+
21
+ registerGlobal(bindEvents);
@@ -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
- export const meta = {
2
- name: "dom-primitive-bindings",
3
- version: "2.0.0"
4
- };
5
-
6
- export default meta;
1
+ export const meta = {
2
+ name: "dom-primitive-bindings",
3
+ version: "5.0.0"
4
+ };
5
+
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
- import checkContainer from "./container/index.js";
2
- import checkBindings from "./bindings/index.js";
3
-
4
- const runGuards = (containerOrId, bindings) => {
5
- const container = checkContainer(containerOrId);
6
- if (!container) return null;
7
-
8
- const validBindings = checkBindings(bindings);
9
- if (!validBindings) return null;
10
-
11
- return { container, bindings: validBindings };
12
- };
13
-
14
- export default runGuards;
15
-
1
+ import checkContainer from "./container/index.js";
2
+ import checkBindings from "./bindings/index.js";
3
+ import checkActions from "./actions/index.js";
4
+
5
+ const runGuards = (containerOrId, bindings, actions = {}) => {
6
+ const container = checkContainer(containerOrId);
7
+ if (!container) return null;
8
+
9
+ const validBindings = checkBindings(bindings);
10
+ const validActions = checkActions(actions);
11
+
12
+ return {
13
+ container,
14
+ bindings: validBindings,
15
+ actions: validActions
16
+ };
17
+ };
18
+
19
+ export default runGuards;
@@ -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;
@@ -1,18 +1,18 @@
1
- import meta from "./meta.js";
2
-
3
- const registerGlobal = (bindEvents, metadata = meta) => {
4
- if (!bindEvents || typeof globalThis === "undefined") return;
5
-
6
- globalThis.ks ??= {};
7
-
8
- globalThis.ks.domPrimitiveBindings = {
9
- meta: metadata,
10
- bindEvents
11
- };
12
-
13
- globalThis.ks.bindEvents ??= bindEvents;
14
-
15
- return globalThis.ks.domPrimitiveBindings;
16
- };
17
-
18
- export default registerGlobal;
1
+ import meta from "./meta.js";
2
+
3
+ const registerGlobal = (api, metadata = meta) => {
4
+ if (!api || typeof globalThis === "undefined") return;
5
+
6
+ globalThis.ks ??= {};
7
+
8
+ globalThis.ks.domPrimitiveBindings = {
9
+ meta: metadata,
10
+ ...api
11
+ };
12
+
13
+ globalThis.ks.bindEvents ??= api.bindEvents;
14
+
15
+ return globalThis.ks.domPrimitiveBindings;
16
+ };
17
+
18
+ export default registerGlobal;
@@ -1,6 +1,6 @@
1
- export const meta = {
2
- name: "dom-primitive-bindings",
3
- version: "3.0.0"
4
- };
5
-
6
- export default meta;
1
+ export const meta = {
2
+ name: "dom-primitive-bindings",
3
+ version: "5.0.0"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,22 @@
1
+ const sampleContainer = () => "app";
2
+
3
+ const sampleBindings = () => [
4
+ {
5
+ event: "click",
6
+ selector: "#loadBtn",
7
+ action: "loadData"
8
+ }
9
+ ];
10
+
11
+ const sampleActions = () => ({
12
+ loadData: () => {}
13
+ });
14
+
15
+ const sample = () => ({
16
+ containerOrId: sampleContainer(),
17
+ bindings: sampleBindings(),
18
+ actions: sampleActions()
19
+ });
20
+
21
+ export { sample, sampleContainer, sampleBindings, sampleActions };
22
+ export default sample;
@@ -1,8 +0,0 @@
1
- import groupBindings from "./groupBindings.js";
2
-
3
- const prepareBindings = (bindings) => {
4
- return groupBindings(bindings);
5
- };
6
-
7
- export default prepareBindings;
8
-
@@ -1,11 +0,0 @@
1
- import validateArray from "./validateArray.js";
2
-
3
- const checkBindings = (bindings) => {
4
- const valid = validateArray(bindings);
5
- if (valid.length === 0) return null;
6
-
7
- return valid;
8
- };
9
-
10
- export default checkBindings;
11
-
@@ -1,10 +0,0 @@
1
- const validateArray = (bindings) => {
2
- if (!Array.isArray(bindings)) return [];
3
-
4
- return bindings.filter((binding) => (
5
- binding && typeof binding === "object" && typeof binding.event === "string" && typeof binding.selector === "string"
6
- ));
7
- };
8
-
9
- export default validateArray;
10
-
@@ -1,8 +0,0 @@
1
- import groupBindings from "./groupBindings.js";
2
-
3
- const prepareBindings = (bindings) => {
4
- return groupBindings(bindings);
5
- };
6
-
7
- export default prepareBindings;
8
-
@@ -1,11 +0,0 @@
1
- import validateArray from "./validateArray.js";
2
-
3
- const checkBindings = (bindings) => {
4
- const valid = validateArray(bindings);
5
- if (valid.length === 0) return null;
6
-
7
- return valid;
8
- };
9
-
10
- export default checkBindings;
11
-
@@ -1,10 +0,0 @@
1
- const validateArray = (bindings) => {
2
- if (!Array.isArray(bindings)) return [];
3
-
4
- return bindings.filter((binding) => (
5
- binding && typeof binding === "object" && typeof binding.event === "string" && typeof binding.selector === "string"
6
- ));
7
- };
8
-
9
- export default validateArray;
10
-
package/src/v4/index.js DELETED
@@ -1,22 +0,0 @@
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
-
6
- const bindEvents = (containerOrId, bindings = []) => {
7
- const guarded = runGuards(containerOrId, bindings);
8
- if (!guarded) return null;
9
-
10
- const { container, bindings: validBindings } = guarded;
11
- const eventGroups = prepareBindings(validBindings);
12
-
13
- attachListeners(container, eventGroups);
14
-
15
- return container;
16
- };
17
-
18
- export { bindEvents };
19
- export default bindEvents;
20
-
21
- registerGlobal(bindEvents);
22
-
@@ -1,8 +0,0 @@
1
- import groupBindings from "./groupBindings.js";
2
-
3
- const prepareBindings = (bindings) => {
4
- return groupBindings(bindings);
5
- };
6
-
7
- export default prepareBindings;
8
-
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes