nucleus-mold 1.0.1 → 1.0.3

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 Serializable(options?: {
2
+ as?: string;
3
+ }): <T extends Function>(target: T, context: ClassDecoratorContext<any>) => void;
4
+ export declare const Json: {
5
+ marshall: (obj: any, space?: number) => string;
6
+ unmarshall: <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,YAAY,CAAC,OAAO,CAAC,EAAE;IAAE,EAAE,CAAC,EAAE,MAAM,CAAA;CAAE,IAClC,CAAC,SAAS,QAAQ,EAC9B,QAAQ,CAAC,EACT,SAAS,qBAAqB,CAAC,GAAG,CAAC,KACpC,IAAI,CAsBV;AAGD,eAAO,MAAM,IAAI;oBACG,GAAG,UAAU,MAAM;iBAItB,CAAC,cAAc,MAAM,KAAG,CAAC;CAqBzC,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.Json = void 0;
4
+ exports.Serializable = Serializable;
5
+ exports.isSerializable = isSerializable;
6
+ const SERIALIZABLE_MARKER = Symbol('serializable');
7
+ const TYPE_KEY = '__type';
8
+ const registry = new Map();
9
+ function Serializable(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.Json = {
29
+ marshall: (obj, space) => {
30
+ return JSON.stringify(obj, null, space);
31
+ },
32
+ unmarshall: (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,8 +1,13 @@
1
1
  {
2
2
  "name": "nucleus-mold",
3
- "version": "1.0.1",
4
- "files": ["dist", "README.md"],
3
+ "version": "1.0.3",
4
+ "files": [
5
+ "dist",
6
+ "README.md"
7
+ ],
5
8
  "scripts": {
9
+ "build": "tsc",
10
+ "prepublishOnly": "npm run build"
6
11
  },
7
12
  "repository": {
8
13
  "type": "git",
@@ -23,5 +28,10 @@
23
28
  "require": "./dist/index.js",
24
29
  "types": "./dist/index.d.ts"
25
30
  }
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^25.6.2",
34
+ "tsx": "^4.21.0",
35
+ "typescript": "^5.0.0"
26
36
  }
27
37
  }