nucleus-mold 1.0.0 → 1.0.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.
- package/package.json +16 -9
- package/src/index.ts +0 -65
package/package.json
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nucleus-mold",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"
|
|
5
|
-
"main": "index.js",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"files": ["dist", "README.md"],
|
|
6
5
|
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
6
|
},
|
|
9
7
|
"repository": {
|
|
10
8
|
"type": "git",
|
|
11
|
-
"url": "git
|
|
9
|
+
"url": "git://github.com/M2K-5F/nucleus-mold.git"
|
|
12
10
|
},
|
|
13
|
-
"keywords": [
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
"keywords": [
|
|
12
|
+
"typescript"
|
|
13
|
+
],
|
|
14
|
+
"author": "M2K-5F",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"homepage": "https://github.com/M2K-5F/nucleus-mold",
|
|
16
17
|
"bugs": {
|
|
17
18
|
"url": "https://github.com/M2K-5F/nucleus-mold/issues"
|
|
18
19
|
},
|
|
19
|
-
"
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"require": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
20
27
|
}
|
package/src/index.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
const SERIALIZABLE_MARKER = Symbol('serializable')
|
|
2
|
-
const TYPE_KEY = '__type'
|
|
3
|
-
|
|
4
|
-
const registry = new Map<string, Function>()
|
|
5
|
-
|
|
6
|
-
export function Nucleus(options?: { as?: string }) {
|
|
7
|
-
return function<T extends Function>(
|
|
8
|
-
target: T,
|
|
9
|
-
context: ClassDecoratorContext<any>
|
|
10
|
-
): void {
|
|
11
|
-
const className = options?.as || context?.name || target.name
|
|
12
|
-
registry.set(className, target)
|
|
13
|
-
|
|
14
|
-
const proto = (target as any).prototype
|
|
15
|
-
|
|
16
|
-
Object.defineProperty(proto, SERIALIZABLE_MARKER, {
|
|
17
|
-
value: true,
|
|
18
|
-
writable: false,
|
|
19
|
-
enumerable: false,
|
|
20
|
-
configurable: true
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
proto.toJSON = function () {
|
|
24
|
-
return {
|
|
25
|
-
[TYPE_KEY]: className,
|
|
26
|
-
...Object.fromEntries(
|
|
27
|
-
Object.getOwnPropertyNames(this).map(key => [key, (this as any)[key]])
|
|
28
|
-
)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export const JsonMold = {
|
|
36
|
-
conceal: (obj: any, space?: number) => {
|
|
37
|
-
return JSON.stringify(obj, null, space)
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
reveal: <T = any>(json: string): T => {
|
|
41
|
-
return JSON.parse(json, (key, value) => {
|
|
42
|
-
if (value && typeof value === 'object' && value[TYPE_KEY]) {
|
|
43
|
-
const TargetClass = registry.get(value[TYPE_KEY])
|
|
44
|
-
|
|
45
|
-
if (TargetClass) {
|
|
46
|
-
const instance = Object.create(TargetClass.prototype)
|
|
47
|
-
|
|
48
|
-
for (const [k, v] of Object.entries(value)) {
|
|
49
|
-
if (k !== TYPE_KEY) {
|
|
50
|
-
(instance as any)[k] = v
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return instance
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return value
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function isSerializable(obj: any): boolean {
|
|
64
|
-
return obj && obj[SERIALIZABLE_MARKER] === true
|
|
65
|
-
}
|