@ckeditor/ckeditor5-utils 40.0.0 → 40.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.
Files changed (135) hide show
  1. package/CHANGELOG.md +26 -26
  2. package/LICENSE.md +3 -3
  3. package/package.json +1 -1
  4. package/src/abortabledebounce.d.ts +17 -0
  5. package/src/abortabledebounce.js +22 -0
  6. package/src/areconnectedthroughproperties.d.ts +11 -11
  7. package/src/areconnectedthroughproperties.js +73 -73
  8. package/src/ckeditorerror.d.ts +123 -123
  9. package/src/ckeditorerror.js +176 -176
  10. package/src/collection.d.ts +433 -427
  11. package/src/collection.js +583 -575
  12. package/src/comparearrays.d.ts +30 -30
  13. package/src/comparearrays.js +47 -47
  14. package/src/config.d.ts +163 -163
  15. package/src/config.js +163 -162
  16. package/src/count.d.ts +18 -18
  17. package/src/count.js +24 -24
  18. package/src/delay.d.ts +19 -19
  19. package/src/delay.js +26 -26
  20. package/src/diff.d.ts +31 -31
  21. package/src/diff.js +115 -115
  22. package/src/difftochanges.d.ts +59 -59
  23. package/src/difftochanges.js +79 -79
  24. package/src/dom/createelement.d.ts +57 -57
  25. package/src/dom/createelement.js +40 -40
  26. package/src/dom/emittermixin.d.ts +142 -142
  27. package/src/dom/emittermixin.js +239 -239
  28. package/src/dom/findclosestscrollableancestor.d.ts +11 -11
  29. package/src/dom/findclosestscrollableancestor.js +31 -31
  30. package/src/dom/getancestors.d.ts +17 -17
  31. package/src/dom/getancestors.js +27 -27
  32. package/src/dom/getborderwidths.d.ts +24 -24
  33. package/src/dom/getborderwidths.js +24 -24
  34. package/src/dom/getcommonancestor.d.ts +12 -12
  35. package/src/dom/getcommonancestor.js +25 -25
  36. package/src/dom/getdatafromelement.d.ts +14 -14
  37. package/src/dom/getdatafromelement.js +20 -20
  38. package/src/dom/getpositionedancestor.d.ts +10 -10
  39. package/src/dom/getpositionedancestor.js +22 -22
  40. package/src/dom/global.d.ts +32 -32
  41. package/src/dom/global.js +35 -35
  42. package/src/dom/indexof.d.ts +14 -14
  43. package/src/dom/indexof.js +21 -21
  44. package/src/dom/insertat.d.ts +15 -15
  45. package/src/dom/insertat.js +17 -17
  46. package/src/dom/iscomment.d.ts +11 -11
  47. package/src/dom/iscomment.js +14 -14
  48. package/src/dom/isnode.d.ts +11 -11
  49. package/src/dom/isnode.js +21 -21
  50. package/src/dom/isrange.d.ts +11 -11
  51. package/src/dom/isrange.js +13 -13
  52. package/src/dom/istext.d.ts +11 -11
  53. package/src/dom/istext.js +13 -13
  54. package/src/dom/isvalidattributename.d.ts +10 -10
  55. package/src/dom/isvalidattributename.js +22 -22
  56. package/src/dom/isvisible.d.ts +18 -18
  57. package/src/dom/isvisible.js +20 -20
  58. package/src/dom/iswindow.d.ts +11 -11
  59. package/src/dom/iswindow.js +22 -22
  60. package/src/dom/position.d.ts +211 -211
  61. package/src/dom/position.js +313 -313
  62. package/src/dom/rect.d.ts +195 -195
  63. package/src/dom/rect.js +474 -474
  64. package/src/dom/remove.d.ts +13 -13
  65. package/src/dom/remove.js +18 -18
  66. package/src/dom/resizeobserver.d.ts +74 -74
  67. package/src/dom/resizeobserver.js +126 -126
  68. package/src/dom/scroll.d.ts +73 -73
  69. package/src/dom/scroll.js +383 -383
  70. package/src/dom/setdatainelement.d.ts +14 -14
  71. package/src/dom/setdatainelement.js +20 -20
  72. package/src/dom/tounit.d.ts +22 -22
  73. package/src/dom/tounit.js +16 -16
  74. package/src/elementreplacer.d.ts +31 -31
  75. package/src/elementreplacer.js +43 -43
  76. package/src/emittermixin.d.ts +312 -312
  77. package/src/emittermixin.js +453 -453
  78. package/src/env.d.ts +117 -117
  79. package/src/env.js +122 -122
  80. package/src/eventinfo.d.ts +58 -58
  81. package/src/eventinfo.js +26 -26
  82. package/src/fastdiff.d.ts +112 -112
  83. package/src/fastdiff.js +248 -248
  84. package/src/first.d.ts +11 -11
  85. package/src/first.js +17 -17
  86. package/src/focustracker.d.ts +75 -75
  87. package/src/focustracker.js +95 -95
  88. package/src/index.d.ts +64 -61
  89. package/src/index.js +63 -60
  90. package/src/inserttopriorityarray.d.ts +30 -30
  91. package/src/inserttopriorityarray.js +21 -21
  92. package/src/isiterable.d.ts +14 -14
  93. package/src/isiterable.js +16 -16
  94. package/src/keyboard.d.ts +126 -126
  95. package/src/keyboard.js +221 -221
  96. package/src/keystrokehandler.d.ts +87 -87
  97. package/src/keystrokehandler.js +122 -122
  98. package/src/language.d.ts +17 -17
  99. package/src/language.js +19 -19
  100. package/src/locale.d.ts +120 -120
  101. package/src/locale.js +76 -76
  102. package/src/mapsequal.d.ts +15 -15
  103. package/src/mapsequal.js +27 -27
  104. package/src/mix.d.ts +85 -85
  105. package/src/mix.js +50 -50
  106. package/src/nth.d.ts +16 -16
  107. package/src/nth.js +24 -24
  108. package/src/objecttomap.d.ts +23 -23
  109. package/src/objecttomap.js +27 -27
  110. package/src/observablemixin.d.ts +560 -560
  111. package/src/observablemixin.js +580 -580
  112. package/src/priorities.d.ts +33 -33
  113. package/src/priorities.js +23 -23
  114. package/src/retry.d.ts +33 -0
  115. package/src/retry.js +47 -0
  116. package/src/splicearray.d.ts +26 -26
  117. package/src/splicearray.js +40 -40
  118. package/src/spy.d.ts +21 -21
  119. package/src/spy.js +22 -22
  120. package/src/toarray.d.ts +25 -25
  121. package/src/toarray.js +7 -7
  122. package/src/tomap.d.ts +19 -19
  123. package/src/tomap.js +29 -29
  124. package/src/translation-service.d.ts +168 -168
  125. package/src/translation-service.js +198 -198
  126. package/src/uid.d.ts +15 -15
  127. package/src/uid.js +57 -57
  128. package/src/unicode.d.ts +54 -54
  129. package/src/unicode.js +85 -85
  130. package/src/verifylicense.d.ts +15 -15
  131. package/src/verifylicense.js +87 -87
  132. package/src/version.d.ts +10 -10
  133. package/src/version.js +153 -153
  134. package/src/wait.d.ts +16 -0
  135. package/src/wait.js +29 -0
@@ -1,427 +1,433 @@
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
+ * Performs the specified action for each item in the collection.
193
+ *
194
+ * @param ctx Context in which the `callback` will be called.
195
+ */
196
+ forEach(callback: (item: T, index: number) => unknown, ctx?: any): void;
197
+ /**
198
+ * Finds the first item in the collection for which the `callback` returns a true value.
199
+ *
200
+ * @param callback
201
+ * @param ctx Context in which the `callback` will be called.
202
+ * @returns The item for which `callback` returned a true value.
203
+ */
204
+ find(callback: (item: T, index: number) => boolean, ctx?: any): T | undefined;
205
+ /**
206
+ * Returns an array with items for which the `callback` returned a true value.
207
+ *
208
+ * @param callback
209
+ * @param ctx Context in which the `callback` will be called.
210
+ * @returns The array with matching items.
211
+ */
212
+ filter(callback: (item: T, index: number) => boolean, ctx?: any): Array<T>;
213
+ /**
214
+ * Removes all items from the collection and destroys the binding created using
215
+ * {@link #bindTo}.
216
+ *
217
+ * @fires remove
218
+ * @fires change
219
+ */
220
+ clear(): void;
221
+ /**
222
+ * Binds and synchronizes the collection with another one.
223
+ *
224
+ * The binding can be a simple factory:
225
+ *
226
+ * ```ts
227
+ * class FactoryClass {
228
+ * public label: string;
229
+ *
230
+ * constructor( data: { label: string } ) {
231
+ * this.label = data.label;
232
+ * }
233
+ * }
234
+ *
235
+ * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
236
+ * const target = new Collection<FactoryClass>();
237
+ *
238
+ * target.bindTo( source ).as( FactoryClass );
239
+ *
240
+ * source.add( { label: 'foo' } );
241
+ * source.add( { label: 'bar' } );
242
+ *
243
+ * console.log( target.length ); // 2
244
+ * console.log( target.get( 1 ).label ); // 'bar'
245
+ *
246
+ * source.remove( 0 );
247
+ * console.log( target.length ); // 1
248
+ * console.log( target.get( 0 ).label ); // 'bar'
249
+ * ```
250
+ *
251
+ * or the factory driven by a custom callback:
252
+ *
253
+ * ```ts
254
+ * class FooClass {
255
+ * public label: string;
256
+ *
257
+ * constructor( data: { label: string } ) {
258
+ * this.label = data.label;
259
+ * }
260
+ * }
261
+ *
262
+ * class BarClass {
263
+ * public label: string;
264
+ *
265
+ * constructor( data: { label: string } ) {
266
+ * this.label = data.label;
267
+ * }
268
+ * }
269
+ *
270
+ * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
271
+ * const target = new Collection<FooClass | BarClass>();
272
+ *
273
+ * target.bindTo( source ).using( ( item ) => {
274
+ * if ( item.label == 'foo' ) {
275
+ * return new FooClass( item );
276
+ * } else {
277
+ * return new BarClass( item );
278
+ * }
279
+ * } );
280
+ *
281
+ * source.add( { label: 'foo' } );
282
+ * source.add( { label: 'bar' } );
283
+ *
284
+ * console.log( target.length ); // 2
285
+ * console.log( target.get( 0 ) instanceof FooClass ); // true
286
+ * console.log( target.get( 1 ) instanceof BarClass ); // true
287
+ * ```
288
+ *
289
+ * or the factory out of property name:
290
+ *
291
+ * ```ts
292
+ * const source = new Collection<{ nested: { value: string } }>();
293
+ * const target = new Collection<{ value: string }>();
294
+ *
295
+ * target.bindTo( source ).using( 'nested' );
296
+ *
297
+ * source.add( { nested: { value: 'foo' } } );
298
+ * source.add( { nested: { value: 'bar' } } );
299
+ *
300
+ * console.log( target.length ); // 2
301
+ * console.log( target.get( 0 ).value ); // 'foo'
302
+ * console.log( target.get( 1 ).value ); // 'bar'
303
+ * ```
304
+ *
305
+ * It's possible to skip specified items by returning null value:
306
+ *
307
+ * ```ts
308
+ * const source = new Collection<{ hidden: boolean }>();
309
+ * const target = new Collection<{ hidden: boolean }>();
310
+ *
311
+ * target.bindTo( source ).using( item => {
312
+ * if ( item.hidden ) {
313
+ * return null;
314
+ * }
315
+ *
316
+ * return item;
317
+ * } );
318
+ *
319
+ * source.add( { hidden: true } );
320
+ * source.add( { hidden: false } );
321
+ *
322
+ * console.log( source.length ); // 2
323
+ * console.log( target.length ); // 1
324
+ * ```
325
+ *
326
+ * **Note**: {@link #clear} can be used to break the binding.
327
+ *
328
+ * @typeParam S The type of `externalCollection` element.
329
+ * @param externalCollection A collection to be bound.
330
+ * @returns The binding chain object.
331
+ */
332
+ bindTo<S extends Record<string, any>>(externalCollection: Collection<S>): CollectionBindToChain<S, T>;
333
+ /**
334
+ * Finalizes and activates a binding initiated by {@link #bindTo}.
335
+ *
336
+ * @param factory A function which produces collection items.
337
+ */
338
+ private _setUpBindToBinding;
339
+ /**
340
+ * Returns an unique id property for a given `item`.
341
+ *
342
+ * The method will generate new id and assign it to the `item` if it doesn't have any.
343
+ *
344
+ * @param item Item to be added.
345
+ */
346
+ private _getItemIdBeforeAdding;
347
+ /**
348
+ * Core {@link #remove} method implementation shared in other functions.
349
+ *
350
+ * In contrast this method **does not** fire the {@link #event:change} event.
351
+ *
352
+ * @param subject The item to remove, its id or index in the collection.
353
+ * @returns Returns an array with the removed item and its index.
354
+ * @fires remove
355
+ */
356
+ private _remove;
357
+ /**
358
+ * Iterable interface.
359
+ */
360
+ [Symbol.iterator](): Iterator<T>;
361
+ }
362
+ /**
363
+ * Fired when an item is added to the collection.
364
+ *
365
+ * @eventName ~Collection#add
366
+ * @param item The added item.
367
+ * @param index An index where the addition occurred.
368
+ */
369
+ export type CollectionAddEvent<T = any> = {
370
+ name: 'add';
371
+ args: [item: T, index: number];
372
+ };
373
+ /**
374
+ * Fired when the collection was changed due to adding or removing items.
375
+ *
376
+ * @eventName ~Collection#change
377
+ * @param data Changed items.
378
+ */
379
+ export type CollectionChangeEvent<T = any> = {
380
+ name: 'change';
381
+ args: [data: CollectionChangeEventData<T>];
382
+ };
383
+ /**
384
+ * A structure describing the {@link ~Collection#event:change `Collection#change`} event.
385
+ */
386
+ export type CollectionChangeEventData<T = any> = {
387
+ /**
388
+ * A list of added items.
389
+ */
390
+ added: Iterable<T>;
391
+ /**
392
+ * A list of removed items.
393
+ */
394
+ removed: Iterable<T>;
395
+ /**
396
+ * An index where the addition or removal occurred.
397
+ */
398
+ index: number;
399
+ };
400
+ /**
401
+ * Fired when an item is removed from the collection.
402
+ *
403
+ * @eventName ~Collection#remove
404
+ * @param item The removed item.
405
+ * @param index Index from which item was removed.
406
+ */
407
+ export type CollectionRemoveEvent<T = any> = {
408
+ name: 'remove';
409
+ args: [item: T, index: number];
410
+ };
411
+ /**
412
+ * An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
413
+ * providing functions that specify the type of the binding.
414
+ *
415
+ * See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
416
+ */
417
+ export interface CollectionBindToChain<S, T> {
418
+ /**
419
+ * Creates the class factory binding in which items of the source collection are passed to
420
+ * the constructor of the specified class.
421
+ *
422
+ * @param Class The class constructor used to create instances in the factory.
423
+ */
424
+ as(Class: new (item: S) => T): void;
425
+ /**
426
+ * Creates a callback or a property binding.
427
+ *
428
+ * @param callbackOrProperty When the function is passed, it should return
429
+ * the collection items. When the string is provided, the property value is used to create the bound collection items.
430
+ */
431
+ using(callbackOrProperty: keyof S | ((item: S) => T | null)): void;
432
+ }
433
+ export {};