nucleus-mold 1.0.0 → 1.0.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.
@@ -0,0 +1,9 @@
1
+ export declare function Nucleus(options?: {
2
+ as?: string;
3
+ }): <T extends Function>(target: T, context: ClassDecoratorContext<any>) => void;
4
+ export declare const JsonMold: {
5
+ conceal: (obj: any, space?: number) => string;
6
+ reveal: <T = any>(json: string) => T;
7
+ };
8
+ export declare function isSerializable(obj: any): boolean;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,wBAAgB,OAAO,CAAC,OAAO,CAAC,EAAE;IAAE,EAAE,CAAC,EAAE,MAAM,CAAA;CAAE,IAC7B,CAAC,SAAS,QAAQ,EAC9B,QAAQ,CAAC,EACT,SAAS,qBAAqB,CAAC,GAAG,CAAC,KACpC,IAAI,CAsBV;AAGD,eAAO,MAAM,QAAQ;mBACF,GAAG,UAAU,MAAM;aAIzB,CAAC,cAAc,MAAM,KAAG,CAAC;CAqBrC,CAAA;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAEhD"}
package/dist/index.js ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonMold = void 0;
4
+ exports.Nucleus = Nucleus;
5
+ exports.isSerializable = isSerializable;
6
+ const SERIALIZABLE_MARKER = Symbol('serializable');
7
+ const TYPE_KEY = '__type';
8
+ const registry = new Map();
9
+ function Nucleus(options) {
10
+ return function (target, context) {
11
+ const className = options?.as || context?.name || target.name;
12
+ registry.set(className, target);
13
+ const proto = target.prototype;
14
+ Object.defineProperty(proto, SERIALIZABLE_MARKER, {
15
+ value: true,
16
+ writable: false,
17
+ enumerable: false,
18
+ configurable: true
19
+ });
20
+ proto.toJSON = function () {
21
+ return {
22
+ [TYPE_KEY]: className,
23
+ ...Object.fromEntries(Object.getOwnPropertyNames(this).map(key => [key, this[key]]))
24
+ };
25
+ };
26
+ };
27
+ }
28
+ exports.JsonMold = {
29
+ conceal: (obj, space) => {
30
+ return JSON.stringify(obj, null, space);
31
+ },
32
+ reveal: (json) => {
33
+ return JSON.parse(json, (key, value) => {
34
+ if (value && typeof value === 'object' && value[TYPE_KEY]) {
35
+ const TargetClass = registry.get(value[TYPE_KEY]);
36
+ if (TargetClass) {
37
+ const instance = Object.create(TargetClass.prototype);
38
+ for (const [k, v] of Object.entries(value)) {
39
+ if (k !== TYPE_KEY) {
40
+ instance[k] = v;
41
+ }
42
+ }
43
+ return instance;
44
+ }
45
+ }
46
+ return value;
47
+ });
48
+ }
49
+ };
50
+ function isSerializable(obj) {
51
+ return obj && obj[SERIALIZABLE_MARKER] === true;
52
+ }
package/package.json CHANGED
@@ -1,20 +1,37 @@
1
1
  {
2
2
  "name": "nucleus-mold",
3
- "version": "1.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "1.0.2",
4
+ "files": [
5
+ "dist",
6
+ "README.md"
7
+ ],
6
8
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
9
+ "build": "tsc",
10
+ "prepublishOnly": "npm run build"
8
11
  },
9
12
  "repository": {
10
13
  "type": "git",
11
- "url": "git+https://github.com/M2K-5F/nucleus-mold.git"
14
+ "url": "git://github.com/M2K-5F/nucleus-mold.git"
12
15
  },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
+ "keywords": [
17
+ "typescript"
18
+ ],
19
+ "author": "M2K-5F",
20
+ "license": "MIT",
21
+ "homepage": "https://github.com/M2K-5F/nucleus-mold",
16
22
  "bugs": {
17
23
  "url": "https://github.com/M2K-5F/nucleus-mold/issues"
18
24
  },
19
- "homepage": "https://github.com/M2K-5F/nucleus-mold#readme"
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/index.js",
28
+ "require": "./dist/index.js",
29
+ "types": "./dist/index.d.ts"
30
+ }
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^25.6.2",
34
+ "tsx": "^4.21.0",
35
+ "typescript": "^5.0.0"
36
+ }
20
37
  }
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
- }