@ckeditor/ckeditor5-utils 39.0.2 → 40.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 (131) hide show
  1. package/package.json +1 -1
  2. package/src/areconnectedthroughproperties.d.ts +11 -11
  3. package/src/areconnectedthroughproperties.js +73 -73
  4. package/src/ckeditorerror.d.ts +123 -123
  5. package/src/ckeditorerror.js +176 -176
  6. package/src/collection.d.ts +427 -427
  7. package/src/collection.js +575 -575
  8. package/src/comparearrays.d.ts +30 -30
  9. package/src/comparearrays.js +47 -47
  10. package/src/config.d.ts +163 -163
  11. package/src/config.js +162 -162
  12. package/src/count.d.ts +18 -18
  13. package/src/count.js +24 -24
  14. package/src/delay.d.ts +19 -19
  15. package/src/delay.js +26 -26
  16. package/src/diff.d.ts +31 -31
  17. package/src/diff.js +115 -115
  18. package/src/difftochanges.d.ts +59 -59
  19. package/src/difftochanges.js +79 -79
  20. package/src/dom/createelement.d.ts +57 -57
  21. package/src/dom/createelement.js +40 -40
  22. package/src/dom/emittermixin.d.ts +142 -142
  23. package/src/dom/emittermixin.js +239 -239
  24. package/src/dom/findclosestscrollableancestor.d.ts +11 -11
  25. package/src/dom/findclosestscrollableancestor.js +31 -31
  26. package/src/dom/getancestors.d.ts +17 -17
  27. package/src/dom/getancestors.js +27 -27
  28. package/src/dom/getborderwidths.d.ts +24 -24
  29. package/src/dom/getborderwidths.js +24 -24
  30. package/src/dom/getcommonancestor.d.ts +12 -12
  31. package/src/dom/getcommonancestor.js +25 -25
  32. package/src/dom/getdatafromelement.d.ts +14 -14
  33. package/src/dom/getdatafromelement.js +20 -20
  34. package/src/dom/getpositionedancestor.d.ts +10 -10
  35. package/src/dom/getpositionedancestor.js +22 -22
  36. package/src/dom/global.d.ts +32 -32
  37. package/src/dom/global.js +35 -35
  38. package/src/dom/indexof.d.ts +14 -14
  39. package/src/dom/indexof.js +21 -21
  40. package/src/dom/insertat.d.ts +15 -15
  41. package/src/dom/insertat.js +17 -17
  42. package/src/dom/iscomment.d.ts +11 -11
  43. package/src/dom/iscomment.js +14 -14
  44. package/src/dom/isnode.d.ts +11 -11
  45. package/src/dom/isnode.js +21 -21
  46. package/src/dom/isrange.d.ts +11 -11
  47. package/src/dom/isrange.js +13 -13
  48. package/src/dom/istext.d.ts +11 -11
  49. package/src/dom/istext.js +13 -13
  50. package/src/dom/isvalidattributename.d.ts +10 -10
  51. package/src/dom/isvalidattributename.js +22 -22
  52. package/src/dom/isvisible.d.ts +18 -18
  53. package/src/dom/isvisible.js +20 -20
  54. package/src/dom/iswindow.d.ts +11 -11
  55. package/src/dom/iswindow.js +22 -22
  56. package/src/dom/position.d.ts +211 -208
  57. package/src/dom/position.js +313 -305
  58. package/src/dom/rect.d.ts +195 -191
  59. package/src/dom/rect.js +474 -388
  60. package/src/dom/remove.d.ts +13 -13
  61. package/src/dom/remove.js +18 -18
  62. package/src/dom/resizeobserver.d.ts +74 -74
  63. package/src/dom/resizeobserver.js +126 -126
  64. package/src/dom/scroll.d.ts +73 -73
  65. package/src/dom/scroll.js +383 -383
  66. package/src/dom/setdatainelement.d.ts +14 -14
  67. package/src/dom/setdatainelement.js +20 -20
  68. package/src/dom/tounit.d.ts +22 -22
  69. package/src/dom/tounit.js +16 -16
  70. package/src/elementreplacer.d.ts +31 -31
  71. package/src/elementreplacer.js +43 -43
  72. package/src/emittermixin.d.ts +312 -312
  73. package/src/emittermixin.js +453 -453
  74. package/src/env.d.ts +117 -117
  75. package/src/env.js +122 -122
  76. package/src/eventinfo.d.ts +58 -58
  77. package/src/eventinfo.js +26 -26
  78. package/src/fastdiff.d.ts +112 -112
  79. package/src/fastdiff.js +248 -248
  80. package/src/first.d.ts +11 -11
  81. package/src/first.js +17 -17
  82. package/src/focustracker.d.ts +75 -75
  83. package/src/focustracker.js +95 -95
  84. package/src/index.d.ts +61 -62
  85. package/src/index.js +60 -61
  86. package/src/inserttopriorityarray.d.ts +30 -30
  87. package/src/inserttopriorityarray.js +21 -21
  88. package/src/isiterable.d.ts +14 -14
  89. package/src/isiterable.js +16 -16
  90. package/src/keyboard.d.ts +126 -126
  91. package/src/keyboard.js +221 -221
  92. package/src/keystrokehandler.d.ts +87 -87
  93. package/src/keystrokehandler.js +122 -122
  94. package/src/language.d.ts +17 -17
  95. package/src/language.js +19 -19
  96. package/src/locale.d.ts +120 -120
  97. package/src/locale.js +76 -76
  98. package/src/mapsequal.d.ts +15 -15
  99. package/src/mapsequal.js +27 -27
  100. package/src/mix.d.ts +85 -85
  101. package/src/mix.js +50 -50
  102. package/src/nth.d.ts +16 -16
  103. package/src/nth.js +24 -24
  104. package/src/objecttomap.d.ts +23 -23
  105. package/src/objecttomap.js +27 -27
  106. package/src/observablemixin.d.ts +560 -560
  107. package/src/observablemixin.js +580 -580
  108. package/src/priorities.d.ts +33 -33
  109. package/src/priorities.js +23 -23
  110. package/src/splicearray.d.ts +26 -26
  111. package/src/splicearray.js +40 -40
  112. package/src/spy.d.ts +21 -21
  113. package/src/spy.js +22 -22
  114. package/src/toarray.d.ts +25 -25
  115. package/src/toarray.js +7 -7
  116. package/src/tomap.d.ts +19 -19
  117. package/src/tomap.js +29 -29
  118. package/src/translation-service.d.ts +168 -168
  119. package/src/translation-service.js +198 -198
  120. package/src/uid.d.ts +15 -15
  121. package/src/uid.js +57 -57
  122. package/src/unicode.d.ts +54 -54
  123. package/src/unicode.js +85 -85
  124. package/src/verifylicense.d.ts +15 -15
  125. package/src/verifylicense.js +87 -87
  126. package/src/version.d.ts +10 -10
  127. package/src/version.js +153 -153
  128. package/src/dom/getelementsintersectionrect.d.ts +0 -14
  129. package/src/dom/getelementsintersectionrect.js +0 -43
  130. package/src/dom/getscrollableancestors.d.ts +0 -14
  131. package/src/dom/getscrollableancestors.js +0 -28
@@ -1,427 +1,427 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- declare const Collection_base: {
6
- new (): import("./emittermixin").Emitter;
7
- prototype: import("./emittermixin").Emitter;
8
- };
9
- /**
10
- * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
11
- * in the collection (like in an array) or by their ids.
12
- *
13
- * If an object without an `id` property is being added to the collection, the `id` property will be generated
14
- * automatically. Note that the automatically generated id is unique only within this single collection instance.
15
- *
16
- * By default an item in the collection is identified by its `id` property. The name of the identifier can be
17
- * configured through the constructor of the collection.
18
- *
19
- * @typeParam T The type of the collection element.
20
- */
21
- export default class Collection<T extends Record<string, any>> extends Collection_base implements Iterable<T> {
22
- /**
23
- * The internal list of items in the collection.
24
- */
25
- private readonly _items;
26
- /**
27
- * The internal map of items in the collection.
28
- */
29
- private readonly _itemMap;
30
- /**
31
- * The name of the property which is considered to identify an item.
32
- */
33
- private readonly _idProperty;
34
- /**
35
- * A collection instance this collection is bound to as a result
36
- * of calling {@link #bindTo} method.
37
- */
38
- private _bindToCollection?;
39
- /**
40
- * A helper mapping external items of a bound collection ({@link #bindTo})
41
- * and actual items of this collection. It provides information
42
- * necessary to properly remove items bound to another collection.
43
- *
44
- * See {@link #_bindToInternalToExternalMap}.
45
- */
46
- private readonly _bindToExternalToInternalMap;
47
- /**
48
- * A helper mapping items of this collection to external items of a bound collection
49
- * ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
50
- * to avoid loops in two–way bindings.
51
- *
52
- * See {@link #_bindToExternalToInternalMap}.
53
- */
54
- private readonly _bindToInternalToExternalMap;
55
- /**
56
- * Stores indexes of skipped items from bound external collection.
57
- */
58
- private _skippedIndexesFromExternal;
59
- /**
60
- * Creates a new Collection instance.
61
- *
62
- * You can pass a configuration object as the argument of the constructor:
63
- *
64
- * ```ts
65
- * const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
66
- * emptyCollection.add( { name: 'John' } );
67
- * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
68
- * ```
69
- *
70
- * The collection is empty by default. You can add new items using the {@link #add} method:
71
- *
72
- * ```ts
73
- * const collection = new Collection<{ id: string }>();
74
- *
75
- * collection.add( { id: 'John' } );
76
- * console.log( collection.get( 0 ) ); // -> { id: 'John' }
77
- * ```
78
- *
79
- * @label NO_ITEMS
80
- * @param options The options object.
81
- * @param options.idProperty The name of the property which is used to identify an item.
82
- * Items that do not have such a property will be assigned one when added to the collection.
83
- */
84
- constructor(options?: {
85
- readonly idProperty?: string;
86
- });
87
- /**
88
- * Creates a new Collection instance with specified initial items.
89
- *
90
- * ```ts
91
- * const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );
92
- *
93
- * console.log( collection.get( 0 ) ); // -> { id: 'John' }
94
- * console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
95
- * console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
96
- * ```
97
- *
98
- * You can always pass a configuration object as the last argument of the constructor:
99
- *
100
- * ```ts
101
- * const nonEmptyCollection = new Collection<{ name: string }>( [ { name: 'John' } ], { idProperty: 'name' } );
102
- * nonEmptyCollection.add( { name: 'George' } );
103
- * console.log( collection.get( 'George' ) ); // -> { name: 'George' }
104
- * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
105
- * ```
106
- *
107
- * @label INITIAL_ITEMS
108
- * @param initialItems The initial items of the collection.
109
- * @param options The options object.
110
- * @param options.idProperty The name of the property which is used to identify an item.
111
- * Items that do not have such a property will be assigned one when added to the collection.
112
- */
113
- constructor(initialItems: Iterable<T>, options?: {
114
- readonly idProperty?: string;
115
- });
116
- /**
117
- * The number of items available in the collection.
118
- */
119
- get length(): number;
120
- /**
121
- * Returns the first item from the collection or null when collection is empty.
122
- */
123
- get first(): T | null;
124
- /**
125
- * Returns the last item from the collection or null when collection is empty.
126
- */
127
- get last(): T | null;
128
- /**
129
- * Adds an item into the collection.
130
- *
131
- * If the item does not have an id, then it will be automatically generated and set on the item.
132
- *
133
- * @param item
134
- * @param index The position of the item in the collection. The item
135
- * is pushed to the collection when `index` not specified.
136
- * @fires add
137
- * @fires change
138
- */
139
- add(item: T, index?: number): this;
140
- /**
141
- * Adds multiple items into the collection.
142
- *
143
- * Any item not containing an id will get an automatically generated one.
144
- *
145
- * @param items
146
- * @param index The position of the insertion. Items will be appended if no `index` is specified.
147
- * @fires add
148
- * @fires change
149
- */
150
- addMany(items: Iterable<T>, index?: number): this;
151
- /**
152
- * Gets an item by its ID or index.
153
- *
154
- * @param idOrIndex The item ID or index in the collection.
155
- * @returns The requested item or `null` if such item does not exist.
156
- */
157
- get(idOrIndex: string | number): T | null;
158
- /**
159
- * Returns a Boolean indicating whether the collection contains an item.
160
- *
161
- * @param itemOrId The item or its ID in the collection.
162
- * @returns `true` if the collection contains the item, `false` otherwise.
163
- */
164
- has(itemOrId: T | string): boolean;
165
- /**
166
- * Gets an index of an item in the collection.
167
- * When an item is not defined in the collection, the index will equal -1.
168
- *
169
- * @param itemOrId The item or its ID in the collection.
170
- * @returns The index of a given item.
171
- */
172
- getIndex(itemOrId: T | string): number;
173
- /**
174
- * Removes an item from the collection.
175
- *
176
- * @param subject The item to remove, its ID or index in the collection.
177
- * @returns The removed item.
178
- * @fires remove
179
- * @fires change
180
- */
181
- remove(subject: T | number | string): T;
182
- /**
183
- * Executes the callback for each item in the collection and composes an array or values returned by this callback.
184
- *
185
- * @typeParam U The result type of the callback.
186
- * @param callback
187
- * @param ctx Context in which the `callback` will be called.
188
- * @returns The result of mapping.
189
- */
190
- map<U>(callback: (item: T, index: number) => U, ctx?: any): Array<U>;
191
- /**
192
- * Finds the first item in the collection for which the `callback` returns a true value.
193
- *
194
- * @param callback
195
- * @param ctx Context in which the `callback` will be called.
196
- * @returns The item for which `callback` returned a true value.
197
- */
198
- find(callback: (item: T, index: number) => boolean, ctx?: any): T | undefined;
199
- /**
200
- * Returns an array with items for which the `callback` returned a true value.
201
- *
202
- * @param callback
203
- * @param ctx Context in which the `callback` will be called.
204
- * @returns The array with matching items.
205
- */
206
- filter(callback: (item: T, index: number) => boolean, ctx?: any): Array<T>;
207
- /**
208
- * Removes all items from the collection and destroys the binding created using
209
- * {@link #bindTo}.
210
- *
211
- * @fires remove
212
- * @fires change
213
- */
214
- clear(): void;
215
- /**
216
- * Binds and synchronizes the collection with another one.
217
- *
218
- * The binding can be a simple factory:
219
- *
220
- * ```ts
221
- * class FactoryClass {
222
- * public label: string;
223
- *
224
- * constructor( data: { label: string } ) {
225
- * this.label = data.label;
226
- * }
227
- * }
228
- *
229
- * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
230
- * const target = new Collection<FactoryClass>();
231
- *
232
- * target.bindTo( source ).as( FactoryClass );
233
- *
234
- * source.add( { label: 'foo' } );
235
- * source.add( { label: 'bar' } );
236
- *
237
- * console.log( target.length ); // 2
238
- * console.log( target.get( 1 ).label ); // 'bar'
239
- *
240
- * source.remove( 0 );
241
- * console.log( target.length ); // 1
242
- * console.log( target.get( 0 ).label ); // 'bar'
243
- * ```
244
- *
245
- * or the factory driven by a custom callback:
246
- *
247
- * ```ts
248
- * class FooClass {
249
- * public label: string;
250
- *
251
- * constructor( data: { label: string } ) {
252
- * this.label = data.label;
253
- * }
254
- * }
255
- *
256
- * class BarClass {
257
- * public label: string;
258
- *
259
- * constructor( data: { label: string } ) {
260
- * this.label = data.label;
261
- * }
262
- * }
263
- *
264
- * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
265
- * const target = new Collection<FooClass | BarClass>();
266
- *
267
- * target.bindTo( source ).using( ( item ) => {
268
- * if ( item.label == 'foo' ) {
269
- * return new FooClass( item );
270
- * } else {
271
- * return new BarClass( item );
272
- * }
273
- * } );
274
- *
275
- * source.add( { label: 'foo' } );
276
- * source.add( { label: 'bar' } );
277
- *
278
- * console.log( target.length ); // 2
279
- * console.log( target.get( 0 ) instanceof FooClass ); // true
280
- * console.log( target.get( 1 ) instanceof BarClass ); // true
281
- * ```
282
- *
283
- * or the factory out of property name:
284
- *
285
- * ```ts
286
- * const source = new Collection<{ nested: { value: string } }>();
287
- * const target = new Collection<{ value: string }>();
288
- *
289
- * target.bindTo( source ).using( 'nested' );
290
- *
291
- * source.add( { nested: { value: 'foo' } } );
292
- * source.add( { nested: { value: 'bar' } } );
293
- *
294
- * console.log( target.length ); // 2
295
- * console.log( target.get( 0 ).value ); // 'foo'
296
- * console.log( target.get( 1 ).value ); // 'bar'
297
- * ```
298
- *
299
- * It's possible to skip specified items by returning null value:
300
- *
301
- * ```ts
302
- * const source = new Collection<{ hidden: boolean }>();
303
- * const target = new Collection<{ hidden: boolean }>();
304
- *
305
- * target.bindTo( source ).using( item => {
306
- * if ( item.hidden ) {
307
- * return null;
308
- * }
309
- *
310
- * return item;
311
- * } );
312
- *
313
- * source.add( { hidden: true } );
314
- * source.add( { hidden: false } );
315
- *
316
- * console.log( source.length ); // 2
317
- * console.log( target.length ); // 1
318
- * ```
319
- *
320
- * **Note**: {@link #clear} can be used to break the binding.
321
- *
322
- * @typeParam S The type of `externalCollection` element.
323
- * @param externalCollection A collection to be bound.
324
- * @returns The binding chain object.
325
- */
326
- bindTo<S extends Record<string, any>>(externalCollection: Collection<S>): CollectionBindToChain<S, T>;
327
- /**
328
- * Finalizes and activates a binding initiated by {@link #bindTo}.
329
- *
330
- * @param factory A function which produces collection items.
331
- */
332
- private _setUpBindToBinding;
333
- /**
334
- * Returns an unique id property for a given `item`.
335
- *
336
- * The method will generate new id and assign it to the `item` if it doesn't have any.
337
- *
338
- * @param item Item to be added.
339
- */
340
- private _getItemIdBeforeAdding;
341
- /**
342
- * Core {@link #remove} method implementation shared in other functions.
343
- *
344
- * In contrast this method **does not** fire the {@link #event:change} event.
345
- *
346
- * @param subject The item to remove, its id or index in the collection.
347
- * @returns Returns an array with the removed item and its index.
348
- * @fires remove
349
- */
350
- private _remove;
351
- /**
352
- * Iterable interface.
353
- */
354
- [Symbol.iterator](): Iterator<T>;
355
- }
356
- /**
357
- * Fired when an item is added to the collection.
358
- *
359
- * @eventName ~Collection#add
360
- * @param item The added item.
361
- * @param index An index where the addition occurred.
362
- */
363
- export type CollectionAddEvent<T = any> = {
364
- name: 'add';
365
- args: [item: T, index: number];
366
- };
367
- /**
368
- * Fired when the collection was changed due to adding or removing items.
369
- *
370
- * @eventName ~Collection#change
371
- * @param data Changed items.
372
- */
373
- export type CollectionChangeEvent<T = any> = {
374
- name: 'change';
375
- args: [data: CollectionChangeEventData<T>];
376
- };
377
- /**
378
- * A structure describing the {@link ~Collection#event:change `Collection#change`} event.
379
- */
380
- export type CollectionChangeEventData<T = any> = {
381
- /**
382
- * A list of added items.
383
- */
384
- added: Iterable<T>;
385
- /**
386
- * A list of removed items.
387
- */
388
- removed: Iterable<T>;
389
- /**
390
- * An index where the addition or removal occurred.
391
- */
392
- index: number;
393
- };
394
- /**
395
- * Fired when an item is removed from the collection.
396
- *
397
- * @eventName ~Collection#remove
398
- * @param item The removed item.
399
- * @param index Index from which item was removed.
400
- */
401
- export type CollectionRemoveEvent<T = any> = {
402
- name: 'remove';
403
- args: [item: T, index: number];
404
- };
405
- /**
406
- * An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
407
- * providing functions that specify the type of the binding.
408
- *
409
- * See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
410
- */
411
- export interface CollectionBindToChain<S, T> {
412
- /**
413
- * Creates the class factory binding in which items of the source collection are passed to
414
- * the constructor of the specified class.
415
- *
416
- * @param Class The class constructor used to create instances in the factory.
417
- */
418
- as(Class: new (item: S) => T): void;
419
- /**
420
- * Creates a callback or a property binding.
421
- *
422
- * @param callbackOrProperty When the function is passed, it should return
423
- * the collection items. When the string is provided, the property value is used to create the bound collection items.
424
- */
425
- using(callbackOrProperty: keyof S | ((item: S) => T | null)): void;
426
- }
427
- export {};
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ declare const Collection_base: {
6
+ new (): import("./emittermixin").Emitter;
7
+ prototype: import("./emittermixin").Emitter;
8
+ };
9
+ /**
10
+ * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
11
+ * in the collection (like in an array) or by their ids.
12
+ *
13
+ * If an object without an `id` property is being added to the collection, the `id` property will be generated
14
+ * automatically. Note that the automatically generated id is unique only within this single collection instance.
15
+ *
16
+ * By default an item in the collection is identified by its `id` property. The name of the identifier can be
17
+ * configured through the constructor of the collection.
18
+ *
19
+ * @typeParam T The type of the collection element.
20
+ */
21
+ export default class Collection<T extends Record<string, any>> extends Collection_base implements Iterable<T> {
22
+ /**
23
+ * The internal list of items in the collection.
24
+ */
25
+ private readonly _items;
26
+ /**
27
+ * The internal map of items in the collection.
28
+ */
29
+ private readonly _itemMap;
30
+ /**
31
+ * The name of the property which is considered to identify an item.
32
+ */
33
+ private readonly _idProperty;
34
+ /**
35
+ * A collection instance this collection is bound to as a result
36
+ * of calling {@link #bindTo} method.
37
+ */
38
+ private _bindToCollection?;
39
+ /**
40
+ * A helper mapping external items of a bound collection ({@link #bindTo})
41
+ * and actual items of this collection. It provides information
42
+ * necessary to properly remove items bound to another collection.
43
+ *
44
+ * See {@link #_bindToInternalToExternalMap}.
45
+ */
46
+ private readonly _bindToExternalToInternalMap;
47
+ /**
48
+ * A helper mapping items of this collection to external items of a bound collection
49
+ * ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
50
+ * to avoid loops in two–way bindings.
51
+ *
52
+ * See {@link #_bindToExternalToInternalMap}.
53
+ */
54
+ private readonly _bindToInternalToExternalMap;
55
+ /**
56
+ * Stores indexes of skipped items from bound external collection.
57
+ */
58
+ private _skippedIndexesFromExternal;
59
+ /**
60
+ * Creates a new Collection instance.
61
+ *
62
+ * You can pass a configuration object as the argument of the constructor:
63
+ *
64
+ * ```ts
65
+ * const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
66
+ * emptyCollection.add( { name: 'John' } );
67
+ * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
68
+ * ```
69
+ *
70
+ * The collection is empty by default. You can add new items using the {@link #add} method:
71
+ *
72
+ * ```ts
73
+ * const collection = new Collection<{ id: string }>();
74
+ *
75
+ * collection.add( { id: 'John' } );
76
+ * console.log( collection.get( 0 ) ); // -> { id: 'John' }
77
+ * ```
78
+ *
79
+ * @label NO_ITEMS
80
+ * @param options The options object.
81
+ * @param options.idProperty The name of the property which is used to identify an item.
82
+ * Items that do not have such a property will be assigned one when added to the collection.
83
+ */
84
+ constructor(options?: {
85
+ readonly idProperty?: string;
86
+ });
87
+ /**
88
+ * Creates a new Collection instance with specified initial items.
89
+ *
90
+ * ```ts
91
+ * const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );
92
+ *
93
+ * console.log( collection.get( 0 ) ); // -> { id: 'John' }
94
+ * console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
95
+ * console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
96
+ * ```
97
+ *
98
+ * You can always pass a configuration object as the last argument of the constructor:
99
+ *
100
+ * ```ts
101
+ * const nonEmptyCollection = new Collection<{ name: string }>( [ { name: 'John' } ], { idProperty: 'name' } );
102
+ * nonEmptyCollection.add( { name: 'George' } );
103
+ * console.log( collection.get( 'George' ) ); // -> { name: 'George' }
104
+ * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
105
+ * ```
106
+ *
107
+ * @label INITIAL_ITEMS
108
+ * @param initialItems The initial items of the collection.
109
+ * @param options The options object.
110
+ * @param options.idProperty The name of the property which is used to identify an item.
111
+ * Items that do not have such a property will be assigned one when added to the collection.
112
+ */
113
+ constructor(initialItems: Iterable<T>, options?: {
114
+ readonly idProperty?: string;
115
+ });
116
+ /**
117
+ * The number of items available in the collection.
118
+ */
119
+ get length(): number;
120
+ /**
121
+ * Returns the first item from the collection or null when collection is empty.
122
+ */
123
+ get first(): T | null;
124
+ /**
125
+ * Returns the last item from the collection or null when collection is empty.
126
+ */
127
+ get last(): T | null;
128
+ /**
129
+ * Adds an item into the collection.
130
+ *
131
+ * If the item does not have an id, then it will be automatically generated and set on the item.
132
+ *
133
+ * @param item
134
+ * @param index The position of the item in the collection. The item
135
+ * is pushed to the collection when `index` not specified.
136
+ * @fires add
137
+ * @fires change
138
+ */
139
+ add(item: T, index?: number): this;
140
+ /**
141
+ * Adds multiple items into the collection.
142
+ *
143
+ * Any item not containing an id will get an automatically generated one.
144
+ *
145
+ * @param items
146
+ * @param index The position of the insertion. Items will be appended if no `index` is specified.
147
+ * @fires add
148
+ * @fires change
149
+ */
150
+ addMany(items: Iterable<T>, index?: number): this;
151
+ /**
152
+ * Gets an item by its ID or index.
153
+ *
154
+ * @param idOrIndex The item ID or index in the collection.
155
+ * @returns The requested item or `null` if such item does not exist.
156
+ */
157
+ get(idOrIndex: string | number): T | null;
158
+ /**
159
+ * Returns a Boolean indicating whether the collection contains an item.
160
+ *
161
+ * @param itemOrId The item or its ID in the collection.
162
+ * @returns `true` if the collection contains the item, `false` otherwise.
163
+ */
164
+ has(itemOrId: T | string): boolean;
165
+ /**
166
+ * Gets an index of an item in the collection.
167
+ * When an item is not defined in the collection, the index will equal -1.
168
+ *
169
+ * @param itemOrId The item or its ID in the collection.
170
+ * @returns The index of a given item.
171
+ */
172
+ getIndex(itemOrId: T | string): number;
173
+ /**
174
+ * Removes an item from the collection.
175
+ *
176
+ * @param subject The item to remove, its ID or index in the collection.
177
+ * @returns The removed item.
178
+ * @fires remove
179
+ * @fires change
180
+ */
181
+ remove(subject: T | number | string): T;
182
+ /**
183
+ * Executes the callback for each item in the collection and composes an array or values returned by this callback.
184
+ *
185
+ * @typeParam U The result type of the callback.
186
+ * @param callback
187
+ * @param ctx Context in which the `callback` will be called.
188
+ * @returns The result of mapping.
189
+ */
190
+ map<U>(callback: (item: T, index: number) => U, ctx?: any): Array<U>;
191
+ /**
192
+ * Finds the first item in the collection for which the `callback` returns a true value.
193
+ *
194
+ * @param callback
195
+ * @param ctx Context in which the `callback` will be called.
196
+ * @returns The item for which `callback` returned a true value.
197
+ */
198
+ find(callback: (item: T, index: number) => boolean, ctx?: any): T | undefined;
199
+ /**
200
+ * Returns an array with items for which the `callback` returned a true value.
201
+ *
202
+ * @param callback
203
+ * @param ctx Context in which the `callback` will be called.
204
+ * @returns The array with matching items.
205
+ */
206
+ filter(callback: (item: T, index: number) => boolean, ctx?: any): Array<T>;
207
+ /**
208
+ * Removes all items from the collection and destroys the binding created using
209
+ * {@link #bindTo}.
210
+ *
211
+ * @fires remove
212
+ * @fires change
213
+ */
214
+ clear(): void;
215
+ /**
216
+ * Binds and synchronizes the collection with another one.
217
+ *
218
+ * The binding can be a simple factory:
219
+ *
220
+ * ```ts
221
+ * class FactoryClass {
222
+ * public label: string;
223
+ *
224
+ * constructor( data: { label: string } ) {
225
+ * this.label = data.label;
226
+ * }
227
+ * }
228
+ *
229
+ * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
230
+ * const target = new Collection<FactoryClass>();
231
+ *
232
+ * target.bindTo( source ).as( FactoryClass );
233
+ *
234
+ * source.add( { label: 'foo' } );
235
+ * source.add( { label: 'bar' } );
236
+ *
237
+ * console.log( target.length ); // 2
238
+ * console.log( target.get( 1 ).label ); // 'bar'
239
+ *
240
+ * source.remove( 0 );
241
+ * console.log( target.length ); // 1
242
+ * console.log( target.get( 0 ).label ); // 'bar'
243
+ * ```
244
+ *
245
+ * or the factory driven by a custom callback:
246
+ *
247
+ * ```ts
248
+ * class FooClass {
249
+ * public label: string;
250
+ *
251
+ * constructor( data: { label: string } ) {
252
+ * this.label = data.label;
253
+ * }
254
+ * }
255
+ *
256
+ * class BarClass {
257
+ * public label: string;
258
+ *
259
+ * constructor( data: { label: string } ) {
260
+ * this.label = data.label;
261
+ * }
262
+ * }
263
+ *
264
+ * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
265
+ * const target = new Collection<FooClass | BarClass>();
266
+ *
267
+ * target.bindTo( source ).using( ( item ) => {
268
+ * if ( item.label == 'foo' ) {
269
+ * return new FooClass( item );
270
+ * } else {
271
+ * return new BarClass( item );
272
+ * }
273
+ * } );
274
+ *
275
+ * source.add( { label: 'foo' } );
276
+ * source.add( { label: 'bar' } );
277
+ *
278
+ * console.log( target.length ); // 2
279
+ * console.log( target.get( 0 ) instanceof FooClass ); // true
280
+ * console.log( target.get( 1 ) instanceof BarClass ); // true
281
+ * ```
282
+ *
283
+ * or the factory out of property name:
284
+ *
285
+ * ```ts
286
+ * const source = new Collection<{ nested: { value: string } }>();
287
+ * const target = new Collection<{ value: string }>();
288
+ *
289
+ * target.bindTo( source ).using( 'nested' );
290
+ *
291
+ * source.add( { nested: { value: 'foo' } } );
292
+ * source.add( { nested: { value: 'bar' } } );
293
+ *
294
+ * console.log( target.length ); // 2
295
+ * console.log( target.get( 0 ).value ); // 'foo'
296
+ * console.log( target.get( 1 ).value ); // 'bar'
297
+ * ```
298
+ *
299
+ * It's possible to skip specified items by returning null value:
300
+ *
301
+ * ```ts
302
+ * const source = new Collection<{ hidden: boolean }>();
303
+ * const target = new Collection<{ hidden: boolean }>();
304
+ *
305
+ * target.bindTo( source ).using( item => {
306
+ * if ( item.hidden ) {
307
+ * return null;
308
+ * }
309
+ *
310
+ * return item;
311
+ * } );
312
+ *
313
+ * source.add( { hidden: true } );
314
+ * source.add( { hidden: false } );
315
+ *
316
+ * console.log( source.length ); // 2
317
+ * console.log( target.length ); // 1
318
+ * ```
319
+ *
320
+ * **Note**: {@link #clear} can be used to break the binding.
321
+ *
322
+ * @typeParam S The type of `externalCollection` element.
323
+ * @param externalCollection A collection to be bound.
324
+ * @returns The binding chain object.
325
+ */
326
+ bindTo<S extends Record<string, any>>(externalCollection: Collection<S>): CollectionBindToChain<S, T>;
327
+ /**
328
+ * Finalizes and activates a binding initiated by {@link #bindTo}.
329
+ *
330
+ * @param factory A function which produces collection items.
331
+ */
332
+ private _setUpBindToBinding;
333
+ /**
334
+ * Returns an unique id property for a given `item`.
335
+ *
336
+ * The method will generate new id and assign it to the `item` if it doesn't have any.
337
+ *
338
+ * @param item Item to be added.
339
+ */
340
+ private _getItemIdBeforeAdding;
341
+ /**
342
+ * Core {@link #remove} method implementation shared in other functions.
343
+ *
344
+ * In contrast this method **does not** fire the {@link #event:change} event.
345
+ *
346
+ * @param subject The item to remove, its id or index in the collection.
347
+ * @returns Returns an array with the removed item and its index.
348
+ * @fires remove
349
+ */
350
+ private _remove;
351
+ /**
352
+ * Iterable interface.
353
+ */
354
+ [Symbol.iterator](): Iterator<T>;
355
+ }
356
+ /**
357
+ * Fired when an item is added to the collection.
358
+ *
359
+ * @eventName ~Collection#add
360
+ * @param item The added item.
361
+ * @param index An index where the addition occurred.
362
+ */
363
+ export type CollectionAddEvent<T = any> = {
364
+ name: 'add';
365
+ args: [item: T, index: number];
366
+ };
367
+ /**
368
+ * Fired when the collection was changed due to adding or removing items.
369
+ *
370
+ * @eventName ~Collection#change
371
+ * @param data Changed items.
372
+ */
373
+ export type CollectionChangeEvent<T = any> = {
374
+ name: 'change';
375
+ args: [data: CollectionChangeEventData<T>];
376
+ };
377
+ /**
378
+ * A structure describing the {@link ~Collection#event:change `Collection#change`} event.
379
+ */
380
+ export type CollectionChangeEventData<T = any> = {
381
+ /**
382
+ * A list of added items.
383
+ */
384
+ added: Iterable<T>;
385
+ /**
386
+ * A list of removed items.
387
+ */
388
+ removed: Iterable<T>;
389
+ /**
390
+ * An index where the addition or removal occurred.
391
+ */
392
+ index: number;
393
+ };
394
+ /**
395
+ * Fired when an item is removed from the collection.
396
+ *
397
+ * @eventName ~Collection#remove
398
+ * @param item The removed item.
399
+ * @param index Index from which item was removed.
400
+ */
401
+ export type CollectionRemoveEvent<T = any> = {
402
+ name: 'remove';
403
+ args: [item: T, index: number];
404
+ };
405
+ /**
406
+ * An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
407
+ * providing functions that specify the type of the binding.
408
+ *
409
+ * See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
410
+ */
411
+ export interface CollectionBindToChain<S, T> {
412
+ /**
413
+ * Creates the class factory binding in which items of the source collection are passed to
414
+ * the constructor of the specified class.
415
+ *
416
+ * @param Class The class constructor used to create instances in the factory.
417
+ */
418
+ as(Class: new (item: S) => T): void;
419
+ /**
420
+ * Creates a callback or a property binding.
421
+ *
422
+ * @param callbackOrProperty When the function is passed, it should return
423
+ * the collection items. When the string is provided, the property value is used to create the bound collection items.
424
+ */
425
+ using(callbackOrProperty: keyof S | ((item: S) => T | null)): void;
426
+ }
427
+ export {};