dyo-tools 0.1.0 → 0.3.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 (184) hide show
  1. package/.c8rc.json +4 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +47 -0
  4. package/Makefile +34 -0
  5. package/README.md +0 -7
  6. package/babel.config.js +1 -0
  7. package/cucumber-report.html +48 -0
  8. package/dist/constants.d.ts +6 -0
  9. package/dist/constants.js +63 -0
  10. package/dist/constants.js.map +1 -0
  11. package/dist/core/DTAction.d.ts +10 -0
  12. package/dist/core/DTAction.js +24 -0
  13. package/dist/core/DTAction.js.map +1 -0
  14. package/dist/core/DTBunch.d.ts +12 -17
  15. package/dist/core/DTBunch.js +39 -115
  16. package/dist/core/DTBunch.js.map +1 -1
  17. package/dist/core/DTComponent.d.ts +13 -5
  18. package/dist/core/DTComponent.js +39 -1
  19. package/dist/core/DTComponent.js.map +1 -1
  20. package/dist/core/DTComponentPhysical.d.ts +12 -0
  21. package/dist/core/DTComponentPhysical.js +22 -0
  22. package/dist/core/DTComponentPhysical.js.map +1 -0
  23. package/dist/core/DTComponentWithMeta.d.ts +2 -2
  24. package/dist/core/DTComponentWithMeta.js.map +1 -1
  25. package/dist/core/DTElement.d.ts +7 -7
  26. package/dist/core/DTElement.js +12 -11
  27. package/dist/core/DTElement.js.map +1 -1
  28. package/dist/core/DTManager.d.ts +33 -0
  29. package/dist/core/DTManager.js +190 -0
  30. package/dist/core/DTManager.js.map +1 -0
  31. package/dist/core/DTMaster.d.ts +24 -0
  32. package/dist/core/DTMaster.js +133 -0
  33. package/dist/core/DTMaster.js.map +1 -0
  34. package/dist/core/DTModule.d.ts +14 -0
  35. package/dist/core/DTModule.js +39 -0
  36. package/dist/core/DTModule.js.map +1 -0
  37. package/dist/index.d.ts +7 -1
  38. package/dist/index.js +15 -3
  39. package/dist/index.js.map +1 -1
  40. package/dist/libs/DYOFinder.d.ts +10 -0
  41. package/dist/libs/DYOFinder.js +96 -0
  42. package/dist/libs/DYOFinder.js.map +1 -0
  43. package/dist/libs/player/DTPlayer.element.d.ts +5 -0
  44. package/dist/libs/player/DTPlayer.element.js +11 -0
  45. package/dist/libs/player/DTPlayer.element.js.map +1 -0
  46. package/dist/libs/player/DTPlayer.manager.d.ts +11 -0
  47. package/dist/libs/player/DTPlayer.manager.js +42 -0
  48. package/dist/libs/player/DTPlayer.manager.js.map +1 -0
  49. package/dist/tsconfig.tsbuildinfo +1 -1321
  50. package/dist/types/core.d.ts +103 -0
  51. package/dist/types/core.js +15 -0
  52. package/dist/types/core.js.map +1 -0
  53. package/dist/types/index.d.ts +2 -58
  54. package/dist/types/index.js +16 -12
  55. package/dist/types/index.js.map +1 -1
  56. package/dist/types/player.d.ts +5 -0
  57. package/dist/types/player.js +3 -0
  58. package/dist/types/player.js.map +1 -0
  59. package/docs/.nojekyll +1 -0
  60. package/docs/assets/highlight.css +29 -0
  61. package/docs/assets/main.js +59 -0
  62. package/docs/assets/navigation.js +1 -0
  63. package/docs/assets/search.js +1 -0
  64. package/docs/assets/style.css +1414 -0
  65. package/docs/classes/core_DTAction.DYOToolsAction.html +89 -0
  66. package/docs/classes/core_DTBunch.DYOToolsBunch.html +254 -0
  67. package/docs/classes/core_DTComponent.DYOToolsComponent.html +76 -0
  68. package/docs/classes/core_DTComponentPhysical.DYOToolsComponentPhysical.html +110 -0
  69. package/docs/classes/core_DTComponentWithMeta.DYOToolsComponentWithMeta.html +96 -0
  70. package/docs/classes/core_DTElement.DYOToolsElement.html +135 -0
  71. package/docs/classes/core_DTError.DYOToolsError.html +37 -0
  72. package/docs/classes/core_DTManager.DYOToolsManager.html +237 -0
  73. package/docs/classes/core_DTMaster.DYOToolsMaster.html +150 -0
  74. package/docs/classes/core_DTModule.DYOToolsModule.html +92 -0
  75. package/docs/classes/libs_DYOFinder.DYOFinder.html +34 -0
  76. package/docs/classes/libs_player_DTPlayer_element.DYOToolsPlayer.html +134 -0
  77. package/docs/classes/libs_player_DTPlayer_manager.DYOToolsPlayerManager.html +243 -0
  78. package/docs/enums/types_core.FilterOperatorType.html +10 -0
  79. package/docs/hierarchy.html +1 -0
  80. package/docs/index.html +1 -0
  81. package/docs/interfaces/types_core.DTBunchFilters.html +6 -0
  82. package/docs/interfaces/types_core.DTBunchOptions.html +19 -0
  83. package/docs/interfaces/types_core.DTBunchToObject.html +7 -0
  84. package/docs/interfaces/types_core.DTComponentOptions.html +5 -0
  85. package/docs/interfaces/types_core.DTComponentToObject.html +4 -0
  86. package/docs/interfaces/types_core.DTElementToObject.html +7 -0
  87. package/docs/interfaces/types_core.DTManagerFilters.html +6 -0
  88. package/docs/interfaces/types_core.DTManagerOptions.html +8 -0
  89. package/docs/interfaces/types_core.DTManagerToObject.html +5 -0
  90. package/docs/interfaces/types_core.DTMasterToObject.html +8 -0
  91. package/docs/interfaces/types_core.DTModuleToObject.html +6 -0
  92. package/docs/interfaces/types_core.DYOFinderConfigurationPropDefault.html +4 -0
  93. package/docs/interfaces/types_core.DYOFinderConfigurationPropObjectSearch.html +4 -0
  94. package/docs/interfaces/types_core.DYOFinderFilterOperatorAdvanced.html +5 -0
  95. package/docs/interfaces/types_core.DYOFinderFilterOperatorBase.html +5 -0
  96. package/docs/interfaces/types_player.DTPlayerManagerSimpleConfiguration.html +3 -0
  97. package/docs/modules/constants.html +6 -0
  98. package/docs/modules/core_DTAction.html +2 -0
  99. package/docs/modules/core_DTBunch.html +2 -0
  100. package/docs/modules/core_DTComponent.html +2 -0
  101. package/docs/modules/core_DTComponentPhysical.html +2 -0
  102. package/docs/modules/core_DTComponentWithMeta.html +2 -0
  103. package/docs/modules/core_DTElement.html +2 -0
  104. package/docs/modules/core_DTError.html +2 -0
  105. package/docs/modules/core_DTManager.html +2 -0
  106. package/docs/modules/core_DTMaster.html +2 -0
  107. package/docs/modules/core_DTModule.html +2 -0
  108. package/docs/modules/libs_DYOFinder.html +2 -0
  109. package/docs/modules/libs_player_DTPlayer_element.html +2 -0
  110. package/docs/modules/libs_player_DTPlayer_manager.html +2 -0
  111. package/docs/modules/types.html +29 -0
  112. package/docs/modules/types_core.html +28 -0
  113. package/docs/modules/types_player.html +2 -0
  114. package/docs/types/types_core.DTAcceptedMetaData.html +2 -0
  115. package/docs/types/types_core.DTManagerItemType.html +1 -0
  116. package/docs/types/types_core.DTManagerItemsType.html +2 -0
  117. package/docs/types/types_core.DYOFinderComponentType.html +1 -0
  118. package/docs/types/types_core.DYOFinderConfiguration.html +2 -0
  119. package/docs/types/types_core.DYOFinderConfigurationProp.html +1 -0
  120. package/docs/types/types_core.DYOFinderFilterOperator.html +1 -0
  121. package/docs/types/types_core.DYOFinderFilterOperatorArgument.html +1 -0
  122. package/docs/types/types_core.DYOFinderFilters.html +1 -0
  123. package/docs/types/types_core.StandardPrimitiveType.html +2 -0
  124. package/docs/types/types_core.StandardPrimitiveTypeWithArray.html +1 -0
  125. package/docs/variables/constants.bunchDefaultOptions.html +2 -0
  126. package/docs/variables/constants.componentBunchDefaultFinderConfiguration.html +1 -0
  127. package/docs/variables/constants.componentManagerDefaultFinderConfiguration.html +1 -0
  128. package/docs/variables/constants.componentPhysicalDefaultFinderConfiguration.html +1 -0
  129. package/docs/variables/constants.managerDefaultOptions.html +2 -0
  130. package/jest.config.js +6 -0
  131. package/package.json +32 -23
  132. package/src/constants.ts +85 -0
  133. package/src/core/DTAction.ts +52 -0
  134. package/src/core/DTBunch.ts +467 -0
  135. package/src/core/DTComponent.ts +225 -0
  136. package/src/core/DTComponentPhysical.ts +53 -0
  137. package/src/core/DTComponentWithMeta.ts +65 -0
  138. package/src/core/DTElement.ts +102 -0
  139. package/src/core/DTError.ts +78 -0
  140. package/src/core/DTManager.ts +465 -0
  141. package/src/core/DTMaster.ts +318 -0
  142. package/src/core/DTModule.ts +90 -0
  143. package/src/index.ts +17 -0
  144. package/src/libs/DYOFinder.ts +175 -0
  145. package/src/libs/player/DTPlayer.element.ts +9 -0
  146. package/src/libs/player/DTPlayer.manager.ts +84 -0
  147. package/src/types/core.ts +169 -0
  148. package/src/types/index.ts +2 -0
  149. package/src/types/player.ts +6 -0
  150. package/test/core/DTAction.double.ts +78 -0
  151. package/test/core/DTAction.spec.ts +76 -0
  152. package/test/core/DTBunch.double.ts +261 -0
  153. package/test/core/DTBunch.spec.ts +897 -0
  154. package/test/core/DTComponent.double.ts +164 -0
  155. package/test/core/DTComponent.spec.ts +295 -0
  156. package/test/core/DTComponentPhysical.double.ts +76 -0
  157. package/test/core/DTComponentPhysical.spec.ts +78 -0
  158. package/test/core/DTComponentWithMeta.double.ts +115 -0
  159. package/test/core/DTComponentWithMeta.spec.ts +124 -0
  160. package/test/core/DTElement.double.ts +163 -0
  161. package/test/core/DTElement.spec.ts +146 -0
  162. package/test/core/DTError.double.ts +92 -0
  163. package/test/core/DTError.spec.ts +89 -0
  164. package/test/core/DTManager.double.ts +216 -0
  165. package/test/core/DTManager.spec.ts +965 -0
  166. package/test/core/DTMaster.double.ts +141 -0
  167. package/test/core/DTMaster.spec.ts +584 -0
  168. package/test/core/DTModule.double.ts +80 -0
  169. package/test/core/DTModule.spec.ts +138 -0
  170. package/test/core/copy.spec.ts +243 -0
  171. package/test/libs/DYOFinder.double.ts +152 -0
  172. package/test/libs/DYOFinder.spec.ts +194 -0
  173. package/test/libs/player/DTPlayer.element.double.ts +55 -0
  174. package/test/libs/player/DTPlayer.element.spec.ts +28 -0
  175. package/test/libs/player/DTPlayer.manager.double.ts +92 -0
  176. package/test/libs/player/DTPlayer.manager.spec.ts +178 -0
  177. package/tsconfig.dev.json +22 -0
  178. package/tsconfig.json +21 -0
  179. package/dist/core/DTPlayer.d.ts +0 -8
  180. package/dist/core/DTPlayer.js +0 -30
  181. package/dist/core/DTPlayer.js.map +0 -1
  182. package/dist/utils/filters.d.ts +0 -6
  183. package/dist/utils/filters.js +0 -39
  184. package/dist/utils/filters.js.map +0 -1
@@ -0,0 +1,52 @@
1
+ import DYOToolsComponent from './DTComponent';
2
+ import { DTComponentToObject } from '../types';
3
+ import DYOToolsMaster from './DTMaster';
4
+
5
+ export default abstract class DYOToolsAction<Payload = unknown> extends DYOToolsComponent {
6
+ /**
7
+ * Defining component type to "action".
8
+ */
9
+ protected _componentType: string = 'action';
10
+
11
+ /**
12
+ * Returns Master component from context, or undefined.
13
+ */
14
+ getMaster(): DYOToolsMaster | undefined {
15
+ return this.getContext<DYOToolsMaster>('master');
16
+ }
17
+
18
+ /**
19
+ * Abstract method for action execution process.
20
+ *
21
+ * @param payload Object Payload to pass for action execution.
22
+ * @returns Promise
23
+ */
24
+ abstract execute(payload: Payload): Promise<void>;
25
+
26
+ /**
27
+ * Return String representation of the Action instance.
28
+ *
29
+ * @returns String representation of the Action.
30
+ */
31
+ toString(): string {
32
+ return `Component ${this._key} - Type: Action`;
33
+ }
34
+
35
+ /**
36
+ * Return JSON Object representation of the Action instance.
37
+ *
38
+ * JSON Object returned has the following structure :
39
+ * * **id** : _id property of the Action.
40
+ * * **key** : _key property of the Action.
41
+ * * **type** : _componentType property of the Action.
42
+ *
43
+ * @returns JSON Object representation of the Action.
44
+ */
45
+ toObject(): DTComponentToObject {
46
+ return {
47
+ id: this._id,
48
+ key: this._key,
49
+ type: this._componentType,
50
+ };
51
+ }
52
+ }
@@ -0,0 +1,467 @@
1
+ import DYOToolsElement from './DTElement';
2
+ import DYOToolsManager from './DTManager';
3
+ import {
4
+ DTAcceptedMetaData, DTBunchFilters, DTBunchOptions, DTBunchToObject, DYOFinderConfiguration,
5
+ } from '../types';
6
+ import DYOToolsError from './DTError';
7
+ import DYOToolsComponentPhysical from './DTComponentPhysical';
8
+ import { bunchDefaultOptions as defaultOptions, componentBunchDefaultFinderConfiguration } from '../constants';
9
+ import DYOFinder from '../libs/DYOFinder';
10
+
11
+ export default class DYOToolsBunch<
12
+ IBunchItem extends DYOToolsElement<DTAcceptedMetaData> = DYOToolsElement<DTAcceptedMetaData>,
13
+ IComponentMeta extends DTAcceptedMetaData = DTAcceptedMetaData,
14
+ > extends DYOToolsComponentPhysical<IComponentMeta, DTBunchOptions> {
15
+ /**
16
+ * Defining component type to "bunch".
17
+ */
18
+ protected _componentType = 'bunch';
19
+
20
+ /**
21
+ * Ordered Array of DTElement instance managed by the bunch.
22
+ */
23
+ protected _items: IBunchItem[];
24
+
25
+ /**
26
+ * Current DYOFinder instance.
27
+ *
28
+ * This instance offers advanced methods to manipulate items, like searching.
29
+ */
30
+ protected _finder: DYOFinder;
31
+
32
+ /**
33
+ * Applying the parent constructor, and execute following process steps :
34
+ * * Add **items** to the bunch instance (using adding specifications).
35
+ * * Merge specific **options** configuration with default in _options.
36
+ * * Initialize *DYOFinder* with **getFinderConfiguration** method.
37
+ *
38
+ * @see [addAtIndex](#addAtIndex) method for adding specifications.
39
+ * @param key
40
+ * @param items Array of DTElement instance to add. Default empty array.
41
+ * @param options Specific options configuration for the instance. Default empty object.
42
+ */
43
+ constructor(key?: string, items: IBunchItem[] = [], options: Partial<DTBunchOptions> = {}) {
44
+ super(key, { ...defaultOptions, ...options });
45
+
46
+ this._items = [];
47
+ if (items && items.length > 0) {
48
+ this.addMany(items);
49
+ }
50
+
51
+ this._finder = new DYOFinder(this, this.getFinderConfiguration());
52
+ }
53
+
54
+ /**
55
+ * Returns DYOFinder configuration for standard DTBunch instance.
56
+ *
57
+ * This method can be overridden to extend the configuration.
58
+ *
59
+ * @returns DYOFinderConfiguration standard configuration.
60
+ */
61
+ getFinderConfiguration(): DYOFinderConfiguration {
62
+ return componentBunchDefaultFinderConfiguration;
63
+ }
64
+
65
+ /**
66
+ * Setter for _owner property.
67
+ *
68
+ * If **inheritOwner** is *true*, apply new **owner** to each item.
69
+ */
70
+ setOwner(value: string): void {
71
+ super.setOwner(value);
72
+
73
+ // Update owner elements
74
+ const { inheritOwner } = this._options;
75
+ if (inheritOwner) {
76
+ this._items.forEach((item) => { item.setOwner(this.getOwner()); });
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Remove the current owner of bunch.
82
+ *
83
+ * If **inheritOwner** is *true*, remove current owner to each item.
84
+ */
85
+ removeOwner(): void {
86
+ super.removeOwner();
87
+
88
+ // Update owner elements
89
+ const { inheritOwner } = this._options;
90
+ if (inheritOwner) {
91
+ this._items.forEach((item) => { item.removeOwner(); });
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Add an element **item** as the last element into _items property array.
97
+ *
98
+ * @see [addAtIndex](#addAtIndex) method for adding specifications.
99
+ * @param item A DTElement instance to add into the bunch.
100
+ * @param options Optional Bunch option configuration object to apply only for this method execution. Options are not
101
+ * saved in current _options property. Available Options are : **uniqueKey**, **inheritOwner**, **replaceIndex**
102
+ * and **errors**.
103
+ */
104
+ add(item: IBunchItem, options: Partial<Omit<DTBunchOptions, 'virtualContainer'>> = {}): void {
105
+ this.addAtIndex(item, this._items.length, options);
106
+ }
107
+
108
+ /**
109
+ * Add an element **item** at specified **index** into _items property array.
110
+ *
111
+ * The adding process has the following specifications :
112
+ * * If the added item has the same _id than existing item, an error occurred (depending on **errors** option).
113
+ * * Option **uniqueKey** = *true*. If the added item has the same _key than existing item,
114
+ * an error occurred (depending on **errors** option).
115
+ * * Option **inheritOwner** = *true*. When the new item is added, its owner is replaced by the current bunch owner.
116
+ * * Option **virtualContainer** = *false*. When the new item is added, its container is replaced by the current bunch
117
+ * instance. The item is removed from the old container.
118
+ * * If an item already exists at the specified index, the new item is added at the index, and following items are
119
+ * automatically affected at next indexes. If **replaceIndex** option is *true*, the new item replaces the former one
120
+ * at the index instead.
121
+ * * If the bunch has a parent **Manager**, the added item is also added to the **Manager library**, only if this one
122
+ * doesn't already exist in the library.
123
+ *
124
+ * @param item A DTElement instance to add into the bunch.
125
+ * @param index Index value where the item might be added. Must be a number between 0 and the current _items length.
126
+ * If not, the provided argument is automatically changed to 0 or current _items length.
127
+ * @param options Optional Bunch option configuration object to apply only for this method execution. Options are not
128
+ * saved in current _options property. Available Options are : **uniqueKey**, **inheritOwner**, **replaceIndex**
129
+ * and **errors**.
130
+ */
131
+ addAtIndex(item: IBunchItem, index: number, options: Partial<Omit<DTBunchOptions, ''>> = {}): void {
132
+ const {
133
+ uniqueKey, replaceIndex, inheritOwner, virtualContainer,
134
+ }: Partial<DTBunchOptions> = { ...this._options, ...options };
135
+ let hasError = false;
136
+ let finalIndex = index;
137
+
138
+ // Handle ID conflicts
139
+ const existingItem = this.get(item.getId());
140
+ if (existingItem) {
141
+ hasError = true;
142
+ this.triggerError(new DYOToolsError(
143
+ 'id_conflict',
144
+ 'Element with same id already exists in the bunch',
145
+ this,
146
+ item,
147
+ ));
148
+ }
149
+
150
+ // Handle Key conflicts
151
+ if (uniqueKey && !hasError) {
152
+ const existingItemByKey = this.find({ key: { $eq: item.getKey() } });
153
+ if (existingItemByKey) {
154
+ hasError = true;
155
+ this.triggerError(new DYOToolsError(
156
+ 'key_conflict',
157
+ 'Element with same key already exists in the bunch',
158
+ this,
159
+ item,
160
+ ));
161
+ }
162
+ }
163
+
164
+ if (!hasError) {
165
+ // Update indexes if out of limits
166
+ if (index < 0) {
167
+ finalIndex = 0;
168
+ }
169
+ if (index > this._items.length) {
170
+ finalIndex = this._items.length;
171
+ }
172
+
173
+ // Update Context with manager
174
+ if (this._context) {
175
+ item.setContext(this._context);
176
+ }
177
+
178
+ if (!virtualContainer) {
179
+ const oldContainer = item.getContainer();
180
+ if (oldContainer && oldContainer.getComponentType() === 'bunch') {
181
+ (oldContainer as DYOToolsBunch<IBunchItem, DTAcceptedMetaData>).remove(item.getId());
182
+ }
183
+ item.setContainer(this);
184
+ }
185
+
186
+ // Update Owner
187
+ if (inheritOwner) {
188
+ item.setOwner(this._owner);
189
+ }
190
+
191
+ // Update Manager library
192
+ if (this.getContext('manager')) {
193
+ const manager: DYOToolsManager<IBunchItem> = this.getContext('manager') as DYOToolsManager<IBunchItem>;
194
+ if (!manager.getLibrary().get(item.getId())) {
195
+ manager.getLibrary().add(item);
196
+ }
197
+ }
198
+
199
+ // Add the new Item
200
+ if (replaceIndex) {
201
+ this._items[finalIndex] = item;
202
+ } else {
203
+ const arrayPart1 = this._items.slice(0, finalIndex);
204
+ const arrayPart2 = this._items.slice(finalIndex);
205
+
206
+ this._items = [...arrayPart1, item, ...arrayPart2];
207
+ }
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Add each element of an array **items** at the end of the _items property array.
213
+ *
214
+ * @see [addAtIndex](#addAtIndex) method for adding specifications.
215
+ * @param items An array of DTElement instances to add into the bunch.
216
+ * @param options Optional Bunch option configuration object to apply only for this method execution. Options are not
217
+ * saved in current _options property. Available Options are : **uniqueKey**, **inheritOwner**, **replaceIndex**
218
+ * and **errors**.
219
+ */
220
+ addMany(items: IBunchItem[], options: Partial<Omit<DTBunchOptions, 'virtualContainer'>> = {}): void {
221
+ this.addManyAtIndex(items, this._items.length, options);
222
+ }
223
+
224
+ /**
225
+ * Add each element of an array **items** at specified **index** into _items property array.
226
+ * The first element is added at provided **index** argument, and each next element at next indexes, following adding
227
+ * specifications.
228
+ *
229
+ * @see [addAtIndex](#addAtIndex) method for adding specifications.
230
+ * @param items An array of DTElement instances to add into the bunch.
231
+ * @param index Index value where the item might be added. Must be a number between 0 and the current _items length.
232
+ * If not, the provided argument is automatically changed to 0 or current _items length.
233
+ * @param options Optional Bunch option configuration object to apply only for this method execution. Options are not
234
+ * saved in current _options property. Available Options are : **uniqueKey**, **inheritOwner**, **replaceIndex**
235
+ * and **errors**.
236
+ */
237
+ addManyAtIndex(items: IBunchItem[], index: number, options: Partial<Omit<DTBunchOptions, 'virtualContainer'>> = {}): void {
238
+ const previousItems = this._items;
239
+ const { errors }: Partial<DTBunchOptions> = { ...this._options, ...options };
240
+ let currentIndex = index;
241
+
242
+ if (index < 0) {
243
+ currentIndex = 0;
244
+ }
245
+
246
+ try {
247
+ for (const item of items) {
248
+ this.addAtIndex(item, currentIndex, options);
249
+ currentIndex += 1;
250
+ }
251
+ } catch (exception) {
252
+ if (!errors) {
253
+ this._items = previousItems;
254
+ throw exception;
255
+ }
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Return one DTElement instance included in the _items property by index or id.
261
+ *
262
+ * * If a Number is provided, return the DTElement instance at the corresponding index into _items.
263
+ * * If a String is provided, return the DTElement instance with the corresponding _id property into _items.
264
+ *
265
+ * @param index Number index value or String _id value.
266
+ * @returns DTElement instance that corresponds to index or id provided, or undefined if not found.
267
+ */
268
+ get(index: string | number): IBunchItem | undefined {
269
+ if (typeof index === 'number') {
270
+ return this._items[index];
271
+ }
272
+ const itemFiltered = this._items.filter((item: IBunchItem) => item.getId() === index);
273
+ return itemFiltered.length > 0 ? itemFiltered[0] : undefined;
274
+ }
275
+
276
+ /**
277
+ * Return all DTElement instance managed by the Bunch.
278
+ *
279
+ * @returns DTElement array corresponding to current _items property.
280
+ */
281
+ getAll(): IBunchItem[] {
282
+ return this._items;
283
+ }
284
+
285
+ /**
286
+ * Return current index of a DTElement instance into _items property by _id.
287
+ *
288
+ * @param id String _id value of the DTElement instance.
289
+ * @returns Current index number into _items, or -1 if not found.
290
+ */
291
+ indexOf(id: string): number {
292
+ let indexOfItem = -1;
293
+ for (let i = 0; i < this._items.length; i += 1) {
294
+ if (this._items[i].getId() === id) {
295
+ indexOfItem = i;
296
+ break;
297
+ }
298
+ }
299
+ return indexOfItem;
300
+ }
301
+
302
+ /**
303
+ * Remove a DTElement instance into the _items property by index or id.
304
+ *
305
+ * * If a Number is provided, remove the DTElement instance at the corresponding index into _items.
306
+ * * If a String is provided, remove the DTElement instance with the corresponding _id property into _items.
307
+ *
308
+ * Note : Remove also affectation to the current container of the removed item
309
+ * (only if Option **virtualContainer** is *false*).
310
+ *
311
+ * @param index Number index value or String _id value.
312
+ */
313
+ remove(index: string | number): void {
314
+ if (typeof index === 'number') {
315
+ this.removeMany([index as number]);
316
+ } else {
317
+ this.removeMany([index as string]);
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Remove multiple DTElement instances into the _items property by index or id. An array of indexes or ids to remove
323
+ * must be provided.
324
+ *
325
+ * * If a Number Array is provided, remove DTElement instances at corresponding indexes into _items.
326
+ * * If a String Array is provided, remove DTElement instances with corresponding _id properties into _items.
327
+ *
328
+ * Note : Remove also affectation to the current container of removed items
329
+ * (only if Option **virtualContainer** is *false*).
330
+ *
331
+ * @param indexes Number Array index values or String Array _id values.
332
+ */
333
+ removeMany(indexes: string[] | number[]): void {
334
+ const { virtualContainer } = this._options;
335
+ const newItems = [];
336
+ for (let i = 0; i < this._items.length; i += 1) {
337
+ if (typeof indexes[0] === 'number') {
338
+ if (!(indexes as number[]).includes(i)) {
339
+ newItems.push(this._items[i]);
340
+ } else if (!virtualContainer) {
341
+ this._items[i].removeContainer();
342
+ }
343
+ } else if (!(indexes as string[]).includes(this._items[i].getId())) {
344
+ newItems.push(this._items[i]);
345
+ } else if (!virtualContainer) {
346
+ this._items[i].removeContainer();
347
+ }
348
+ }
349
+
350
+ this._items = newItems;
351
+ }
352
+
353
+ /**
354
+ * Remove all DTElement instances into the _items property.
355
+ *
356
+ * Note : Remove also affectation to the current container of removed items
357
+ * (only if Option **virtualContainer** is *false*).
358
+ */
359
+ removeAll(): void {
360
+ const keysToRemove: number[] = this._items.map((item, index) => index);
361
+ this.removeMany(keysToRemove);
362
+ }
363
+
364
+ /**
365
+ * Return an array of DTElement from _items property filtered with a **filters** argument.
366
+ *
367
+ * This method use the DYOFinder instance **execute** method.
368
+ *
369
+ * Search filters can be applied on following DTElement properties :
370
+ * * **id** : property _id. Basic operators only.
371
+ * * **key** : property _key. Basic operators only.
372
+ * * **container** : property _id of current _container instance. Basic operators only.
373
+ * * **owner** : property _owner. Basic operators only.
374
+ * * **meta** : each meta Key of _meta property. Extended operators can be used.
375
+ *
376
+ * Examples of **filters** argument :
377
+ * * { key: { $eq: "key_1" } } : Return all DTElement instance into _items with *key_1* as _key property.
378
+ * * { context: { $in: [null, "bunch_1"] } } : Return all DTElement instance into _items having no context or a
379
+ * bunch context with *bunch_1* as _id property.
380
+ * * { key: { $ne: "key_1" }, meta: { score: { $gte: 50, $lte: 100 } } } : Return all DTElement instance into _items
381
+ * with _key property different than *key_1*, and meta key *score* value from _meta property between 50 and 100.
382
+ *
383
+ * @param filters Filters Object. The format is :
384
+ * { [property_1] : { [operator_1] : filter_value, [operator_2] : filter_value_2, ... }, [property_2] : { ... }, ... }
385
+ *
386
+ * For **meta**, you have to pass the meta key before the operator :
387
+ * { meta: { [meta_key1] : { [operator_1] : filter_value_1, ... }, [meta_key2] : { ... }, ... }, ... }
388
+ * @returns Array of DTElement instance corresponding to the filters. Empty if no filter or invalid ones are passed.
389
+ * @see DYOFinder
390
+ */
391
+ find(filters: Partial<DTBunchFilters>): IBunchItem[] {
392
+ return this._finder.execute<IBunchItem>(filters);
393
+ }
394
+
395
+ /**
396
+ * Create and return a new DTBunch instance by applying from current instance :
397
+ * - Copy _key property
398
+ * - Copy _meta property
399
+ * - Copy _globalOptions property
400
+ * - Make a copy of each element in _items, and add it into _items of the copied Bunch.
401
+ *
402
+ * @returns New DTBunch instance copied.
403
+ */
404
+ copy(): DYOToolsBunch<IBunchItem, IComponentMeta> {
405
+ let copyItems;
406
+ if (this._options.virtualContainer) {
407
+ copyItems = this._items;
408
+ } else {
409
+ copyItems = this._items.length === 0 ? [] : this._items.map((item) => item.copy() as IBunchItem);
410
+ }
411
+
412
+ const copyBunch = new DYOToolsBunch<IBunchItem, IComponentMeta>(this._key, copyItems, this._options);
413
+ copyBunch.setManyMeta({ ...this.getManyMeta() });
414
+
415
+ return copyBunch;
416
+ }
417
+
418
+ /**
419
+ * Return JSON Object representation of the Bunch instance.
420
+ *
421
+ * JSON Object returned has the following structure :
422
+ * * **id** : _id property of the Bunch.
423
+ * * **key** : _key property of the Bunch.
424
+ * * **type** : _componentType property of the Bunch.
425
+ * * **items** : Array of JSON Object representation for each DTElement instance in _items property of the Bunch.
426
+ * * **owner** : String current value of _owner property of the Bunch (only if defined).
427
+ * * **meta** : JSON Object of all current metadata in _meta property of the Bunch (only if not empty).
428
+ *
429
+ * @returns JSON Object representation of the Bunch.
430
+ */
431
+ toObject(): DTBunchToObject<IComponentMeta> {
432
+ const objectBunch: DTBunchToObject<IComponentMeta> = {
433
+ id: this._id,
434
+ key: this._key,
435
+ type: this._componentType,
436
+ items: [],
437
+ };
438
+
439
+ if (this._items.length) {
440
+ objectBunch.items = this._items.map((item) => item.toObject());
441
+ }
442
+
443
+ if (this._owner) {
444
+ objectBunch.owner = this._owner.toString();
445
+ }
446
+
447
+ if (this._meta && Object.keys(this._meta).length > 0) {
448
+ objectBunch.meta = { ...this.getManyMeta() };
449
+ }
450
+
451
+ return objectBunch;
452
+ }
453
+
454
+ /**
455
+ * Return String representation of the Bunch instance.
456
+ *
457
+ * @returns String representation of the Bunch.
458
+ */
459
+ toString(): string {
460
+ let ownerKey = '';
461
+ if (this._owner) {
462
+ ownerKey = ` - Owner: ${this._owner}`;
463
+ }
464
+
465
+ return `Component ${this._key} - Type: Bunch${ownerKey} - Items: ${this._items.length}`;
466
+ }
467
+ }