dom-primitive-bindings 1.7.3 → 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 (39) hide show
  1. package/README.md +171 -153
  2. package/package.json +4 -2
  3. package/src/v6/guards/actions/index.js +21 -21
  4. package/src/v6/guards/bindings/index.js +5 -5
  5. package/src/v6/guards/bindings/validateArray.js +23 -23
  6. package/src/v6/guards/bindings/validateBinding.js +22 -22
  7. package/src/v6/guards/index.js +19 -19
  8. package/src/v6/index.js +21 -21
  9. package/src/v6/prepare/index.js +23 -23
  10. package/src/v6/register/meta.js +6 -6
  11. package/src/v7/guards/actions/index.js +21 -21
  12. package/src/v7/guards/bindings/index.js +5 -5
  13. package/src/v7/guards/bindings/validateArray.js +23 -23
  14. package/src/v7/guards/bindings/validateBinding.js +22 -22
  15. package/src/v7/guards/index.js +19 -19
  16. package/src/v7/index.js +43 -43
  17. package/src/v7/prepare/index.js +23 -23
  18. package/src/v7/register/index.js +18 -18
  19. package/src/v7/register/meta.js +6 -6
  20. package/src/v7/sample/index.js +22 -22
  21. package/src/v8/dispatch/executeBinding.js +21 -0
  22. package/src/v8/dispatch/getClientData.js +8 -0
  23. package/src/v8/dispatch/getStartInput.js +10 -0
  24. package/src/v8/dispatch/index.js +15 -0
  25. package/src/v8/guards/actions/index.js +21 -0
  26. package/src/v8/guards/bindings/index.js +5 -0
  27. package/src/v8/guards/bindings/validateArray.js +23 -0
  28. package/src/v8/guards/bindings/validateBinding.js +22 -0
  29. package/src/v8/guards/container/index.js +13 -0
  30. package/src/v8/guards/container/resolveElement.js +14 -0
  31. package/src/v8/guards/index.js +19 -0
  32. package/src/v8/index.js +37 -0
  33. package/src/v8/listeners/attachListener.js +10 -0
  34. package/src/v8/listeners/index.js +12 -0
  35. package/src/v8/prepare/groupBindings.js +14 -0
  36. package/src/v8/prepare/index.js +23 -0
  37. package/src/v8/register/index.js +15 -0
  38. package/src/v8/register/meta.js +6 -0
  39. package/src/v8/sample/index.js +22 -0
@@ -1,21 +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;
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;
@@ -1,5 +1,5 @@
1
- import validateArray from "./validateArray.js";
2
-
3
- const checkBindings = (bindings) => validateArray(bindings);
4
-
5
- export default checkBindings;
1
+ import validateArray from "./validateArray.js";
2
+
3
+ const checkBindings = (bindings) => validateArray(bindings);
4
+
5
+ export default checkBindings;
@@ -1,23 +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;
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;
@@ -1,22 +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
+ 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,19 +1,19 @@
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
+ 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;
package/src/v7/index.js CHANGED
@@ -1,43 +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
- });
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
+ });
@@ -1,23 +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
+ 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 = (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
+ 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: "5.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;
@@ -1,22 +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
+ 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;
@@ -0,0 +1,21 @@
1
+ import getStartInput from "./getStartInput.js";
2
+ import getClientData from "./getClientData.js";
3
+
4
+ const executeBinding = ({ binding, event, matchElement }) => {
5
+ const startInput = getStartInput(binding, event, matchElement);
6
+ const clientData = getClientData(binding, event, matchElement);
7
+ const runner = binding.runner;
8
+
9
+ const result = typeof runner === "function"
10
+ ? runner(startInput, binding.instructions, clientData)
11
+ : startInput;
12
+
13
+ if (result !== null && result !== undefined && typeof binding.onExtract === "function") {
14
+ binding.onExtract(result, event, matchElement);
15
+ }
16
+
17
+ return result;
18
+ };
19
+
20
+ export default executeBinding;
21
+
@@ -0,0 +1,8 @@
1
+ const getClientData = (binding, event, matchElement) => ({
2
+ ...binding.clientData,
3
+ event,
4
+ targetElement: matchElement
5
+ });
6
+
7
+ export default getClientData;
8
+
@@ -0,0 +1,10 @@
1
+ const getStartInput = (binding, event, matchElement) => {
2
+ if (binding.root === "event" || event?.type?.startsWith("key")) {
3
+ return event;
4
+ }
5
+
6
+ return matchElement;
7
+ };
8
+
9
+ export default getStartInput;
10
+
@@ -0,0 +1,15 @@
1
+ import executeBinding from "./executeBinding.js";
2
+
3
+ const dispatchEvent = ({ bindings, event, container }) => {
4
+ for (const binding of bindings) {
5
+ const matchElement = event.target?.closest?.(binding.selector);
6
+ if (!matchElement || !container.contains(matchElement)) {
7
+ continue;
8
+ }
9
+
10
+ executeBinding({ binding, event, matchElement });
11
+ }
12
+ };
13
+
14
+ export default dispatchEvent;
15
+
@@ -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;
@@ -0,0 +1,13 @@
1
+ import resolveElement from "./resolveElement.js";
2
+
3
+ const checkContainer = (containerOrId) => {
4
+ if (!containerOrId) return null;
5
+
6
+ const element = resolveElement(containerOrId);
7
+ if (!element) return null;
8
+
9
+ return element;
10
+ };
11
+
12
+ export default checkContainer;
13
+
@@ -0,0 +1,14 @@
1
+ const resolveElement = (containerOrId) => {
2
+ if (typeof containerOrId === "string") {
3
+ return document.getElementById(containerOrId) || document.querySelector(containerOrId);
4
+ }
5
+
6
+ if (containerOrId && typeof containerOrId === "object" && "addEventListener" in containerOrId) {
7
+ return containerOrId;
8
+ }
9
+
10
+ return null;
11
+ };
12
+
13
+ export default resolveElement;
14
+
@@ -0,0 +1,19 @@
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,37 @@
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
+
35
+ export default bindEvents;
36
+
37
+ registerGlobal(bindEvents);
@@ -0,0 +1,10 @@
1
+ import dispatchEvent from "../dispatch/index.js";
2
+
3
+ const attachListener = (container, eventType, bindings) => {
4
+ container.addEventListener(eventType, (event) => {
5
+ dispatchEvent({ bindings, event, container });
6
+ });
7
+ };
8
+
9
+ export default attachListener;
10
+
@@ -0,0 +1,12 @@
1
+ import attachListener from "./attachListener.js";
2
+
3
+ const attachListeners = (container, eventGroups) => {
4
+ for (const [eventType, bindings] of eventGroups.entries()) {
5
+ attachListener(container, eventType, bindings);
6
+ }
7
+
8
+ return container;
9
+ };
10
+
11
+ export default attachListeners;
12
+