aprog 2.1.7 → 2.1.9
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 +2 -0
- package/package.json +1 -1
- package/src/classes/AprogClass.js +23 -0
- package/src/classes/SmartWrapper.js +40 -0
package/index.js
CHANGED
|
@@ -22,3 +22,5 @@ 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';
|
|
26
|
+
export { default as SmartWrapper } from './src/classes/SmartWrapper.js';
|
package/package.json
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
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;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
class SmartWrapper {
|
|
2
|
+
constructor(value) {
|
|
3
|
+
this._value = value;
|
|
4
|
+
|
|
5
|
+
return new Proxy(this, {
|
|
6
|
+
get: (target, prop) => {
|
|
7
|
+
if (prop === 'val') return () => target._value;
|
|
8
|
+
if (prop === 'isNull') return () => target._value === null || target._value === undefined;
|
|
9
|
+
if (prop === 'toArray') return () => Array.isArray(target._value) ? target._value : Object.values(target._value ?? {});
|
|
10
|
+
if (prop === 'toObject') return () => (typeof target._value === 'object' ? target._value : {});
|
|
11
|
+
if (prop === 'toString') return () => typeof target._value === 'string' ? target._value : String(target._value ?? '');
|
|
12
|
+
if (prop === 'get') {
|
|
13
|
+
// підтримка dot notation
|
|
14
|
+
return (path, def = null) => {
|
|
15
|
+
const keys = path.split('.');
|
|
16
|
+
let current = target._value;
|
|
17
|
+
for (const key of keys) {
|
|
18
|
+
if (current && (typeof current === 'object') && key in current) {
|
|
19
|
+
current = current[key];
|
|
20
|
+
} else {
|
|
21
|
+
return new SmartWrapper(def);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return new SmartWrapper(current);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// доступ до властивості як ланцюг
|
|
29
|
+
if (target._value && typeof target._value === 'object' && prop in target._value) {
|
|
30
|
+
return new SmartWrapper(target._value[prop]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// fallback — undefined
|
|
34
|
+
return new SmartWrapper(undefined);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default SmartWrapper;
|