nucleus-mold 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Иван Семёнов
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # nucleus-mold
2
+
3
+ ## A lightweight and efficient utility for serialization and polymorphic deserialization of complex objects in TypeScript 5.0+
4
+
5
+ ## 💻 Usage Example
6
+ ### 1. Define Your Models
7
+ ```typescript
8
+ import { Nucleus } from './json-mold';
9
+ @Nucleus({ as: 'System.Wallet' })
10
+ export class Wallet {
11
+ public balance: number = 0;
12
+
13
+ // Private constructor is supported!
14
+ private constructor(balance: number) {
15
+ this.balance = balance;
16
+ }
17
+
18
+ public static create(balance: number) {
19
+ return new Wallet(balance);
20
+ }
21
+
22
+ public deposit(amount: number) {
23
+ this.balance += amount;
24
+ }
25
+ }
26
+
27
+ @Nucleus({ as: 'System.User' })
28
+ export class User {
29
+ constructor(
30
+ public id: string,
31
+ public name: string,
32
+ public wallet: Wallet // Nested @Nucleus object
33
+ ) {}
34
+
35
+ public greet() {
36
+ return `Hello, I'm ${this.name}. Balance: ${this.wallet.balance}$`;
37
+ }
38
+ }
39
+ ```
40
+
41
+ ### 2. Serialize and Deserialize
42
+ ```typescript
43
+ import { JsonMold, isSerializable } from './json-mold';
44
+ import { User, Wallet } from './models';
45
+
46
+ const wallet = Wallet.create(250);
47
+ const user = new User("usr_100", "Alex", wallet);
48
+
49
+ console.log(isSerializable(user)); // true
50
+
51
+ // --- CONCEAL (Serialize) ---
52
+ const jsonStr = JsonMold.conceal(user);
53
+ console.log(jsonStr);
54
+ /*
55
+ Output:
56
+ {
57
+ "__type": "System.User",
58
+ "id": "usr_100",
59
+ "name": "Alex",
60
+ "wallet": {
61
+ "__type": "System.Wallet",
62
+ "balance": 250
63
+ }
64
+ }
65
+ */
66
+
67
+ // --- REVEAL (Deserialize) ---
68
+ const restoredUser = JsonMold.reveal<User>(jsonStr);
69
+
70
+ // It recovers the exact prototype chains without invoking constructors
71
+ console.log(restoredUser instanceof User); // true
72
+ console.log(restoredUser.wallet instanceof Wallet); // true
73
+
74
+ // Methods and 'this' context are fully functional
75
+ restoredUser.wallet.deposit(50);
76
+ console.log(restoredUser.greet()); // "Hello, I'm Alex. Balance: 300$"
77
+ ```
78
+
79
+ ## 🛠️ API Reference
80
+
81
+ ### `@Nucleus(options?: { as?: string })`
82
+ A class decorator that registers the target class in the registry and injects the `toJSON` method.
83
+ * `options.as` *(optional)*: A unique string identifier to prevent name collisions across different modules.
84
+
85
+ ### `JsonMold.conceal(obj: any, space?: number): string`
86
+ Converts a domain object into a JSON string. Automatically attaches the `__type` meta key to any nested `@Nucleus` instances.
87
+
88
+ ### `JsonMold.reveal<T = any>(json: string): T`
89
+ Parses a JSON string and recursively rebuilds instances of registered classes by their `__type` token using `Object.create`.
90
+
91
+ ### `isSerializable(obj: any): boolean`
92
+ A helper function that returns `true` if the passed object comes from a class decorated with `@Nucleus`.
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "nucleus-mold",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/M2K-5F/nucleus-mold.git"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "bugs": {
17
+ "url": "https://github.com/M2K-5F/nucleus-mold/issues"
18
+ },
19
+ "homepage": "https://github.com/M2K-5F/nucleus-mold#readme"
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,65 @@
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
+ }