mapper-factory 3.1.3 → 3.2.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/dist/mapper.d.ts DELETED
@@ -1,190 +0,0 @@
1
- import { ClassType } from "./types";
2
- export declare class MapperFactory {
3
- /**
4
- * Constructor of the mapper.
5
- *
6
- * @param object object to be mapped considering metadata "src", "transformer" and "reverser"
7
- */
8
- constructor(object?: any);
9
- /**
10
- * Maps the properties of an object based on the provided metadata list.
11
- *
12
- * @param object - The object whose properties need to be mapped.
13
- * @param metadataList - The list of metadata used to map the object's properties.
14
- *
15
- * This method iterates over each property of the given object. For each property, it retrieves the corresponding metadata keys.
16
- * If metadata keys are found, it maps the property using the `mapProperty` method for each metadata key.
17
- * If no metadata keys are found, it maps the property directly using the `mapDirectProperty` method.
18
- */
19
- private mapObject;
20
- /**
21
- * Retrieves the metadata keys associated with a given property name.
22
- *
23
- * @param metadataList - The list of metadata objects to search through.
24
- * @param propertyName - The name of the property to find metadata keys for.
25
- * @returns An array of metadata keys that are associated with the specified property name.
26
- */
27
- private getMetaKeys;
28
- /**
29
- * Maps a property from the given object to the current instance based on the provided metadata.
30
- *
31
- * @param object - The source object containing the property to be mapped.
32
- * @param metadataList - A list of metadata that provides mapping information.
33
- * @param propertyName - The name of the property to be mapped.
34
- * @param metaKey - The key used to retrieve the specific metadata for the property.
35
- *
36
- * This method performs the following steps:
37
- * 1. Retrieves the metadata for the given metaKey.
38
- * 2. If metadata exists, it gets the properties' stereoid and creates a copy of the object.
39
- * 3. Iterates over the properties' stereoid and updates the object copy.
40
- * 4. If a transformer function is defined in the metadata, it applies the transformer to the object copy and the original object.
41
- * 5. If no metadata exists for the metaKey, it maps the property directly using the `mapDirectProperty` method.
42
- */
43
- private mapProperty;
44
- /**
45
- * Parses a dot-separated string and returns an array of PropStereoid objects.
46
- * Each PropStereoid object contains information about the property name,
47
- * whether it is an array, and the array index if applicable.
48
- *
49
- * @param src - The dot-separated string to parse.
50
- * @returns An array of PropStereoid objects.
51
- */
52
- private getPropsStereoid;
53
- /**
54
- * Retrieves a copy of an object property based on the provided `PropStereoid`.
55
- *
56
- * @param objCopy - The object from which the property copy is to be retrieved.
57
- * @param prop - The `PropStereoid` object containing property details.
58
- * @returns A copy of the specified property from the object.
59
- *
60
- * @remarks
61
- * If the property is an array, the method navigates through the array indices
62
- * specified in `prop.arrIndex` to retrieve the nested property.
63
- * Otherwise, it directly retrieves the property specified in `prop.prop`.
64
- *
65
- * @example
66
- * ```typescript
67
- * const obj = { a: { b: [ { c: 1 }, { c: 2 } ] } };
68
- * const prop = { isArray: true, arrIndex: '[1]', prop: 'b' };
69
- * const result = getObjectCopy(obj, prop);
70
- * console.log(result); // Output: { c: 2 }
71
- * ```
72
- */
73
- private getObjectCopy;
74
- /**
75
- * Maps a direct property from the given object to the current instance based on the provided metadata list.
76
- *
77
- * @param object - The source object containing the properties to be mapped.
78
- * @param metadataList - An optional list of metadata that defines how properties should be mapped and transformed.
79
- * @param propertyName - The name of the property to be mapped from the source object.
80
- *
81
- * The method performs the following steps:
82
- * 1. Checks if the metadata list contains a key that matches the source property name.
83
- * 2. If a matching metadata key is found, it uses the metadata to determine the source property name (`src`) and applies any defined transformer function.
84
- * 3. If no matching metadata key is found, it directly maps the property from the source object, applying any transformer function if defined.
85
- */
86
- private mapDirectProperty;
87
- /**
88
- * Initializes properties of the current object based on the provided metadata list.
89
- *
90
- * @param metadataList - An object containing metadata information for properties.
91
- * @param object - The object to be used for initializing properties.
92
- *
93
- * The method iterates over the keys of the metadataList. For each key, if the corresponding metadata
94
- * has an `initialize` property set to true, a `transformer` function, and the current object does not
95
- * already have a property with the same name, it initializes the property using the transformer function.
96
- */
97
- private initializeProperties;
98
- /**
99
- * Converts the current object instance to a mapped object.
100
- *
101
- * This method iterates over the properties of the current object and maps them
102
- * according to the metadata provided by `getMapFieldMetadataList`. If a property
103
- * has associated metadata, it uses the `mapToJson` method to map it. Otherwise,
104
- * it directly assigns the property value to the resulting object, applying a
105
- * reverser function if specified in the metadata.
106
- *
107
- * @returns {any} The mapped object.
108
- */
109
- toMap(): any;
110
- /**
111
- * Maps the provided object to a JSON structure based on the given metadata list.
112
- *
113
- * @param metadataList - An object containing metadata information for mapping.
114
- * @param obj - The object to be mapped to JSON.
115
- * @param propertyName - The name of the property to be mapped.
116
- *
117
- * The method supports nested properties and arrays. If the source property name
118
- * (src) contains dots, it indicates nested properties. The method will traverse
119
- * the object structure accordingly and create nested objects or arrays as needed.
120
- *
121
- * The `getPropsStereoid` method is used to parse the nested property names and
122
- * determine if they are arrays. The `getReversedValue` method is used to get the
123
- * value to be assigned to the final property.
124
- *
125
- * Example:
126
- * ```
127
- * const metadataList = {
128
- * 'nested.property': { src: 'nested.property' },
129
- * 'array[0].item': { src: 'array[0].item' }
130
- * };
131
- * const obj = {};
132
- * mapToJson(metadataList, obj, 'nested.property');
133
- * mapToJson(metadataList, obj, 'array[0].item');
134
- * ```
135
- * The resulting `obj` will have the structure:
136
- * ```
137
- * {
138
- * nested: {
139
- * property: <value>
140
- * },
141
- * array: [
142
- * { item: <value> }
143
- * ]
144
- * }
145
- * ```
146
- */
147
- private mapToJson;
148
- /**
149
- * Retrieves the reversed value of a property based on the provided metadata.
150
- *
151
- * @param metadataList - An object containing metadata for properties.
152
- * @param propertyName - The name of the property to retrieve the reversed value for.
153
- * @returns The reversed value of the property. If the property is an array, it applies the reverser function or maps each item to its mapped value. If the property has a `toMap` method, it calls that method. Otherwise, it applies the reverser function or returns the property value directly.
154
- */
155
- private getReversedValue;
156
- /**
157
- * Maps the properties of the given object to the current instance based on metadata.
158
- *
159
- * @param obj - The source object whose properties are to be mapped.
160
- * @returns The current instance with mapped properties.
161
- */
162
- objToModel(obj: Object): this;
163
- /**
164
- * Checks if all properties of the current object are either `undefined` or `null`.
165
- *
166
- * @returns {boolean} `true` if all properties are `undefined` or `null`, otherwise `false`.
167
- */
168
- empty(): boolean;
169
- /**
170
- * Retrieves a nested property value from the object based on the provided path.
171
- * The path can include dot notation and array indices.
172
- *
173
- * @template T - The expected type of the property value.
174
- * @param {string} path - The path to the property, using dot notation and array indices.
175
- * @returns {T} - The value of the property at the specified path.
176
- */
177
- get<T>(path: string): T;
178
- /**
179
- * Sets the value at the specified path within the object.
180
- * The path is a string that can include dot notation and array indices.
181
- *
182
- * @param path - The path to the property to set, using dot notation and array indices.
183
- * @param value - The value to set at the specified path.
184
- */
185
- set(path: string, value: any): void;
186
- }
187
- /**
188
- * @deprecated The method should not be used
189
- */
190
- export declare function MapperFactoryFun(baseClass?: ClassType): ClassType;
package/dist/mapper.js DELETED
@@ -1,371 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MapperFactoryFun = exports.MapperFactory = void 0;
4
- const field_decorator_1 = require("./field.decorator");
5
- class MapperFactory {
6
- /**
7
- * Constructor of the mapper.
8
- *
9
- * @param object object to be mapped considering metadata "src", "transformer" and "reverser"
10
- */
11
- constructor(object) {
12
- const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
13
- if (object) {
14
- this.mapObject(object, metadataList);
15
- }
16
- this.initializeProperties(metadataList, object);
17
- console.log('Final state of this in MapperFactory:', this);
18
- }
19
- /**
20
- * Maps the properties of an object based on the provided metadata list.
21
- *
22
- * @param object - The object whose properties need to be mapped.
23
- * @param metadataList - The list of metadata used to map the object's properties.
24
- *
25
- * This method iterates over each property of the given object. For each property, it retrieves the corresponding metadata keys.
26
- * If metadata keys are found, it maps the property using the `mapProperty` method for each metadata key.
27
- * If no metadata keys are found, it maps the property directly using the `mapDirectProperty` method.
28
- */
29
- mapObject(object, metadataList) {
30
- Object.keys(object).forEach(propertyName => {
31
- const metaKeys = this.getMetaKeys(metadataList, propertyName);
32
- if (metaKeys.length) {
33
- metaKeys.forEach(metaKey => {
34
- this.mapProperty(object, metadataList, propertyName, metaKey);
35
- });
36
- }
37
- else {
38
- this.mapDirectProperty(object, metadataList, propertyName);
39
- }
40
- });
41
- }
42
- /**
43
- * Retrieves the metadata keys associated with a given property name.
44
- *
45
- * @param metadataList - The list of metadata objects to search through.
46
- * @param propertyName - The name of the property to find metadata keys for.
47
- * @returns An array of metadata keys that are associated with the specified property name.
48
- */
49
- getMetaKeys(metadataList, propertyName) {
50
- return metadataList ? Object.keys(metadataList).filter(metadata => metadataList[metadata]?.src?.split('.')?.includes(propertyName)) : [];
51
- }
52
- /**
53
- * Maps a property from the given object to the current instance based on the provided metadata.
54
- *
55
- * @param object - The source object containing the property to be mapped.
56
- * @param metadataList - A list of metadata that provides mapping information.
57
- * @param propertyName - The name of the property to be mapped.
58
- * @param metaKey - The key used to retrieve the specific metadata for the property.
59
- *
60
- * This method performs the following steps:
61
- * 1. Retrieves the metadata for the given metaKey.
62
- * 2. If metadata exists, it gets the properties' stereoid and creates a copy of the object.
63
- * 3. Iterates over the properties' stereoid and updates the object copy.
64
- * 4. If a transformer function is defined in the metadata, it applies the transformer to the object copy and the original object.
65
- * 5. If no metadata exists for the metaKey, it maps the property directly using the `mapDirectProperty` method.
66
- */
67
- mapProperty(object, metadataList, propertyName, metaKey) {
68
- const metaProp = metadataList[metaKey];
69
- if (metaProp) {
70
- const propsStereoid = this.getPropsStereoid(metaProp.src);
71
- let objCopy = { ...object };
72
- propsStereoid.forEach(prop => {
73
- objCopy = this.getObjectCopy(objCopy, prop);
74
- });
75
- this[metaKey] = metaProp.transformer ? metaProp.transformer(objCopy, object) : objCopy;
76
- }
77
- else {
78
- this.mapDirectProperty(object, metadataList, propertyName);
79
- }
80
- }
81
- /**
82
- * Parses a dot-separated string and returns an array of PropStereoid objects.
83
- * Each PropStereoid object contains information about the property name,
84
- * whether it is an array, and the array index if applicable.
85
- *
86
- * @param src - The dot-separated string to parse.
87
- * @returns An array of PropStereoid objects.
88
- */
89
- getPropsStereoid(src) {
90
- return src.split('.').map(prop => {
91
- const index = prop.indexOf('[');
92
- return {
93
- prop: index > 0 ? prop.substring(0, index) : prop,
94
- isArray: prop.includes('[') && prop.includes(']'),
95
- arrIndex: prop.substring(index),
96
- };
97
- });
98
- }
99
- /**
100
- * Retrieves a copy of an object property based on the provided `PropStereoid`.
101
- *
102
- * @param objCopy - The object from which the property copy is to be retrieved.
103
- * @param prop - The `PropStereoid` object containing property details.
104
- * @returns A copy of the specified property from the object.
105
- *
106
- * @remarks
107
- * If the property is an array, the method navigates through the array indices
108
- * specified in `prop.arrIndex` to retrieve the nested property.
109
- * Otherwise, it directly retrieves the property specified in `prop.prop`.
110
- *
111
- * @example
112
- * ```typescript
113
- * const obj = { a: { b: [ { c: 1 }, { c: 2 } ] } };
114
- * const prop = { isArray: true, arrIndex: '[1]', prop: 'b' };
115
- * const result = getObjectCopy(obj, prop);
116
- * console.log(result); // Output: { c: 2 }
117
- * ```
118
- */
119
- getObjectCopy(objCopy, prop) {
120
- if (prop.isArray) {
121
- const arrIndex = prop.arrIndex.split(/\[(\w+)\]/g).filter(index => index !== '');
122
- objCopy = objCopy[prop.prop];
123
- arrIndex.forEach(index => {
124
- objCopy = objCopy[index];
125
- });
126
- }
127
- else {
128
- objCopy = objCopy[prop.prop];
129
- }
130
- return objCopy;
131
- }
132
- /**
133
- * Maps a direct property from the given object to the current instance based on the provided metadata list.
134
- *
135
- * @param object - The source object containing the properties to be mapped.
136
- * @param metadataList - An optional list of metadata that defines how properties should be mapped and transformed.
137
- * @param propertyName - The name of the property to be mapped from the source object.
138
- *
139
- * The method performs the following steps:
140
- * 1. Checks if the metadata list contains a key that matches the source property name.
141
- * 2. If a matching metadata key is found, it uses the metadata to determine the source property name (`src`) and applies any defined transformer function.
142
- * 3. If no matching metadata key is found, it directly maps the property from the source object, applying any transformer function if defined.
143
- */
144
- mapDirectProperty(object, metadataList, propertyName) {
145
- const metaKey = metadataList ? Object.keys(metadataList).find(metadata => metadataList[metadata]?.src === propertyName) : null;
146
- if (metaKey) {
147
- const src = metadataList[metaKey].src || propertyName;
148
- this[metaKey] = metadataList[metaKey].transformer ? metadataList[metaKey].transformer(object[src], object) : object[src];
149
- }
150
- else {
151
- this[propertyName] = (metadataList && metadataList[propertyName]?.transformer) ? metadataList[propertyName].transformer(object[propertyName], object) : object[propertyName];
152
- }
153
- }
154
- /**
155
- * Initializes properties of the current object based on the provided metadata list.
156
- *
157
- * @param metadataList - An object containing metadata information for properties.
158
- * @param object - The object to be used for initializing properties.
159
- *
160
- * The method iterates over the keys of the metadataList. For each key, if the corresponding metadata
161
- * has an `initialize` property set to true, a `transformer` function, and the current object does not
162
- * already have a property with the same name, it initializes the property using the transformer function.
163
- */
164
- initializeProperties(metadataList, object) {
165
- if (metadataList) {
166
- Object.keys(metadataList).forEach(metaName => {
167
- if (metadataList[metaName]?.initialize && metadataList[metaName]?.transformer && this[metaName] === undefined) {
168
- this[metaName] = metadataList[metaName]?.transformer(null, object);
169
- }
170
- });
171
- }
172
- }
173
- /**
174
- * Converts the current object instance to a mapped object.
175
- *
176
- * This method iterates over the properties of the current object and maps them
177
- * according to the metadata provided by `getMapFieldMetadataList`. If a property
178
- * has associated metadata, it uses the `mapToJson` method to map it. Otherwise,
179
- * it directly assigns the property value to the resulting object, applying a
180
- * reverser function if specified in the metadata.
181
- *
182
- * @returns {any} The mapped object.
183
- */
184
- toMap() {
185
- const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
186
- const obj = {};
187
- Object.keys(this).forEach(propertyName => {
188
- if (metadataList && metadataList[propertyName]) {
189
- this.mapToJson(metadataList, obj, propertyName);
190
- }
191
- else {
192
- obj[propertyName] = metadataList && metadataList[propertyName]?.reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
193
- }
194
- if (!obj[propertyName]) {
195
- delete obj[propertyName];
196
- }
197
- });
198
- return obj;
199
- }
200
- /**
201
- * Maps the provided object to a JSON structure based on the given metadata list.
202
- *
203
- * @param metadataList - An object containing metadata information for mapping.
204
- * @param obj - The object to be mapped to JSON.
205
- * @param propertyName - The name of the property to be mapped.
206
- *
207
- * The method supports nested properties and arrays. If the source property name
208
- * (src) contains dots, it indicates nested properties. The method will traverse
209
- * the object structure accordingly and create nested objects or arrays as needed.
210
- *
211
- * The `getPropsStereoid` method is used to parse the nested property names and
212
- * determine if they are arrays. The `getReversedValue` method is used to get the
213
- * value to be assigned to the final property.
214
- *
215
- * Example:
216
- * ```
217
- * const metadataList = {
218
- * 'nested.property': { src: 'nested.property' },
219
- * 'array[0].item': { src: 'array[0].item' }
220
- * };
221
- * const obj = {};
222
- * mapToJson(metadataList, obj, 'nested.property');
223
- * mapToJson(metadataList, obj, 'array[0].item');
224
- * ```
225
- * The resulting `obj` will have the structure:
226
- * ```
227
- * {
228
- * nested: {
229
- * property: <value>
230
- * },
231
- * array: [
232
- * { item: <value> }
233
- * ]
234
- * }
235
- * ```
236
- */
237
- mapToJson(metadataList, obj, propertyName) {
238
- const src = metadataList[propertyName].src || propertyName;
239
- if (src.includes('.')) {
240
- const propsStereoid = this.getPropsStereoid(src);
241
- let objCopy = obj;
242
- let lastIndex;
243
- propsStereoid.forEach((prop, i) => {
244
- if (prop.isArray) {
245
- const arrIndex = prop.arrIndex.split(/\[(\w+)\]/g).filter(index => index !== '');
246
- objCopy[prop.prop] = objCopy[prop.prop] || [];
247
- objCopy = objCopy[prop.prop];
248
- arrIndex.forEach((index, j) => {
249
- objCopy[index] = objCopy[index] || (j === arrIndex.length - 1 ? {} : []);
250
- if (j !== propsStereoid.length - 1) {
251
- objCopy = objCopy[index];
252
- }
253
- else {
254
- lastIndex = index;
255
- }
256
- });
257
- }
258
- else {
259
- objCopy[prop.prop] = objCopy[prop.prop] || {};
260
- if (i !== propsStereoid.length - 1) {
261
- objCopy = objCopy[prop.prop];
262
- }
263
- else {
264
- lastIndex = prop.prop;
265
- }
266
- }
267
- });
268
- objCopy[lastIndex] = this.getReversedValue(metadataList, propertyName);
269
- }
270
- else {
271
- obj[src] = this.getReversedValue(metadataList, propertyName);
272
- }
273
- }
274
- /**
275
- * Retrieves the reversed value of a property based on the provided metadata.
276
- *
277
- * @param metadataList - An object containing metadata for properties.
278
- * @param propertyName - The name of the property to retrieve the reversed value for.
279
- * @returns The reversed value of the property. If the property is an array, it applies the reverser function or maps each item to its mapped value. If the property has a `toMap` method, it calls that method. Otherwise, it applies the reverser function or returns the property value directly.
280
- */
281
- getReversedValue(metadataList, propertyName) {
282
- if (Array.isArray(this[propertyName])) {
283
- return metadataList[propertyName].reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName].map(item => item?.toMap ? item.toMap() : item);
284
- }
285
- else if (metadataList[propertyName].toMap) {
286
- return this[propertyName]?.toMap();
287
- }
288
- else {
289
- return metadataList[propertyName].reverser ? metadataList[propertyName].reverser(this[propertyName], this) : this[propertyName];
290
- }
291
- }
292
- /**
293
- * Maps the properties of the given object to the current instance based on metadata.
294
- *
295
- * @param obj - The source object whose properties are to be mapped.
296
- * @returns The current instance with mapped properties.
297
- */
298
- objToModel(obj) {
299
- const metadataList = (0, field_decorator_1.getMapFieldMetadataList)(this);
300
- Object.keys(obj).forEach(propertyName => {
301
- if (metadataList && metadataList[propertyName]) {
302
- if (metadataList[propertyName].transformer) {
303
- this[propertyName] = Array.isArray(obj[propertyName])
304
- ? obj[propertyName].map(item => item)
305
- : metadataList[propertyName].transformer(obj[propertyName], obj);
306
- }
307
- else {
308
- this[propertyName] = obj[propertyName];
309
- }
310
- }
311
- else {
312
- this[propertyName] = obj[propertyName];
313
- }
314
- });
315
- return this;
316
- }
317
- /**
318
- * Checks if all properties of the current object are either `undefined` or `null`.
319
- *
320
- * @returns {boolean} `true` if all properties are `undefined` or `null`, otherwise `false`.
321
- */
322
- empty() {
323
- return !Object.keys(this).some(propertyName => this[propertyName] !== undefined && this[propertyName] !== null);
324
- }
325
- /**
326
- * Retrieves a nested property value from the object based on the provided path.
327
- * The path can include dot notation and array indices.
328
- *
329
- * @template T - The expected type of the property value.
330
- * @param {string} path - The path to the property, using dot notation and array indices.
331
- * @returns {T} - The value of the property at the specified path.
332
- */
333
- get(path) {
334
- const props = path.replace(/\[(\w+)\]/g, '.$1').split('.');
335
- return props.reduce((acc, prop) => acc && acc[prop], this);
336
- }
337
- /**
338
- * Sets the value at the specified path within the object.
339
- * The path is a string that can include dot notation and array indices.
340
- *
341
- * @param path - The path to the property to set, using dot notation and array indices.
342
- * @param value - The value to set at the specified path.
343
- */
344
- set(path, value) {
345
- const props = path.replace(/\[(\w+)\]/g, '.$1').split('.');
346
- let obj = this;
347
- let i;
348
- for (i = 0; i < props?.length - 1; i++) {
349
- props?.[i] && (obj = obj?.[props?.[i]]);
350
- }
351
- props?.[i] && obj && (obj[props?.[i]] = value);
352
- }
353
- }
354
- exports.MapperFactory = MapperFactory;
355
- /**
356
- * @deprecated The method should not be used
357
- */
358
- function MapperFactoryFun(baseClass = Object) {
359
- return class Mapper extends baseClass {
360
- /**
361
- * Constructor of the mapper.
362
- *
363
- * @param object object to be mapped considering metadata "src" and "transformer"
364
- * @param rest other optional parameters
365
- */
366
- constructor(object, ...rest) {
367
- super(...rest);
368
- }
369
- };
370
- }
371
- exports.MapperFactoryFun = MapperFactoryFun;