protoobject 0.0.1 → 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.
Files changed (35) hide show
  1. package/README.md +248 -2
  2. package/lib/classes/proto-object.d.ts +143 -0
  3. package/lib/classes/proto-object.js +268 -0
  4. package/lib/classes/proto-object.js.map +1 -0
  5. package/lib/decorators/static-implements.d.ts +6 -0
  6. package/lib/decorators/static-implements.js +16 -0
  7. package/lib/decorators/static-implements.js.map +1 -0
  8. package/lib/index.d.ts +9 -6
  9. package/lib/index.js +11 -5
  10. package/lib/index.js.map +1 -1
  11. package/lib/types/any-object.d.ts +6 -0
  12. package/lib/types/any-object.js +3 -0
  13. package/lib/types/any-object.js.map +1 -0
  14. package/lib/types/collection-transformer.d.ts +21 -0
  15. package/lib/types/collection-transformer.js +4 -0
  16. package/lib/types/collection-transformer.js.map +1 -0
  17. package/lib/types/dynamic-methods.d.ts +31 -0
  18. package/lib/types/dynamic-methods.js +4 -0
  19. package/lib/types/dynamic-methods.js.map +1 -0
  20. package/lib/types/record-transformer.d.ts +21 -0
  21. package/lib/types/record-transformer.js +4 -0
  22. package/lib/types/record-transformer.js.map +1 -0
  23. package/lib/types/static-methods.d.ts +109 -0
  24. package/lib/types/static-methods.js +5 -0
  25. package/lib/types/static-methods.js.map +1 -0
  26. package/lib/types/unknown-object.d.ts +6 -0
  27. package/lib/types/unknown-object.js +3 -0
  28. package/lib/types/unknown-object.js.map +1 -0
  29. package/lib/types/validator-function.d.ts +4 -0
  30. package/lib/types/validator-function.js +4 -0
  31. package/lib/types/validator-function.js.map +1 -0
  32. package/lib/utils/protoobject-factory.d.ts +9 -0
  33. package/lib/utils/protoobject-factory.js +35 -0
  34. package/lib/utils/protoobject-factory.js.map +1 -0
  35. package/package.json +18 -7
package/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # ProtoObject
2
2
 
3
- TBD
3
+ A universal class for creating any JSON objects and simple manipulations with them.
4
+
5
+ Just inherit from this class and easily organize your work with data. This can be data storage in an SQL/NoSQL database, data storage in localStorage/sessionStorage, simple transfer of data serialization on one side and the same idle deserialization of data on the other side. You can write your own ProtoBuffer protocol on top of this library and use it for RPC or to write a universal data library for FrontEnd and BackEnd.
6
+
7
+ Think of it as a data framework.
8
+
9
+ Inspired by gRPC and Firebase.
4
10
 
5
11
  [![npm](https://img.shields.io/npm/v/protoobject.svg)](https://www.npmjs.com/package/protoobject)
6
12
  [![npm](https://img.shields.io/npm/dy/protoobject.svg)](https://www.npmjs.com/package/protoobject)
@@ -16,7 +22,247 @@ TBD
16
22
 
17
23
  ## DOCS
18
24
 
19
- TBD
25
+ ### The main methods of the ProtoObject class
26
+
27
+ These methods ensure that the class and its heirs interact with the external system and will not contain backward incompatible changes.
28
+
29
+ | --- type of the property --- | --- name of the property --- | --- description --- |
30
+ | --- | --- | --- |
31
+ | static | `fromJSON` | A method for converting a simple json to ProtoObject class or its heir |
32
+ | dynamic | `toJSON` | A method for converting a ProtoObject class or its heir to simple json |
33
+ | dynamic | `toString` | A method for converting a ProtoObject class or its heir to a string |
34
+ | dynamic | `copy` | Copying a ProtoObject class or its heirs |
35
+ | dynamic | `assign` | Deep assign data to an instance of the ProtoObject class or its heir |
36
+ | static | `recordTransformer` | Factory for creating a data transformer for the ProtoObject class or its heir |
37
+ | static | `collectionTransformer` | Factory for creating a data transformer for the array of ProtoObject classes or its heirs |
38
+
39
+ ### The auxiliary methods of the ProtoObject class
40
+
41
+ These methods ensure the operation of the class itself and can change significantly over time.
42
+
43
+ | --- type of the property --- | --- name of the property --- | --- description --- |
44
+ | --- | --- | --- |
45
+ | static | `getProperties` | Get all properties of an object and its prototypes |
46
+ | static | `getEnumerableProperties` | Get all enumerable properties of an object and its prototypes |
47
+ | static | `recursiveAssign` | A recursive function for assigning properties to an object or returning a property if it is not interchangeable |
48
+ | static | `deepAssign` | Deep assign data to an instance of the ProtoObject class or its heir |
49
+ | static | `valueToJSON` | The converter of values into simple types |
50
+ | static | `valueFromJSON` | The converter of simple types into values |
51
+
52
+ ### JavaScript
53
+
54
+ Creating an heir class
55
+
56
+ #### JavaScript - Creating an heir class using inheritance
57
+
58
+ Note that you cannot use static method validation using a decorator in JavaScript, TypeScript provides more features.
59
+
60
+ Note the call to `this.assign(data);` in the constructor of your own class. This is due to the code build, which causes your class to first call `super(data);` and then apply the value of the properties specified in the class (if the value is not specified, `undefined` will be applied). This is the reason why `super(data);` will not make an assignment for the properties specified in your class.
61
+
62
+ ```js
63
+ class UserAddress extends ProtoObject {
64
+ constructor(data) {
65
+ if (data) this.assign(data);
66
+ return this;
67
+ }
68
+
69
+ country;
70
+
71
+ postCode;
72
+ }
73
+ ```
74
+
75
+ #### JavaScript - Creating an heir class using a factory
76
+
77
+ You can skip fields with standard types `String`, `Number`, `Boolean` and use a superclass converters (`UserRights?.prototype?.toJSON?.call(this)` and `ProtoObject.fromJSON(data)`) for these types, but you must implement the conversion of the remaining types manually.
78
+
79
+ ```js
80
+ const UserRights = protoObjectFactory({
81
+ fromJSON(data) {
82
+ return new this({
83
+ ...ProtoObject.fromJSON(data),
84
+ updatedAt: new Date(data?.updatedAt),
85
+ });
86
+ },
87
+ toJSON() {
88
+ return {
89
+ ...UserRights?.prototype?.toJSON?.call(this),
90
+ updatedAt: this.updatedAt?.toJSON(),
91
+ };
92
+ },
93
+ });
94
+ ```
95
+
96
+ #### JavaScript - Creating an heir class using inheritance with conversion of additional data types
97
+
98
+ Note that you cannot use static method validation using a decorator in JavaScript, TypeScript provides more features.
99
+
100
+ Note the call to `this.assign(data);` in the constructor of your own class. This is due to the code build, which causes your class to first call `super(data);` and then apply the value of the properties specified in the class (if the value is not specified, `undefined` will be applied). This is the reason why `super(data);` will not make an assignment for the properties specified in your class.
101
+
102
+ You can skip fields with standard types `String`, `Number`, `Boolean` and use a superclass converters (`super.toJSON()` and `super.fromJSON(data)`) for these types, but you must implement the conversion of the remaining types manually.
103
+
104
+ ```js
105
+ class User extends ProtoObject {
106
+ constructor(data) {
107
+ super(data);
108
+ if (data) this.assign(data);
109
+ return this;
110
+ }
111
+
112
+ id;
113
+
114
+ email;
115
+
116
+ createdAt;
117
+
118
+ photo;
119
+
120
+ address;
121
+
122
+ toJSON() {
123
+ return {
124
+ ...super.toJSON(),
125
+ createdAt: this.createdAt.toJSON(),
126
+ photo:
127
+ this.photo instanceof Buffer ? this.photo.toString("hex") : undefined,
128
+ address:
129
+ this.address instanceof UserAddress ? this.address.toJSON() : undefined,
130
+ rights:
131
+ this.rights instanceof UserRights ? this.rights?.toJSON() : undefined,
132
+ };
133
+ }
134
+
135
+ static fromJSON(data) {
136
+ return new User({
137
+ ...super.fromJSON(data),
138
+ createdAt:
139
+ typeof data.createdAt === "string"
140
+ ? new Date(data.createdAt)
141
+ : undefined,
142
+ photo:
143
+ typeof data.photo === "string"
144
+ ? Buffer.from(data.photo, "hex")
145
+ : undefined,
146
+ address: data.address ? UserAddress.fromJSON(data.address) : undefined,
147
+ rights: data.rights ? UserRights.fromJSON(data.rights) : undefined,
148
+ });
149
+ }
150
+ }
151
+ ```
152
+
153
+ ### TypeScript
154
+
155
+ Creating an heir class
156
+
157
+ #### TypeScript - Creating an heir class using inheritance
158
+
159
+ Note that to check the static properties of a class, you can use the decorator `@StaticImplements<ProtoObjectStaticMethods<User>>()`.
160
+
161
+ Note the call to `this.assign(data);` in the constructor of your own class. This is due to the code build, which causes your class to first call `super(data);` and then apply the value of the properties specified in the class (if the value is not specified, `undefined` will be applied). This is the reason why `super(data);` will not make an assignment for the properties specified in your class.
162
+
163
+ ```js
164
+ @StaticImplements<ProtoObjectStaticMethods<UserAddress>>()
165
+ export class UserAddress extends ProtoObject<UserAddress> {
166
+ constructor(data?: Partial<UserAddress>) {
167
+ super(data);
168
+ if (data) this.assign(data);
169
+ return this;
170
+ }
171
+
172
+ country!: string;
173
+
174
+ postCode!: string;
175
+ }
176
+ ```
177
+
178
+ #### TypeScript - Creating an heir class using a factory
179
+
180
+ You can skip fields with standard types `String`, `Number`, `Boolean` and use a superclass converters (`UserRights?.prototype?.toJSON?.call(this)` and `ProtoObject.fromJSON(data)`) for these types, but you must implement the conversion of the remaining types manually.
181
+
182
+ ```js
183
+ interface IUserRights extends ProtoObject<IUserRights> {
184
+ isAdmin: boolean;
185
+ updatedAt: Date;
186
+ }
187
+ const UserRights = protoObjectFactory<IUserRights>({
188
+ fromJSON(data) {
189
+ return new this({
190
+ ...ProtoObject.fromJSON(data),
191
+ updatedAt: new Date(data?.updatedAt),
192
+ });
193
+ },
194
+ toJSON() {
195
+ return {
196
+ ...UserRights?.prototype?.toJSON?.call(this),
197
+ updatedAt: this.updatedAt?.toJSON(),
198
+ };
199
+ },
200
+ });
201
+ ```
202
+
203
+ #### TypeScript - Creating an heir class using inheritance with conversion of additional data types
204
+
205
+ Note that to check the static properties of a class, you can use the decorator `@StaticImplements<ProtoObjectStaticMethods<User>>()`.
206
+
207
+ Note the call to `this.assign(data);` in the constructor of your own class. This is due to the code build, which causes your class to first call `super(data);` and then apply the value of the properties specified in the class (if the value is not specified, `undefined` will be applied). This is the reason why `super(data);` will not make an assignment for the properties specified in your class.
208
+
209
+ You can skip fields with standard types `String`, `Number`, `Boolean` and use a superclass converters (`super.toJSON()` and `super.fromJSON(data)`) for these types, but you must implement the conversion of the remaining types manually.
210
+
211
+ ```js
212
+ @StaticImplements<ProtoObjectStaticMethods<User>>()
213
+ class User extends ProtoObject<User> {
214
+ constructor(data?: Partial<User>) {
215
+ super(data);
216
+ if (data) this.assign(data);
217
+ return this;
218
+ }
219
+
220
+ id!: string;
221
+
222
+ email!: string;
223
+
224
+ createdAt!: Date;
225
+
226
+ photo?: Buffer;
227
+
228
+ address?: UserAddress;
229
+
230
+ rights?: IUserRights;
231
+
232
+ public toJSON(): { [key: string]: any } {
233
+ return {
234
+ ...super.toJSON(),
235
+ createdAt: this.createdAt.toJSON(),
236
+ photo:
237
+ this.photo instanceof Buffer ? this.photo.toString("hex") : undefined,
238
+ address:
239
+ this.address instanceof UserAddress ? this.address.toJSON() : undefined,
240
+ rights:
241
+ this.rights instanceof UserRights ? this.rights?.toJSON() : undefined,
242
+ };
243
+ }
244
+
245
+ public static fromJSON<User>(data: { [key: string]: unknown }): User {
246
+ return new User({
247
+ ...(super.fromJSON<any>(data) as User),
248
+ createdAt:
249
+ typeof data.createdAt === "string"
250
+ ? new Date(data.createdAt)
251
+ : undefined,
252
+ photo:
253
+ typeof data.photo === "string"
254
+ ? Buffer.from(data.photo, "hex")
255
+ : undefined,
256
+ address: data.address
257
+ ? UserAddress.fromJSON<UserAddress>(
258
+ data.address as { [key: string]: unknown }
259
+ )
260
+ : undefined,
261
+ rights: data.rights ? UserRights.fromJSON(data.rights) : undefined,
262
+ }) as unknown as User;
263
+ }
264
+ }
265
+ ```
20
266
 
21
267
  ## LICENSE
22
268
 
@@ -0,0 +1,143 @@
1
+ import { UnknownObject } from "../types/unknown-object";
2
+ import { ProtoObjectDynamicMethods } from "../types/dynamic-methods";
3
+ import { ValidatorFunction } from "../types/validator-function";
4
+ /**
5
+ * A universal class for creating any JSON objects and simple manipulations with them.
6
+ */
7
+ export declare class ProtoObject<T extends ProtoObjectDynamicMethods<T>> implements ProtoObjectDynamicMethods<T> {
8
+ /**
9
+ *
10
+ * @param data - ProtoObject or its heir properties
11
+ * @returns - the ProtoObject or its heir
12
+ */
13
+ constructor(data?: Partial<T>);
14
+ /**
15
+ * Get all properties of an object and its prototypes
16
+ *
17
+ * @param data - any non-null object, such as an instance of the ProtoObject class or its heir
18
+ * @returns - an array of the properties information
19
+ */
20
+ static getProperties(data: {
21
+ [key: PropertyKey]: unknown;
22
+ }): {
23
+ key: PropertyKey;
24
+ value: unknown;
25
+ descriptor: PropertyDescriptor;
26
+ }[];
27
+ /**
28
+ * Get all enumerable properties of an object and its prototypes
29
+ *
30
+ * @param data - any non-null object, such as an instance of the ProtoObject class or its heir
31
+ * @param options - options for the returned array of properties
32
+ * @param options.onlyWritable - return only writable properties
33
+ * @returns - an array of the properties information
34
+ */
35
+ static getEnumerableProperties<T extends ProtoObjectDynamicMethods<T>>(data: {
36
+ [key: PropertyKey]: unknown;
37
+ }, options?: {
38
+ onlyWritable: boolean;
39
+ }): {
40
+ key: PropertyKey;
41
+ value: unknown;
42
+ descriptor: PropertyDescriptor;
43
+ }[];
44
+ /**
45
+ * A recursive function for assigning properties to an object or returning a property
46
+ * if it is not interchangeable
47
+ *
48
+ * WARN: The first transferred object will be changed.
49
+ *
50
+ * @param obj - an object such as a ProtoObject or its heir or an object property
51
+ * @param data - an object such as a ProtoObject or its heir or an object property
52
+ * @returns - an object such as a ProtoObject or its heir or an object property
53
+ */
54
+ static recursiveAssign<T extends ProtoObjectDynamicMethods<T>, K>(obj: unknown, data?: unknown): K;
55
+ /**
56
+ * Deep assign data to an instance of the ProtoObject class or its heir
57
+ *
58
+ * @param obj - a ProtoObject class or its heir
59
+ * @param data - a ProtoObject class or its heir or any other object
60
+ * @returns - a assigned ProtoObject class or its heir
61
+ */
62
+ static deepAssign<T extends ProtoObjectDynamicMethods<T>>(obj: T, data?: Partial<T>): T;
63
+ /**
64
+ * The converter of values into simple types
65
+ *
66
+ * @param data - a value to convert to a simple type
67
+ * @returns - a simple type
68
+ */
69
+ static valueToJSON(data: unknown): unknown;
70
+ /**
71
+ * The converter of simple types into values
72
+ *
73
+ * @param data - a simple type to convert to a value
74
+ * @returns - a value
75
+ */
76
+ static valueFromJSON(data: unknown): unknown;
77
+ /**
78
+ * A method for converting a simple json to ProtoObject class or its heir
79
+ *
80
+ * @param data - a simple json data
81
+ * @returns - a ProtoObject class or its heir
82
+ */
83
+ static fromJSON<T extends ProtoObjectDynamicMethods<T>>(data: {
84
+ [key: string]: unknown;
85
+ }): T;
86
+ /**
87
+ * A method for converting a ProtoObject class or its heir to simple json
88
+ *
89
+ * @returns - a simple json
90
+ */
91
+ toJSON(): {
92
+ [key: string]: any;
93
+ };
94
+ /**
95
+ * A method for converting a ProtoObject class or its heir to a string
96
+ *
97
+ * @returns - string
98
+ */
99
+ toString(): string;
100
+ /**
101
+ * Copying a ProtoObject class or its heirs
102
+ *
103
+ * @returns - a deep copy of the ProtoObject object or its heir
104
+ */
105
+ copy(): T;
106
+ /**
107
+ * Deep assign data to an instance of the ProtoObject class or its heir
108
+ *
109
+ * @param data - a ProtoObject class or its heir or any other object
110
+ * @returns - a assigned ProtoObject class or its heir
111
+ */
112
+ assign(data: Partial<T>): T;
113
+ /**
114
+ * Factory for creating a data transformer for the ProtoObject class or its heir
115
+ *
116
+ * @param param0 - data validators
117
+ * @param param0.validatorTo - data validator when converting to a simple JSON object
118
+ * @param param0.validatorFrom - data validator when converting to a class
119
+ * @returns data transformer for the ProtoObject class or its heir
120
+ */
121
+ static recordTransformer<T extends ProtoObjectDynamicMethods<T>>({ validatorTo, validatorFrom, }: {
122
+ validatorTo?: ValidatorFunction<T>;
123
+ validatorFrom?: ValidatorFunction<UnknownObject>;
124
+ }): {
125
+ to(obj: unknown): UnknownObject | undefined;
126
+ from(json: unknown): T | undefined;
127
+ };
128
+ /**
129
+ * Factory for creating a data transformer for the array of ProtoObject classes or its heirs
130
+ *
131
+ * @param param0 - data validators
132
+ * @param param0.validatorTo - data validator when converting to a simple JSON object
133
+ * @param param0.validatorFrom - data validator when converting to a class
134
+ * @returns data transformer for the array of the ProtoObject classes or its heirs
135
+ */
136
+ static collectionTransformer<T extends ProtoObjectDynamicMethods<T>>({ validatorTo, validatorFrom, }: {
137
+ validatorTo?: ValidatorFunction<T>;
138
+ validatorFrom?: ValidatorFunction<UnknownObject>;
139
+ }): {
140
+ to(objArr: unknown): UnknownObject[] | undefined;
141
+ from(jsonArr: unknown): T[] | undefined;
142
+ };
143
+ }
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProtoObject = void 0;
4
+ /**
5
+ * A universal class for creating any JSON objects and simple manipulations with them.
6
+ */
7
+ class ProtoObject {
8
+ /**
9
+ *
10
+ * @param data - ProtoObject or its heir properties
11
+ * @returns - the ProtoObject or its heir
12
+ */
13
+ constructor(data) {
14
+ return ProtoObject.deepAssign(this, data ?? {});
15
+ }
16
+ /**
17
+ * Get all properties of an object and its prototypes
18
+ *
19
+ * @param data - any non-null object, such as an instance of the ProtoObject class or its heir
20
+ * @returns - an array of the properties information
21
+ */
22
+ static getProperties(data) {
23
+ const props = [];
24
+ // eslint-disable-next-line guard-for-in
25
+ for (const key in data) {
26
+ const prop = Object.getOwnPropertyDescriptor(data, key);
27
+ if (prop)
28
+ props.push({ key, value: prop?.value, descriptor: prop });
29
+ }
30
+ return props;
31
+ }
32
+ /**
33
+ * Get all enumerable properties of an object and its prototypes
34
+ *
35
+ * @param data - any non-null object, such as an instance of the ProtoObject class or its heir
36
+ * @param options - options for the returned array of properties
37
+ * @param options.onlyWritable - return only writable properties
38
+ * @returns - an array of the properties information
39
+ */
40
+ static getEnumerableProperties(data, options = { onlyWritable: false }) {
41
+ const classNode = this;
42
+ return classNode
43
+ .getProperties(data)
44
+ .filter((prop) => !prop.descriptor.get &&
45
+ !prop.descriptor.set &&
46
+ prop.descriptor.enumerable)
47
+ .filter((prop) => options?.onlyWritable ? prop.descriptor.writable : true);
48
+ }
49
+ /**
50
+ * A recursive function for assigning properties to an object or returning a property
51
+ * if it is not interchangeable
52
+ *
53
+ * WARN: The first transferred object will be changed.
54
+ *
55
+ * @param obj - an object such as a ProtoObject or its heir or an object property
56
+ * @param data - an object such as a ProtoObject or its heir or an object property
57
+ * @returns - an object such as a ProtoObject or its heir or an object property
58
+ */
59
+ static recursiveAssign(obj, data) {
60
+ const classNode = this;
61
+ if (typeof obj === "undefined" ||
62
+ obj === null ||
63
+ (typeof data !== "undefined" && typeof obj !== typeof data))
64
+ return data;
65
+ if (data === null)
66
+ return data;
67
+ switch (typeof data) {
68
+ case "undefined":
69
+ return obj;
70
+ case "bigint":
71
+ case "boolean":
72
+ case "function":
73
+ case "number":
74
+ case "string":
75
+ case "symbol":
76
+ return data;
77
+ case "object": {
78
+ if (Array.isArray(data)) {
79
+ return data;
80
+ }
81
+ if (data instanceof ProtoObject &&
82
+ data?.constructor?.name !== obj?.constructor?.name) {
83
+ return data.copy();
84
+ }
85
+ if (!(data instanceof ProtoObject) &&
86
+ typeof data.constructor === "function" &&
87
+ data.constructor?.name !== "Object") {
88
+ return data;
89
+ }
90
+ for (const prop of classNode.getEnumerableProperties(data)) {
91
+ if (typeof prop.key !== "string")
92
+ continue;
93
+ const origProp = Object.getOwnPropertyDescriptor(obj, prop.key);
94
+ if (!origProp ||
95
+ (!origProp.get &&
96
+ !origProp.set &&
97
+ origProp.enumerable &&
98
+ origProp.writable)) {
99
+ obj[prop.key] = classNode.recursiveAssign(obj[prop.key], data[prop.key]);
100
+ }
101
+ else {
102
+ continue;
103
+ }
104
+ }
105
+ return obj;
106
+ }
107
+ }
108
+ return obj;
109
+ }
110
+ /**
111
+ * Deep assign data to an instance of the ProtoObject class or its heir
112
+ *
113
+ * @param obj - a ProtoObject class or its heir
114
+ * @param data - a ProtoObject class or its heir or any other object
115
+ * @returns - a assigned ProtoObject class or its heir
116
+ */
117
+ static deepAssign(obj, data) {
118
+ const classNode = this;
119
+ return classNode.recursiveAssign(obj, data ?? {});
120
+ }
121
+ /**
122
+ * The converter of values into simple types
123
+ *
124
+ * @param data - a value to convert to a simple type
125
+ * @returns - a simple type
126
+ */
127
+ static valueToJSON(data) {
128
+ switch (typeof data) {
129
+ case "boolean":
130
+ case "number":
131
+ case "string":
132
+ return data;
133
+ default:
134
+ return undefined;
135
+ }
136
+ }
137
+ /**
138
+ * The converter of simple types into values
139
+ *
140
+ * @param data - a simple type to convert to a value
141
+ * @returns - a value
142
+ */
143
+ static valueFromJSON(data) {
144
+ switch (typeof data) {
145
+ case "boolean":
146
+ case "number":
147
+ case "string":
148
+ return data;
149
+ default:
150
+ return undefined;
151
+ }
152
+ }
153
+ /**
154
+ * A method for converting a simple json to ProtoObject class or its heir
155
+ *
156
+ * @param data - a simple json data
157
+ * @returns - a ProtoObject class or its heir
158
+ */
159
+ static fromJSON(data) {
160
+ const classNode = this;
161
+ const json = {};
162
+ // eslint-disable-next-line guard-for-in
163
+ for (const key in data) {
164
+ const value = classNode.valueFromJSON(data[key]);
165
+ if (value)
166
+ json[key] = value;
167
+ }
168
+ return new classNode(json);
169
+ }
170
+ /**
171
+ * A method for converting a ProtoObject class or its heir to simple json
172
+ *
173
+ * @returns - a simple json
174
+ */
175
+ toJSON() {
176
+ const classNode = this
177
+ .constructor;
178
+ const json = {};
179
+ const props = ProtoObject.getEnumerableProperties(this);
180
+ for (const prop of props) {
181
+ const value = classNode.valueToJSON(prop.value);
182
+ if (value)
183
+ json[prop.key] = value;
184
+ }
185
+ return json;
186
+ }
187
+ /**
188
+ * A method for converting a ProtoObject class or its heir to a string
189
+ *
190
+ * @returns - string
191
+ */
192
+ toString() {
193
+ return JSON.stringify(this.toJSON());
194
+ }
195
+ /**
196
+ * Copying a ProtoObject class or its heirs
197
+ *
198
+ * @returns - a deep copy of the ProtoObject object or its heir
199
+ */
200
+ copy() {
201
+ const classNode = this.constructor;
202
+ return classNode.fromJSON(this.toJSON());
203
+ }
204
+ /**
205
+ * Deep assign data to an instance of the ProtoObject class or its heir
206
+ *
207
+ * @param data - a ProtoObject class or its heir or any other object
208
+ * @returns - a assigned ProtoObject class or its heir
209
+ */
210
+ assign(data) {
211
+ const classNode = this.constructor;
212
+ return classNode.deepAssign(this, data);
213
+ }
214
+ /**
215
+ * Factory for creating a data transformer for the ProtoObject class or its heir
216
+ *
217
+ * @param param0 - data validators
218
+ * @param param0.validatorTo - data validator when converting to a simple JSON object
219
+ * @param param0.validatorFrom - data validator when converting to a class
220
+ * @returns data transformer for the ProtoObject class or its heir
221
+ */
222
+ static recordTransformer({ validatorTo, validatorFrom, }) {
223
+ const classNode = this;
224
+ return {
225
+ to(obj) {
226
+ if (!obj || (typeof validatorTo === "function" && !validatorTo(obj)))
227
+ return undefined;
228
+ return obj.toJSON();
229
+ },
230
+ from(json) {
231
+ if (!json ||
232
+ (typeof validatorFrom === "function" && !validatorFrom(json)))
233
+ return undefined;
234
+ return classNode.fromJSON(json);
235
+ },
236
+ };
237
+ }
238
+ /**
239
+ * Factory for creating a data transformer for the array of ProtoObject classes or its heirs
240
+ *
241
+ * @param param0 - data validators
242
+ * @param param0.validatorTo - data validator when converting to a simple JSON object
243
+ * @param param0.validatorFrom - data validator when converting to a class
244
+ * @returns data transformer for the array of the ProtoObject classes or its heirs
245
+ */
246
+ static collectionTransformer({ validatorTo, validatorFrom, }) {
247
+ const classNode = this;
248
+ return {
249
+ to(objArr) {
250
+ if (!Array.isArray(objArr))
251
+ return undefined;
252
+ return objArr
253
+ .filter((obj) => !!obj && (typeof validatorTo !== "function" || !!validatorTo(obj)))
254
+ .map((obj) => obj.toJSON());
255
+ },
256
+ from(jsonArr) {
257
+ if (!Array.isArray(jsonArr))
258
+ return undefined;
259
+ return jsonArr
260
+ .filter((json) => !!json &&
261
+ (typeof validatorFrom !== "function" || !!validatorFrom(json)))
262
+ .map((json) => classNode.fromJSON(json));
263
+ },
264
+ };
265
+ }
266
+ }
267
+ exports.ProtoObject = ProtoObject;
268
+ //# sourceMappingURL=proto-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proto-object.js","sourceRoot":"","sources":["../../src/classes/proto-object.ts"],"names":[],"mappings":";;;AAKA;;GAEG;AACH,MAAa,WAAW;IAGtB;;;;OAIG;IACH,YAAY,IAAiB;QAC3B,OAAO,WAAW,CAAC,UAAU,CAAI,IAAoB,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC;IACrE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,aAAa,CAAC,IAE3B;QACC,MAAM,KAAK,GAIL,EAAE,CAAC;QACT,wCAAwC;QACxC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,IAAI,GAAG,MAAM,CAAC,wBAAwB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;YACxD,IAAI,IAAI;gBAAE,KAAK,CAAC,I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;YACvC,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,qBAAqB,CAAyC,EAC1E,WAAW,EACX,aAAa,GAId;QACC,MAAM,SAAS,GAAG,IAA8C,CAAC;QACjE,OAAO;YACL,EAAE,CAAC,MAAe;gBAChB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;oBAAE,OAAO,SAAS,CAAC;gBAC7C,OAAO,MAAM;qBACV,MAAM,CACL,CAAC,GAAG,EAAE,EAAE,CACN,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,WAAW,KAAK,UAAU,IAAI,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CACrE;qBACA,GAAG,CAAgB,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;YAC/C,CAAC;YACD,IAAI,CAAC,OAAgB;gBACnB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;oBAAE,OAAO,SAAS,CAAC;gBAC9C,OAAO,OAAO;qBACX,MAAM,CACL,CAAC,IAAI,EAAE,EAAE,CACP,CAAC,CAAC,IAAI;oBACN,CAAC,OAAO,aAAa,KAAK,UAAU,IAAI,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CACjE;qBACA,GAAG,CAAI,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;YAChD,CAAC;SACF,CAAC;IACJ,CAAC;CACF;AArUD,kCAqUC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Use this decorator to check the static properties of a class
3
+ *
4
+ * Example: `@StaticImplements<ProtoObjectStaticMethods<User>>()`
5
+ */
6
+ export declare const StaticImplements: <T>() => <U extends T>(constructor: U) => void;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-unused-expressions */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.StaticImplements = void 0;
5
+ /**
6
+ * Use this decorator to check the static properties of a class
7
+ *
8
+ * Example: `@StaticImplements<ProtoObjectStaticMethods<User>>()`
9
+ */
10
+ const StaticImplements = () => {
11
+ return (constructor) => {
12
+ constructor;
13
+ };
14
+ };
15
+ exports.StaticImplements = StaticImplements;
16
+ //# sourceMappingURL=static-implements.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"static-implements.js","sourceRoot":"","sources":["../../src/decorators/static-implements.ts"],"names":[],"mappings":";AAAA,6DAA6D;;;AAE7D;;;;GAIG;AACI,MAAM,gBAAgB,GAAG,GAAM,EAAE;IACtC,OAAO,CAAc,WAAc,EAAE,EAAE;QACrC,WAAW,CAAC;IACd,CAAC,CAAC;AACJ,CAAC,CAAC;AAJW,QAAA,gBAAgB,oBAI3B"}
package/lib/index.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  /**
2
- * TBD
2
+ * A universal class for creating any JSON objects and simple manipulations with them.
3
3
  * @module protoobject
4
4
  * @author Siarhei Dudko <siarhei@dudko.dev>
5
- * @copyright 2020
5
+ * @copyright 2024
6
6
  * @license MIT
7
- * @version 2.0.0
8
- * @requires stream
7
+ * @version 1.0.0
9
8
  */
10
- declare const _default: {};
11
- export = _default;
9
+ import { ProtoObject } from "./classes/proto-object";
10
+ import { ProtoObjectDynamicMethods } from "./types/dynamic-methods";
11
+ import { ProtoObjectStaticMethods } from "./types/static-methods";
12
+ import { StaticImplements } from "./decorators/static-implements";
13
+ import { protoObjectFactory } from "./utils/protoobject-factory";
14
+ export { ProtoObject, StaticImplements, ProtoObjectDynamicMethods, ProtoObjectStaticMethods, protoObjectFactory, };
package/lib/index.js CHANGED
@@ -1,12 +1,18 @@
1
1
  "use strict";
2
2
  /**
3
- * TBD
3
+ * A universal class for creating any JSON objects and simple manipulations with them.
4
4
  * @module protoobject
5
5
  * @author Siarhei Dudko <siarhei@dudko.dev>
6
- * @copyright 2020
6
+ * @copyright 2024
7
7
  * @license MIT
8
- * @version 2.0.0
9
- * @requires stream
8
+ * @version 1.0.0
10
9
  */
11
- module.exports = (module.exports = {});
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.protoObjectFactory = exports.StaticImplements = exports.ProtoObject = void 0;
12
+ const proto_object_1 = require("./classes/proto-object");
13
+ Object.defineProperty(exports, "ProtoObject", { enumerable: true, get: function () { return proto_object_1.ProtoObject; } });
14
+ const static_implements_1 = require("./decorators/static-implements");
15
+ Object.defineProperty(exports, "StaticImplements", { enumerable: true, get: function () { return static_implements_1.StaticImplements; } });
16
+ const protoobject_factory_1 = require("./utils/protoobject-factory");
17
+ Object.defineProperty(exports, "protoObjectFactory", { enumerable: true, get: function () { return protoobject_factory_1.protoObjectFactory; } });
12
18
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;AAEH,iBAAS,CAAC,MAAM,CAAC,OAAO,GAAG,EAAE,CAAC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAEH,yDAAqD;AAOnD,4FAPO,0BAAW,OAOP;AAJb,sEAAkE;AAKhE,iGALO,oCAAgB,OAKP;AAJlB,qEAAiE;AAO/D,mGAPO,wCAAkB,OAOP"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * An universal object
3
+ */
4
+ export interface AnyObject {
5
+ [key: string]: any;
6
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=any-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"any-object.js","sourceRoot":"","sources":["../../src/types/any-object.ts"],"names":[],"mappings":""}
@@ -0,0 +1,21 @@
1
+ import { ProtoObjectDynamicMethods } from "./dynamic-methods";
2
+ /**
3
+ * A data transformer for the array of ProtoObject classes or its heirs
4
+ *
5
+ */
6
+ export interface CollectionTransformer<T extends ProtoObjectDynamicMethods<T>, K> {
7
+ /**
8
+ * Converter of an array of ProtoObject classes or its heirs to simple jsons
9
+ *
10
+ * @param objectArr - the array of ProtoObject classes or its heirs
11
+ * @returns - the array of the simple jsons
12
+ */
13
+ to: (objectArr: unknown) => K[] | undefined;
14
+ /**
15
+ * Converter of an array of simple jsons to ProtoObject classes or its heirs
16
+ *
17
+ * @param jsonArr - the array of the simple jsons
18
+ * @returns - the array of ProtoObject classes or its heirs
19
+ */
20
+ from: (jsonArr: unknown) => T[] | undefined;
21
+ }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ /* eslint-disable no-unused-vars */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=collection-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-transformer.js","sourceRoot":"","sources":["../../src/types/collection-transformer.ts"],"names":[],"mappings":";AAAA,mCAAmC"}
@@ -0,0 +1,31 @@
1
+ import { AnyObject } from "./any-object";
2
+ /**
3
+ * Dynamic methods of the ProtoObject class and its heirs
4
+ */
5
+ export interface ProtoObjectDynamicMethods<T> {
6
+ /**
7
+ * A method for converting a ProtoObject class or its heir to simple json
8
+ *
9
+ * @returns - a simple json
10
+ */
11
+ toJSON: () => AnyObject;
12
+ /**
13
+ * A method for converting a ProtoObject class or its heir to a string
14
+ *
15
+ * @returns - string
16
+ */
17
+ toString(): string;
18
+ /**
19
+ * Copying a ProtoObject class or its heirs
20
+ *
21
+ * @returns - a deep copy of the ProtoObject object or its heir
22
+ */
23
+ copy: () => T;
24
+ /**
25
+ * Deep assign data to an instance of the ProtoObject class or its heir
26
+ *
27
+ * @param data - a ProtoObject class or its heir or any other object
28
+ * @returns - a assigned ProtoObject class or its heir
29
+ */
30
+ assign: (data: Partial<T>) => T;
31
+ }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ /* eslint-disable no-unused-vars */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=dynamic-methods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dynamic-methods.js","sourceRoot":"","sources":["../../src/types/dynamic-methods.ts"],"names":[],"mappings":";AAAA,mCAAmC"}
@@ -0,0 +1,21 @@
1
+ import { ProtoObjectDynamicMethods } from "./dynamic-methods";
2
+ /**
3
+ * A data transformer for the ProtoObject class or its heir
4
+ *
5
+ */
6
+ export interface RecordTransformer<T extends ProtoObjectDynamicMethods<T>, K> {
7
+ /**
8
+ * Converter of a ProtoObject class or its heir to simple json
9
+ *
10
+ * @param objectArr - the ProtoObject class or its heir
11
+ * @returns - the simple json
12
+ */
13
+ to: (obj: unknown) => K | undefined;
14
+ /**
15
+ * Converter of a simple json to ProtoObject class or its heir
16
+ *
17
+ * @param jsonArr - the simple json
18
+ * @returns - the ProtoObject class or its heir
19
+ */
20
+ from: (json: unknown) => T | undefined;
21
+ }
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ /* eslint-disable no-unused-vars */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=record-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record-transformer.js","sourceRoot":"","sources":["../../src/types/record-transformer.ts"],"names":[],"mappings":";AAAA,mCAAmC"}
@@ -0,0 +1,109 @@
1
+ import { AnyObject } from "./any-object";
2
+ import { UnknownObject } from "./unknown-object";
3
+ import { RecordTransformer } from "./record-transformer";
4
+ import { ProtoObjectDynamicMethods } from "./dynamic-methods";
5
+ import { CollectionTransformer } from "./collection-transformer";
6
+ import { ValidatorFunction } from "./validator-function";
7
+ /**
8
+ * Static methods of the ProtoObject class and its heirs
9
+ */
10
+ export interface ProtoObjectStaticMethods<T extends ProtoObjectDynamicMethods<T>> {
11
+ /**
12
+ * A class constructor
13
+ */
14
+ new (data?: Partial<T>): T;
15
+ /**
16
+ * Get all properties of an object and its prototypes
17
+ *
18
+ * @param data - any non-null object, such as an instance of the ProtoObject class or its heir
19
+ * @returns - an array of the properties information
20
+ */
21
+ getProperties(data: {
22
+ [key: PropertyKey]: unknown;
23
+ }): {
24
+ key: PropertyKey;
25
+ value: unknown;
26
+ descriptor: PropertyDescriptor;
27
+ }[];
28
+ /**
29
+ * Get all enumerable properties of an object and its prototypes
30
+ *
31
+ * @param data - any non-null object, such as an instance of the ProtoObject class or its heir
32
+ * @param options - options for the returned array of properties
33
+ * @param options.onlyWritable - return only writable properties
34
+ * @returns - an array of the properties information
35
+ */
36
+ getEnumerableProperties<T extends ProtoObjectDynamicMethods<T>>(data: {
37
+ [key: PropertyKey]: unknown;
38
+ }, options?: {
39
+ onlyWritable: boolean;
40
+ }): {
41
+ key: PropertyKey;
42
+ value: unknown;
43
+ descriptor: PropertyDescriptor;
44
+ }[];
45
+ /**
46
+ * A recursive function for assigning properties to an object or returning a property
47
+ * if it is not interchangeable
48
+ *
49
+ * WARN: The first transferred object will be changed.
50
+ *
51
+ * @param obj - an object such as a ProtoObject or its heir or an object property
52
+ * @param data - an object such as a ProtoObject or its heir or an object property
53
+ * @returns - an object such as a ProtoObject or its heir or an object property
54
+ */
55
+ recursiveAssign<T extends ProtoObjectDynamicMethods<T>>(obj: unknown, data?: unknown): T;
56
+ /**
57
+ * Deep assign data to an instance of the ProtoObject class or its heir
58
+ *
59
+ * @param obj - a ProtoObject class or its heir
60
+ * @param data - a ProtoObject class or its heir or any other object
61
+ * @returns - a assigned ProtoObject class or its heir
62
+ */
63
+ deepAssign<T extends ProtoObjectDynamicMethods<T>>(obj: T, data?: Partial<T>): T;
64
+ /**
65
+ * The converter of values into simple types
66
+ *
67
+ * @param data - a value to convert to a simple type
68
+ * @returns - a simple type
69
+ */
70
+ valueToJSON(data: unknown): unknown;
71
+ /**
72
+ * The converter of simple types into values
73
+ *
74
+ * @param data - a simple type to convert to a value
75
+ * @returns - a value
76
+ */
77
+ valueFromJSON(data: unknown): unknown;
78
+ /**
79
+ * A method for converting a simple json to ProtoObject class or its heir
80
+ *
81
+ * @param data - a simple json data
82
+ * @returns - a ProtoObject class or its heir
83
+ */
84
+ fromJSON: (data: AnyObject) => T;
85
+ /**
86
+ * Factory for creating a data transformer for the ProtoObject class or its heir
87
+ *
88
+ * @param param0 - data validators
89
+ * @param param0.validatorTo - data validator when converting to a simple JSON object
90
+ * @param param0.validatorFrom - data validator when converting to a class
91
+ * @returns data transformer for the ProtoObject class or its heir
92
+ */
93
+ recordTransformer: (options: {
94
+ validatorTo?: ValidatorFunction<T>;
95
+ validatorFrom?: ValidatorFunction<UnknownObject>;
96
+ }) => RecordTransformer<T, UnknownObject>;
97
+ /**
98
+ * Factory for creating a data transformer for the array of ProtoObject classes or its heirs
99
+ *
100
+ * @param param0 - data validators
101
+ * @param param0.validatorTo - data validator when converting to a simple JSON object
102
+ * @param param0.validatorFrom - data validator when converting to a class
103
+ * @returns data transformer for the array of the ProtoObject classes or its heirs
104
+ */
105
+ collectionTransformer: (options: {
106
+ validatorTo?: ValidatorFunction<T>;
107
+ validatorFrom?: ValidatorFunction<UnknownObject>;
108
+ }) => CollectionTransformer<T, UnknownObject>;
109
+ }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-shadow */
3
+ /* eslint-disable no-unused-vars */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ //# sourceMappingURL=static-methods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"static-methods.js","sourceRoot":"","sources":["../../src/types/static-methods.ts"],"names":[],"mappings":";AAAA,iDAAiD;AACjD,mCAAmC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * An universal object
3
+ */
4
+ export interface UnknownObject {
5
+ [key: string]: unknown;
6
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=unknown-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unknown-object.js","sourceRoot":"","sources":["../../src/types/unknown-object.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * The function of validating the fields of an object when it is transformed from/to a database
3
+ */
4
+ export type ValidatorFunction<T> = (data: unknown) => T | undefined;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ /* eslint-disable no-unused-vars */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=validator-function.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator-function.js","sourceRoot":"","sources":["../../src/types/validator-function.ts"],"names":[],"mappings":";AAAA,mCAAmC"}
@@ -0,0 +1,9 @@
1
+ import { ProtoObjectDynamicMethods } from "../types/dynamic-methods";
2
+ import { ProtoObjectStaticMethods } from "../types/static-methods";
3
+ /**
4
+ * A factory for creating classes based on the ProtoObject class
5
+ *
6
+ * @param methods - Methods that should be updated in the class being created.
7
+ * @returns - an ProtoObject's heir
8
+ */
9
+ export declare function protoObjectFactory<T extends ProtoObjectDynamicMethods<T>>(methods?: Partial<ProtoObjectStaticMethods<T> & ProtoObjectDynamicMethods<T>>): ProtoObjectStaticMethods<T>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.protoObjectFactory = protoObjectFactory;
4
+ const proto_object_1 = require("../classes/proto-object");
5
+ /**
6
+ * A factory for creating classes based on the ProtoObject class
7
+ *
8
+ * @param methods - Methods that should be updated in the class being created.
9
+ * @returns - an ProtoObject's heir
10
+ */
11
+ function protoObjectFactory(methods) {
12
+ class CProtoObject extends proto_object_1.ProtoObject {
13
+ constructor(data) {
14
+ super(data);
15
+ if (methods) {
16
+ Object.keys(methods)
17
+ .filter((key) => typeof this[key] === "function")
18
+ .map((key) => key)
19
+ .forEach((key) => {
20
+ this[key] = methods[key];
21
+ });
22
+ }
23
+ }
24
+ }
25
+ if (methods) {
26
+ Object.keys(methods)
27
+ .filter((key) => typeof CProtoObject[key] === "function")
28
+ .map((key) => key)
29
+ .forEach((key) => {
30
+ CProtoObject[key] = methods[key];
31
+ });
32
+ }
33
+ return CProtoObject;
34
+ }
35
+ //# sourceMappingURL=protoobject-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protoobject-factory.js","sourceRoot":"","sources":["../../src/utils/protoobject-factory.ts"],"names":[],"mappings":";;AAUA,gDAuCC;AAhDD,0DAAsD;AAGtD;;;;;GAKG;AACH,SAAgB,kBAAkB,CAChC,OAA6E;IAE7E,MAAM,YAAa,SAAQ,0BAAyB;QAClD,YAAY,IAA2B;YACrC,KAAK,CAAC,IAAI,CAAC,CAAC;YACZ,IAAI,OAAO,EAAE,CAAC;gBACZ,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;qBACjB,MAAM,CACL,CAAC,GAAG,EAAE,EAAE,CACN,OAAO,IAAI,CACT,GAAoD,CACrD,KAAK,UAAU,CACnB;qBACA,GAAG,CACF,CAAC,GAAG,EAAE,EAAE,CAAC,GAAoD,CAC9D;qBACA,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;oBACf,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAQ,CAAC;gBAClC,CAAC,CAAC,CAAC;YACP,CAAC;QACH,CAAC;KACF;IACD,IAAI,OAAO,EAAE,CAAC;QACZ,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC;aACjB,MAAM,CACL,CAAC,GAAG,EAAE,EAAE,CACN,OAAO,YAAY,CACjB,GAAmD,CACpD,KAAK,UAAU,CACnB;aACA,GAAG,CACF,CAAC,GAAG,EAAE,EAAE,CAAC,GAAmD,CAC7D;aACA,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACf,YAAY,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAQ,CAAC;QAC1C,CAAC,CAAC,CAAC;IACP,CAAC;IACD,OAAO,YAAsD,CAAC;AAChE,CAAC"}
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "protoobject",
3
- "version": "0.0.1",
4
- "description": "TBD",
3
+ "version": "1.0.0",
4
+ "description": "A universal class for creating any JSON objects and simple manipulations with them.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "scripts": {
8
- "test": "node --test test/**/*.test.js",
9
- "cov": "node_modules/.bin/nyc npm run test",
10
- "lint": "node_modules/.bin/eslint src/**/*.ts",
8
+ "test:ts": "node --import=tsx --test test/ts/*.test.ts",
9
+ "test:js": "node --test test/js/*.test.js",
10
+ "cov": "./node_modules/.bin/nyc npm run test:ts",
11
+ "lint": "./node_modules/.bin/eslint src/**/*.ts",
11
12
  "prebuild": "npm run lint",
12
- "build": "rm -rf lib && node_modules/.bin/tsc --declaration",
13
+ "build": "rm -rf lib && ./node_modules/.bin/tsc --declaration",
13
14
  "update": "eval \"$(node -e 'const t = require(`./package.json`);const ignore = require(`./ignoreUpdatesModules.json`);console.log(`npm i ${(Object.keys(t.dependencies || {}).filter((e)=>ignore.base.indexOf(e) === -1).map((e)=>(`${e}@latest`)).join(` `))} --save&&npm i ${(Object.keys(t.devDependencies || {}).filter((e)=>ignore.dev.indexOf(e) === -1).map((e)=>(`${e}@latest`)).join(` `))} --save-dev`);')\""
14
15
  },
15
16
  "author": {
@@ -37,7 +38,16 @@
37
38
  "url": "github:dudko-dev/protoobject"
38
39
  },
39
40
  "keywords": [
40
- "protoobject"
41
+ "protoobject",
42
+ "json",
43
+ "jsonstream",
44
+ "object",
45
+ "class",
46
+ "converter",
47
+ "transformer",
48
+ "base-class",
49
+ "class-converter",
50
+ "class-transformer"
41
51
  ],
42
52
  "homepage": "https://github.com/dudko-dev/protoobject",
43
53
  "devDependencies": {
@@ -46,6 +56,7 @@
46
56
  "eslint": "^9.12.0",
47
57
  "nyc": "^17.1.0",
48
58
  "prettier": "^3.3.3",
59
+ "tsx": "^4.19.1",
49
60
  "typescript": "^5.6.3",
50
61
  "typescript-eslint": "^8.8.1"
51
62
  },