aprog 2.2.0 → 2.2.2

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.
package/index.js CHANGED
@@ -22,5 +22,6 @@ export {
22
22
  } from './src/html.js';
23
23
  export { prism } from "./src/prism.js";
24
24
  export { uniquePage } from "./src/pages.js";
25
- export { default as AprogClass } from './src/classes/AprogClass.js';
25
+ export { state, wrap } from "./src/helper.js";
26
+ export { default as StateWrapper } from './src/classes/StateWrapper.js';
26
27
  export { default as SmartWrapper } from './src/classes/SmartWrapper.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aprog",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Допоміжна JavaScript-бібліотека для проєктів",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -150,6 +150,8 @@ class SmartWrapper {
150
150
  if (typeof this._value === 'number' || typeof this._value === 'boolean') {
151
151
  return String(this._value);
152
152
  }
153
+ if (Array.isArray(this._value)) return JSON.stringify(this._value);
154
+ if (typeof this._value === 'object' && this._value !== null) return JSON.stringify(Object.values(this._value));
153
155
  return '';
154
156
  }
155
157
  }
@@ -0,0 +1,57 @@
1
+ class StateWrapper {
2
+ #value;
3
+
4
+ constructor(initialValue) {
5
+ this.#value = initialValue;
6
+
7
+ const handler = {
8
+ get: (target, prop, receiver) => {
9
+ if (prop === 'val') return target.#value;
10
+
11
+ if (prop === 'set') {
12
+ return (newValue) => {
13
+ target.#value = typeof newValue === 'function' ? newValue(target.#value) : newValue;
14
+ };
15
+ }
16
+
17
+ if (prop === 'merge') {
18
+ return (patch) => {
19
+ if (typeof target.#value === 'object' && target.#value !== null) {
20
+ target.#value = { ...target.#value, ...patch };
21
+ }
22
+ };
23
+ }
24
+
25
+ if (prop === 'toJSON') return () => target.#value;
26
+ if (prop === 'toString') return () => String(target.#value);
27
+ if (prop === 'valueOf') return () => target.#value;
28
+
29
+ if (
30
+ typeof target.#value === 'object' &&
31
+ target.#value !== null &&
32
+ prop in target.#value
33
+ ) {
34
+ return target.#value[prop];
35
+ }
36
+
37
+ return Reflect.get(target, prop, receiver);
38
+ },
39
+
40
+ set: (target, prop, newVal) => {
41
+ if (typeof target.#value === 'object' && target.#value !== null) {
42
+ target.#value[prop] = newVal;
43
+ return true;
44
+ }
45
+ return false;
46
+ },
47
+ };
48
+
49
+ return new Proxy(this, handler);
50
+ }
51
+
52
+ valueOf() {
53
+ return this.#value;
54
+ }
55
+ }
56
+
57
+ export default StateWrapper;
@@ -28,7 +28,7 @@ export const ButtonConstructor = {
28
28
  } = {}) {
29
29
  return this.is = button({
30
30
  id: id || GUID(),
31
- class: `${this.class} btn ${className} disabled`,
31
+ class: `${this.class} btn ${className} disabled`.trim(),
32
32
  style: style,
33
33
  ...(type && {type: type}),
34
34
  value: text,
@@ -31,7 +31,7 @@ export const InputConstructor = {
31
31
  } = {}) {
32
32
  return this.is = input({
33
33
  id: id || GUID(),
34
- class: `${this.class} input ${className}`,
34
+ class: `${this.class} input ${className}`.trim(),
35
35
  style: style,
36
36
  ...(type && {type: type}),
37
37
  focus: focus,
@@ -0,0 +1,45 @@
1
+ import {GUID, select} from "aprog";
2
+
3
+ /**
4
+ * AprogJS Constructor
5
+ * --- SelectConstructor ---
6
+ * Copyright (c) 2025 AlexProger.
7
+ */
8
+ export const SelectConstructor = {
9
+ /**
10
+ * create HTMLElement
11
+ */
12
+ constructor() {
13
+ const instance = Object.create(this);
14
+ instance.class = GUID();
15
+ return instance;
16
+ },
17
+
18
+ /**
19
+ * set HTMLElement
20
+ */
21
+ element({
22
+ id = undefined,
23
+ className = "",
24
+ style = {},
25
+ option = undefined,
26
+ options = undefined,
27
+ callback = () => {},
28
+ } = {}) {
29
+ return this.is = select({
30
+ id: id || GUID(),
31
+ class: `${this.class} input ${className}`.trim(),
32
+ style: style,
33
+ ...(option && {option: option}),
34
+ ...(options && {options: options}),
35
+ onChange: callback,
36
+ });
37
+ },
38
+
39
+ /**
40
+ * get target [HTMLElement]
41
+ */
42
+ get target() {
43
+ return this?.is;
44
+ }
45
+ };
package/src/helper.js ADDED
@@ -0,0 +1,6 @@
1
+ import StateWrapper from "./classes/StateWrapper.js";
2
+ import SmartWrapper from "./classes/SmartWrapper.js";
3
+
4
+ export const state = (value) => new StateWrapper(value);
5
+
6
+ export const wrap = (value) => new SmartWrapper(value);
@@ -1,23 +0,0 @@
1
- class AprogClass {
2
- #states = [];
3
- #cursor = 0;
4
-
5
- useState(initialValue) {
6
- const index = this.#cursor;
7
-
8
- if (this.#states[index] === undefined) {
9
- this.#states[index] = initialValue;
10
- }
11
-
12
- const setState = (newValue) => {
13
- const current = this.#states[index];
14
- this.#states[index] = typeof newValue === "function" ? newValue(current) : newValue;
15
- };
16
-
17
- const value = this.#states[index];
18
- this.#cursor++;
19
- return [value, setState];
20
- }
21
- }
22
-
23
- export default AprogClass;