@duet3d/objectmodel 3.4.0-b6 → 3.4.0-b7

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.
@@ -24,3 +24,14 @@ export declare class ModelCollection<T extends IModelObject> extends Array<T> im
24
24
  update(jsonElement: any): IModelObject | null;
25
25
  }
26
26
  export default ModelCollection;
27
+ /**
28
+ * Initialize a model collection from the given data
29
+ * @param itemType Item type to create
30
+ * @param data Data to assign
31
+ * @returns Initialized model collection
32
+ */
33
+ export declare function initCollection<T extends IModelObject>(itemType: {
34
+ new (): T;
35
+ }, data: Array<{
36
+ [Property in keyof T]?: T[Property];
37
+ }>): ModelCollection<T>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelCollection = void 0;
3
+ exports.initCollection = exports.ModelCollection = void 0;
4
4
  const ModelObject_1 = require("./ModelObject");
5
5
  const index_1 = require("./index");
6
6
  /**
@@ -79,3 +79,19 @@ class ModelCollection extends Array {
79
79
  }
80
80
  exports.ModelCollection = ModelCollection;
81
81
  exports.default = ModelCollection;
82
+ /**
83
+ * Initialize a model collection from the given data
84
+ * @param itemType Item type to create
85
+ * @param data Data to assign
86
+ * @returns Initialized model collection
87
+ */
88
+ function initCollection(itemType, data) {
89
+ const result = new ModelCollection(itemType);
90
+ for (let presetItem of data) {
91
+ const item = new itemType();
92
+ item.update(presetItem);
93
+ result.push(item);
94
+ }
95
+ return result;
96
+ }
97
+ exports.initCollection = initCollection;
@@ -27,3 +27,15 @@ export declare class ModelDictionary<T> extends Map<string, T | null> implements
27
27
  update(jsonElement: any): IModelObject | null;
28
28
  }
29
29
  export default ModelDictionary;
30
+ /**
31
+ * Initialize a model dictionary from the given data
32
+ * @param nullDeletesKeys Defines whether setting values to null deletes the corresponding key
33
+ * @param itemConstructor Item constructor
34
+ * @param data Data to assign
35
+ * @returns Initialized model dictionary
36
+ */
37
+ export declare function initDictionary<T>(nullDeletesKeys: boolean, itemConstructor: {
38
+ new (): T;
39
+ }, data: Record<string, {
40
+ [Property in keyof T]?: T[Property];
41
+ }>): ModelDictionary<T>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelDictionary = void 0;
3
+ exports.initDictionary = exports.ModelDictionary = void 0;
4
4
  const ModelObject_1 = require("./ModelObject");
5
5
  /**
6
6
  * Dictionary class to map object model data
@@ -67,3 +67,25 @@ class ModelDictionary extends Map {
67
67
  }
68
68
  exports.ModelDictionary = ModelDictionary;
69
69
  exports.default = ModelDictionary;
70
+ /**
71
+ * Initialize a model dictionary from the given data
72
+ * @param nullDeletesKeys Defines whether setting values to null deletes the corresponding key
73
+ * @param itemConstructor Item constructor
74
+ * @param data Data to assign
75
+ * @returns Initialized model dictionary
76
+ */
77
+ function initDictionary(nullDeletesKeys, itemConstructor, data) {
78
+ const result = new ModelDictionary(nullDeletesKeys, itemConstructor);
79
+ for (let key in data) {
80
+ const item = new itemConstructor();
81
+ if ((0, ModelObject_1.isModelObject)(item)) {
82
+ item.update(data[key]);
83
+ result.set(key, item);
84
+ }
85
+ else {
86
+ result.set(key, data[key]);
87
+ }
88
+ }
89
+ return result;
90
+ }
91
+ exports.initDictionary = initDictionary;
@@ -33,15 +33,15 @@ export declare abstract class ModelObject implements IModelObject {
33
33
  new (): T;
34
34
  }): void;
35
35
  }
36
+ export default ModelObject;
36
37
  /**
37
- * Initialize a class item from the given data
38
- * @param itemType Item type to create
38
+ * Initialize a model object from the given data
39
+ * @param itemType Model type to create
39
40
  * @param data Data to assign
40
- * @returns Initialized item instance
41
+ * @returns Initialized model instance
41
42
  */
42
- export declare function initItem<T>(itemType: {
43
+ export declare function initObject<T>(itemType: {
43
44
  new (): T;
44
45
  }, data: {
45
46
  [Property in keyof T]?: T[Property];
46
47
  }): T;
47
- export default ModelObject;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initItem = exports.ModelObject = exports.isModelObject = void 0;
3
+ exports.initObject = exports.ModelObject = exports.isModelObject = void 0;
4
4
  const index_1 = require("./index");
5
5
  /**
6
6
  * Check whether a given value provides model update functionality
@@ -153,18 +153,24 @@ class ModelObject {
153
153
  }
154
154
  }
155
155
  exports.ModelObject = ModelObject;
156
+ exports.default = ModelObject;
156
157
  /**
157
- * Initialize a class item from the given data
158
- * @param itemType Item type to create
158
+ * Initialize a model object from the given data
159
+ * @param itemType Model type to create
159
160
  * @param data Data to assign
160
- * @returns Initialized item instance
161
+ * @returns Initialized model instance
161
162
  */
162
- function initItem(itemType, data) {
163
+ function initObject(itemType, data) {
163
164
  const result = new itemType();
164
165
  for (let key in data) {
165
- result[key] = data[key];
166
+ const resultKey = result[key];
167
+ if (isModelObject(resultKey)) {
168
+ resultKey.update(data[key]);
169
+ }
170
+ else {
171
+ result[key] = data[key];
172
+ }
166
173
  }
167
174
  return result;
168
175
  }
169
- exports.initItem = initItem;
170
- exports.default = ModelObject;
176
+ exports.initObject = initObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duet3d/objectmodel",
3
- "version": "3.4.0-b6",
3
+ "version": "3.4.0-b7",
4
4
  "description": "TypeScript implementation of the Duet3D Object Model",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",