@creejs/commons-lang 1.0.7 → 1.0.8
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/lib/class-proxy-utils.js +66 -0
- package/lib/index.js +6 -2
- package/lib/instance-proxy-utils.js +27 -0
- package/package.json +1 -1
- package/types/class-proxy-utils.d.ts +22 -0
- package/types/index.d.ts +3 -1
- package/types/instance-proxy-utils.d.ts +13 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { isPlainObject } = require('./type-utils')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @module ClassProxyUtils
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a Proxy Class for given class.
|
|
11
|
+
* @template {object} T
|
|
12
|
+
* @param {Function} cls - The class to proxycls - The class to proxy
|
|
13
|
+
* @param {ProxyHandler<T>} [propertyHandler = {}] - Proxy Property Handler
|
|
14
|
+
* @param {boolean} [sealed=true] - Whether to seal the instance
|
|
15
|
+
* @returns {Function} A proxied classA proxied instance of the class
|
|
16
|
+
*/
|
|
17
|
+
function proxy (cls, propertyHandler, sealed = true) {
|
|
18
|
+
if (typeof cls !== 'function') {
|
|
19
|
+
throw new TypeError(`Not Class: type=${typeof cls}, value=${JSON.stringify(cls)}`)
|
|
20
|
+
}
|
|
21
|
+
if (propertyHandler != null) {
|
|
22
|
+
if (!isPlainObject(propertyHandler)) {
|
|
23
|
+
throw new TypeError(`Not PropertyHandler: type=${typeof propertyHandler}, value=${JSON.stringify(propertyHandler)}`)
|
|
24
|
+
}
|
|
25
|
+
const { get, set } = propertyHandler
|
|
26
|
+
if (get != null && typeof get !== 'function') {
|
|
27
|
+
throw new TypeError(`Not PropertyHandler.get: type=${typeof get}, value=${JSON.stringify(get)}`)
|
|
28
|
+
}
|
|
29
|
+
if (set != null && typeof set !== 'function') {
|
|
30
|
+
throw new TypeError(`Not PropertyHandler.set: type=${typeof set}, value=${JSON.stringify(set)}`)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const construcHandler = {
|
|
34
|
+
/**
|
|
35
|
+
* Creates a proxied instance of a class, optionally sealing it.
|
|
36
|
+
* @param {Function} constructor - The class constructor to instantiate.
|
|
37
|
+
* @param {any[]} args - Arguments to pass to the constructor.
|
|
38
|
+
* @param {Function} [newTarget] - The constructor that was originally called by `new`.
|
|
39
|
+
* @returns {Proxy} A proxied instance of the constructed class.
|
|
40
|
+
*/
|
|
41
|
+
construct (constructor, args, newTarget) {
|
|
42
|
+
const clsInstance = Reflect.construct(constructor, args)
|
|
43
|
+
return new Proxy(sealed ? Object.preventExtensions(clsInstance) : clsInstance, propertyHandler ?? {})
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return new Proxy(cls, construcHandler)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Creates a proxied instance of a class with custom property handling.
|
|
51
|
+
* @template {object} T
|
|
52
|
+
* @param {Function} cls - The class to proxy
|
|
53
|
+
* @param {any[]} args - Arguments to pass to the constructor
|
|
54
|
+
* @param {ProxyHandler<T>} propertyHandler - Handler for property access/mutation
|
|
55
|
+
* @param {boolean} [sealed=true] - Whether the proxy should be sealed
|
|
56
|
+
* @returns {T} Proxied class instance
|
|
57
|
+
*/
|
|
58
|
+
function newProxyInstance (cls, args, propertyHandler, sealed = true) {
|
|
59
|
+
const proxyCls = proxy(cls, propertyHandler, sealed)
|
|
60
|
+
return Reflect.construct(proxyCls, args ?? [])
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = {
|
|
64
|
+
proxy,
|
|
65
|
+
newProxyInstance
|
|
66
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -11,6 +11,8 @@ const TypeUtils = require('./type-utils')
|
|
|
11
11
|
const TypeAssert = require('./type-assert')
|
|
12
12
|
const ExecUtils = require('./exec-utils')
|
|
13
13
|
const PromiseUtils = require('./promise-utils')
|
|
14
|
+
const ClassProxyUtils = require('./class-proxy-utils')
|
|
15
|
+
const InstanceProxyUtils = require('./instance-proxy-utils')
|
|
14
16
|
|
|
15
17
|
module.exports = {
|
|
16
18
|
LangUtils,
|
|
@@ -22,5 +24,7 @@ module.exports = {
|
|
|
22
24
|
Lang: LangUtils,
|
|
23
25
|
String: StringUtils,
|
|
24
26
|
Type: TypeUtils,
|
|
25
|
-
Exec: ExecUtils
|
|
26
|
-
|
|
27
|
+
Exec: ExecUtils,
|
|
28
|
+
ClassProxyUtils,
|
|
29
|
+
InstanceProxyUtils
|
|
30
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { isObject, isNil, isArray } = require('./type-utils')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @module InstanceProxyUtils
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a proxy wrapper around an object instance with optional property handler.
|
|
11
|
+
* @template {object} T
|
|
12
|
+
* @param {T} instance - The target object to proxy
|
|
13
|
+
* @param {ProxyHandler<T>} [propertyHandler] - Optional proxy handler for property access
|
|
14
|
+
* @param {boolean} [sealed=true] - Whether to prevent extensions on the target object
|
|
15
|
+
* @returns {T} The proxied object instance
|
|
16
|
+
* @throws {TypeError} If instance is not an object
|
|
17
|
+
*/
|
|
18
|
+
function proxy (instance, propertyHandler, sealed = true) {
|
|
19
|
+
if (isNil(instance) || !isObject(instance) || isArray(instance)) {
|
|
20
|
+
throw new TypeError(`Not Object: type=${typeof instance}, value=${JSON.stringify(instance)}`)
|
|
21
|
+
}
|
|
22
|
+
return new Proxy(sealed ? Object.preventExtensions(instance) : instance, propertyHandler ?? {})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
proxy
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module ClassProxyUtils
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Proxy Class for given class.
|
|
6
|
+
* @template {object} T
|
|
7
|
+
* @param {Function} cls - The class to proxycls - The class to proxy
|
|
8
|
+
* @param {ProxyHandler<T>} [propertyHandler = {}] - Proxy Property Handler
|
|
9
|
+
* @param {boolean} [sealed=true] - Whether to seal the instance
|
|
10
|
+
* @returns {Function} A proxied classA proxied instance of the class
|
|
11
|
+
*/
|
|
12
|
+
export function proxy<T extends object>(cls: Function, propertyHandler?: ProxyHandler<T>, sealed?: boolean): Function;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a proxied instance of a class with custom property handling.
|
|
15
|
+
* @template {object} T
|
|
16
|
+
* @param {Function} cls - The class to proxy
|
|
17
|
+
* @param {any[]} args - Arguments to pass to the constructor
|
|
18
|
+
* @param {ProxyHandler<T>} propertyHandler - Handler for property access/mutation
|
|
19
|
+
* @param {boolean} [sealed=true] - Whether the proxy should be sealed
|
|
20
|
+
* @returns {T} Proxied class instance
|
|
21
|
+
*/
|
|
22
|
+
export function newProxyInstance<T extends object>(cls: Function, args: any[], propertyHandler: ProxyHandler<T>, sealed?: boolean): T;
|
package/types/index.d.ts
CHANGED
|
@@ -4,4 +4,6 @@ import TypeUtils = require("./type-utils");
|
|
|
4
4
|
import TypeAssert = require("./type-assert");
|
|
5
5
|
import ExecUtils = require("./exec-utils");
|
|
6
6
|
import PromiseUtils = require("./promise-utils");
|
|
7
|
-
|
|
7
|
+
import ClassProxyUtils = require("./class-proxy-utils");
|
|
8
|
+
import InstanceProxyUtils = require("./instance-proxy-utils");
|
|
9
|
+
export { LangUtils, StringUtils, TypeUtils, TypeAssert, ExecUtils, PromiseUtils, LangUtils as Lang, StringUtils as String, TypeUtils as Type, ExecUtils as Exec, ClassProxyUtils, InstanceProxyUtils };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module InstanceProxyUtils
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Creates a proxy wrapper around an object instance with optional property handler.
|
|
6
|
+
* @template {object} T
|
|
7
|
+
* @param {T} instance - The target object to proxy
|
|
8
|
+
* @param {ProxyHandler<T>} [propertyHandler] - Optional proxy handler for property access
|
|
9
|
+
* @param {boolean} [sealed=true] - Whether to prevent extensions on the target object
|
|
10
|
+
* @returns {T} The proxied object instance
|
|
11
|
+
* @throws {TypeError} If instance is not an object
|
|
12
|
+
*/
|
|
13
|
+
export function proxy<T extends object>(instance: T, propertyHandler?: ProxyHandler<T>, sealed?: boolean): T;
|