@ckeditor/ckeditor5-utils 35.3.2 → 36.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.
- package/LICENSE.md +1 -1
- package/package.json +5 -5
- package/src/areconnectedthroughproperties.js +7 -9
- package/src/ckeditorerror.js +52 -71
- package/src/collection.js +108 -150
- package/src/comparearrays.js +11 -9
- package/src/config.js +30 -84
- package/src/count.js +6 -4
- package/src/diff.js +8 -6
- package/src/difftochanges.js +18 -15
- package/src/dom/createelement.js +12 -10
- package/src/dom/emittermixin.js +44 -85
- package/src/dom/findclosestscrollableancestor.js +30 -0
- package/src/dom/getancestors.js +3 -3
- package/src/dom/getborderwidths.js +3 -3
- package/src/dom/getcommonancestor.js +4 -4
- package/src/dom/getdatafromelement.js +3 -3
- package/src/dom/getpositionedancestor.js +2 -3
- package/src/dom/global.js +13 -15
- package/src/dom/indexof.js +3 -3
- package/src/dom/insertat.js +4 -4
- package/src/dom/iscomment.js +1 -4
- package/src/dom/isnode.js +1 -4
- package/src/dom/isrange.js +1 -4
- package/src/dom/istext.js +1 -4
- package/src/dom/isvisible.js +1 -4
- package/src/dom/iswindow.js +1 -4
- package/src/dom/position.js +111 -134
- package/src/dom/rect.js +43 -53
- package/src/dom/remove.js +2 -2
- package/src/dom/resizeobserver.js +11 -36
- package/src/dom/scroll.js +86 -92
- package/src/dom/setdatainelement.js +3 -3
- package/src/dom/tounit.js +2 -11
- package/src/elementreplacer.js +3 -3
- package/src/emittermixin.js +49 -49
- package/src/env.js +15 -76
- package/src/eventinfo.js +3 -3
- package/src/fastdiff.js +116 -97
- package/src/first.js +1 -4
- package/src/focustracker.js +12 -20
- package/src/index.js +19 -1
- package/src/inserttopriorityarray.js +3 -3
- package/src/isiterable.js +3 -3
- package/src/keyboard.js +21 -22
- package/src/keystrokehandler.js +27 -25
- package/src/language.js +2 -3
- package/src/locale.js +12 -15
- package/src/mapsequal.js +5 -5
- package/src/mix.js +16 -14
- package/src/nth.js +1 -5
- package/src/objecttomap.js +7 -5
- package/src/observablemixin.js +127 -151
- package/src/priorities.js +1 -10
- package/src/splicearray.js +13 -12
- package/src/spy.js +2 -2
- package/src/toarray.js +1 -1
- package/src/tomap.js +8 -6
- package/src/translation-service.js +71 -53
- package/src/uid.js +6 -4
- package/src/unicode.js +10 -16
- package/src/version.js +33 -27
package/src/collection.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
6
6
|
* @module utils/collection
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
8
|
+
import EmitterMixin from './emittermixin';
|
|
9
9
|
import CKEditorError from './ckeditorerror';
|
|
10
10
|
import uid from './uid';
|
|
11
11
|
import isIterable from './isiterable';
|
|
@@ -19,45 +19,9 @@ import isIterable from './isiterable';
|
|
|
19
19
|
* By default an item in the collection is identified by its `id` property. The name of the identifier can be
|
|
20
20
|
* configured through the constructor of the collection.
|
|
21
21
|
*
|
|
22
|
-
* @
|
|
22
|
+
* @typeParam T The type of the collection element.
|
|
23
23
|
*/
|
|
24
|
-
export default class Collection extends
|
|
25
|
-
/**
|
|
26
|
-
* Creates a new Collection instance.
|
|
27
|
-
*
|
|
28
|
-
* You can provide an iterable of initial items the collection will be created with:
|
|
29
|
-
*
|
|
30
|
-
* const collection = new Collection( [ { id: 'John' }, { id: 'Mike' } ] );
|
|
31
|
-
*
|
|
32
|
-
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
33
|
-
* console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
|
|
34
|
-
* console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
|
|
35
|
-
*
|
|
36
|
-
* Or you can first create a collection and then add new items using the {@link #add} method:
|
|
37
|
-
*
|
|
38
|
-
* const collection = new Collection();
|
|
39
|
-
*
|
|
40
|
-
* collection.add( { id: 'John' } );
|
|
41
|
-
* console.log( collection.get( 0 ) ); // -> { id: 'John' }
|
|
42
|
-
*
|
|
43
|
-
* Whatever option you choose, you can always pass a configuration object as the last argument
|
|
44
|
-
* of the constructor:
|
|
45
|
-
*
|
|
46
|
-
* const emptyCollection = new Collection( { idProperty: 'name' } );
|
|
47
|
-
* emptyCollection.add( { name: 'John' } );
|
|
48
|
-
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
49
|
-
*
|
|
50
|
-
* const nonEmptyCollection = new Collection( [ { name: 'John' } ], { idProperty: 'name' } );
|
|
51
|
-
* nonEmptyCollection.add( { name: 'George' } );
|
|
52
|
-
* console.log( collection.get( 'George' ) ); // -> { name: 'George' }
|
|
53
|
-
* console.log( collection.get( 'John' ) ); // -> { name: 'John' }
|
|
54
|
-
*
|
|
55
|
-
* @param {Iterable.<Object>|Object} [initialItemsOrOptions] The initial items of the collection or
|
|
56
|
-
* the options object.
|
|
57
|
-
* @param {Object} [options={}] The options object, when the first argument is an array of initial items.
|
|
58
|
-
* @param {String} [options.idProperty='id'] The name of the property which is used to identify an item.
|
|
59
|
-
* Items that do not have such a property will be assigned one when added to the collection.
|
|
60
|
-
*/
|
|
24
|
+
export default class Collection extends EmitterMixin() {
|
|
61
25
|
constructor(initialItemsOrOptions = {}, options = {}) {
|
|
62
26
|
super();
|
|
63
27
|
const hasInitialItems = isIterable(initialItemsOrOptions);
|
|
@@ -80,24 +44,18 @@ export default class Collection extends Emitter {
|
|
|
80
44
|
}
|
|
81
45
|
/**
|
|
82
46
|
* The number of items available in the collection.
|
|
83
|
-
*
|
|
84
|
-
* @member {Number} #length
|
|
85
47
|
*/
|
|
86
48
|
get length() {
|
|
87
49
|
return this._items.length;
|
|
88
50
|
}
|
|
89
51
|
/**
|
|
90
52
|
* Returns the first item from the collection or null when collection is empty.
|
|
91
|
-
*
|
|
92
|
-
* @returns {Object|null} The first item or `null` if collection is empty.
|
|
93
53
|
*/
|
|
94
54
|
get first() {
|
|
95
55
|
return this._items[0] || null;
|
|
96
56
|
}
|
|
97
57
|
/**
|
|
98
58
|
* Returns the last item from the collection or null when collection is empty.
|
|
99
|
-
*
|
|
100
|
-
* @returns {Object|null} The last item or `null` if collection is empty.
|
|
101
59
|
*/
|
|
102
60
|
get last() {
|
|
103
61
|
return this._items[this.length - 1] || null;
|
|
@@ -107,9 +65,8 @@ export default class Collection extends Emitter {
|
|
|
107
65
|
*
|
|
108
66
|
* If the item does not have an id, then it will be automatically generated and set on the item.
|
|
109
67
|
*
|
|
110
|
-
* @
|
|
111
|
-
* @param
|
|
112
|
-
* @param {Number} [index] The position of the item in the collection. The item
|
|
68
|
+
* @param item
|
|
69
|
+
* @param index The position of the item in the collection. The item
|
|
113
70
|
* is pushed to the collection when `index` not specified.
|
|
114
71
|
* @fires add
|
|
115
72
|
* @fires change
|
|
@@ -122,9 +79,8 @@ export default class Collection extends Emitter {
|
|
|
122
79
|
*
|
|
123
80
|
* Any item not containing an id will get an automatically generated one.
|
|
124
81
|
*
|
|
125
|
-
* @
|
|
126
|
-
* @param
|
|
127
|
-
* @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
|
|
82
|
+
* @param items
|
|
83
|
+
* @param index The position of the insertion. Items will be appended if no `index` is specified.
|
|
128
84
|
* @fires add
|
|
129
85
|
* @fires change
|
|
130
86
|
*/
|
|
@@ -160,8 +116,8 @@ export default class Collection extends Emitter {
|
|
|
160
116
|
/**
|
|
161
117
|
* Gets an item by its ID or index.
|
|
162
118
|
*
|
|
163
|
-
* @param
|
|
164
|
-
* @returns
|
|
119
|
+
* @param idOrIndex The item ID or index in the collection.
|
|
120
|
+
* @returns The requested item or `null` if such item does not exist.
|
|
165
121
|
*/
|
|
166
122
|
get(idOrIndex) {
|
|
167
123
|
let item;
|
|
@@ -184,8 +140,8 @@ export default class Collection extends Emitter {
|
|
|
184
140
|
/**
|
|
185
141
|
* Returns a Boolean indicating whether the collection contains an item.
|
|
186
142
|
*
|
|
187
|
-
* @param
|
|
188
|
-
* @returns
|
|
143
|
+
* @param itemOrId The item or its ID in the collection.
|
|
144
|
+
* @returns `true` if the collection contains the item, `false` otherwise.
|
|
189
145
|
*/
|
|
190
146
|
has(itemOrId) {
|
|
191
147
|
if (typeof itemOrId == 'string') {
|
|
@@ -201,8 +157,8 @@ export default class Collection extends Emitter {
|
|
|
201
157
|
* Gets an index of an item in the collection.
|
|
202
158
|
* When an item is not defined in the collection, the index will equal -1.
|
|
203
159
|
*
|
|
204
|
-
* @param
|
|
205
|
-
* @returns
|
|
160
|
+
* @param itemOrId The item or its ID in the collection.
|
|
161
|
+
* @returns The index of a given item.
|
|
206
162
|
*/
|
|
207
163
|
getIndex(itemOrId) {
|
|
208
164
|
let item;
|
|
@@ -217,8 +173,8 @@ export default class Collection extends Emitter {
|
|
|
217
173
|
/**
|
|
218
174
|
* Removes an item from the collection.
|
|
219
175
|
*
|
|
220
|
-
* @param
|
|
221
|
-
* @returns
|
|
176
|
+
* @param subject The item to remove, its ID or index in the collection.
|
|
177
|
+
* @returns The removed item.
|
|
222
178
|
* @fires remove
|
|
223
179
|
* @fires change
|
|
224
180
|
*/
|
|
@@ -234,11 +190,10 @@ export default class Collection extends Emitter {
|
|
|
234
190
|
/**
|
|
235
191
|
* Executes the callback for each item in the collection and composes an array or values returned by this callback.
|
|
236
192
|
*
|
|
237
|
-
* @
|
|
238
|
-
* @param
|
|
239
|
-
* @param
|
|
240
|
-
* @
|
|
241
|
-
* @returns {Array} The result of mapping.
|
|
193
|
+
* @typeParam U The result type of the callback.
|
|
194
|
+
* @param callback
|
|
195
|
+
* @param ctx Context in which the `callback` will be called.
|
|
196
|
+
* @returns The result of mapping.
|
|
242
197
|
*/
|
|
243
198
|
map(callback, ctx) {
|
|
244
199
|
return this._items.map(callback, ctx);
|
|
@@ -246,11 +201,9 @@ export default class Collection extends Emitter {
|
|
|
246
201
|
/**
|
|
247
202
|
* Finds the first item in the collection for which the `callback` returns a true value.
|
|
248
203
|
*
|
|
249
|
-
* @param
|
|
250
|
-
* @param
|
|
251
|
-
* @
|
|
252
|
-
* @param {Object} [ctx] Context in which the `callback` will be called.
|
|
253
|
-
* @returns {Object|undefined} The item for which `callback` returned a true value.
|
|
204
|
+
* @param callback
|
|
205
|
+
* @param ctx Context in which the `callback` will be called.
|
|
206
|
+
* @returns The item for which `callback` returned a true value.
|
|
254
207
|
*/
|
|
255
208
|
find(callback, ctx) {
|
|
256
209
|
return this._items.find(callback, ctx);
|
|
@@ -258,11 +211,9 @@ export default class Collection extends Emitter {
|
|
|
258
211
|
/**
|
|
259
212
|
* Returns an array with items for which the `callback` returned a true value.
|
|
260
213
|
*
|
|
261
|
-
* @param
|
|
262
|
-
* @param
|
|
263
|
-
* @
|
|
264
|
-
* @param {Object} [ctx] Context in which the `callback` will be called.
|
|
265
|
-
* @returns {Array} The array with matching items.
|
|
214
|
+
* @param callback
|
|
215
|
+
* @param ctx Context in which the `callback` will be called.
|
|
216
|
+
* @returns The array with matching items.
|
|
266
217
|
*/
|
|
267
218
|
filter(callback, ctx) {
|
|
268
219
|
return this._items.filter(callback, ctx);
|
|
@@ -294,96 +245,111 @@ export default class Collection extends Emitter {
|
|
|
294
245
|
*
|
|
295
246
|
* The binding can be a simple factory:
|
|
296
247
|
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
* }
|
|
301
|
-
* }
|
|
248
|
+
* ```ts
|
|
249
|
+
* class FactoryClass {
|
|
250
|
+
* public label: string;
|
|
302
251
|
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
252
|
+
* constructor( data: { label: string } ) {
|
|
253
|
+
* this.label = data.label;
|
|
254
|
+
* }
|
|
255
|
+
* }
|
|
305
256
|
*
|
|
306
|
-
*
|
|
257
|
+
* const source = new Collection<{ label: string }>( { idProperty: 'label' } );
|
|
258
|
+
* const target = new Collection<FactoryClass>();
|
|
307
259
|
*
|
|
308
|
-
*
|
|
309
|
-
* source.add( { label: 'bar' } );
|
|
260
|
+
* target.bindTo( source ).as( FactoryClass );
|
|
310
261
|
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
262
|
+
* source.add( { label: 'foo' } );
|
|
263
|
+
* source.add( { label: 'bar' } );
|
|
313
264
|
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
265
|
+
* console.log( target.length ); // 2
|
|
266
|
+
* console.log( target.get( 1 ).label ); // 'bar'
|
|
267
|
+
*
|
|
268
|
+
* source.remove( 0 );
|
|
269
|
+
* console.log( target.length ); // 1
|
|
270
|
+
* console.log( target.get( 0 ).label ); // 'bar'
|
|
271
|
+
* ```
|
|
317
272
|
*
|
|
318
273
|
* or the factory driven by a custom callback:
|
|
319
274
|
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
*
|
|
324
|
-
*
|
|
275
|
+
* ```ts
|
|
276
|
+
* class FooClass {
|
|
277
|
+
* public label: string;
|
|
278
|
+
*
|
|
279
|
+
* constructor( data: { label: string } ) {
|
|
280
|
+
* this.label = data.label;
|
|
281
|
+
* }
|
|
282
|
+
* }
|
|
325
283
|
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* this.label = data.label;
|
|
329
|
-
* }
|
|
330
|
-
* }
|
|
284
|
+
* class BarClass {
|
|
285
|
+
* public label: string;
|
|
331
286
|
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
287
|
+
* constructor( data: { label: string } ) {
|
|
288
|
+
* this.label = data.label;
|
|
289
|
+
* }
|
|
290
|
+
* }
|
|
334
291
|
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* return new FooClass( item );
|
|
338
|
-
* } else {
|
|
339
|
-
* return new BarClass( item );
|
|
340
|
-
* }
|
|
341
|
-
* } );
|
|
292
|
+
* const source = new Collection<{ label: string }>( { idProperty: 'label' } );
|
|
293
|
+
* const target = new Collection<FooClass | BarClass>();
|
|
342
294
|
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
295
|
+
* target.bindTo( source ).using( ( item ) => {
|
|
296
|
+
* if ( item.label == 'foo' ) {
|
|
297
|
+
* return new FooClass( item );
|
|
298
|
+
* } else {
|
|
299
|
+
* return new BarClass( item );
|
|
300
|
+
* }
|
|
301
|
+
* } );
|
|
345
302
|
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
303
|
+
* source.add( { label: 'foo' } );
|
|
304
|
+
* source.add( { label: 'bar' } );
|
|
305
|
+
*
|
|
306
|
+
* console.log( target.length ); // 2
|
|
307
|
+
* console.log( target.get( 0 ) instanceof FooClass ); // true
|
|
308
|
+
* console.log( target.get( 1 ) instanceof BarClass ); // true
|
|
309
|
+
* ```
|
|
349
310
|
*
|
|
350
311
|
* or the factory out of property name:
|
|
351
312
|
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
313
|
+
* ```ts
|
|
314
|
+
* const source = new Collection<{ nested: { value: string } }>();
|
|
315
|
+
* const target = new Collection<{ value: string }>();
|
|
354
316
|
*
|
|
355
|
-
*
|
|
317
|
+
* target.bindTo( source ).using( 'nested' );
|
|
356
318
|
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
319
|
+
* source.add( { nested: { value: 'foo' } } );
|
|
320
|
+
* source.add( { nested: { value: 'bar' } } );
|
|
359
321
|
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
322
|
+
* console.log( target.length ); // 2
|
|
323
|
+
* console.log( target.get( 0 ).value ); // 'foo'
|
|
324
|
+
* console.log( target.get( 1 ).value ); // 'bar'
|
|
325
|
+
* ```
|
|
363
326
|
*
|
|
364
|
-
* It's possible to skip specified items by returning
|
|
327
|
+
* It's possible to skip specified items by returning null value:
|
|
365
328
|
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* const source = new Collection<{ hidden: boolean }>();
|
|
331
|
+
* const target = new Collection<{ hidden: boolean }>();
|
|
368
332
|
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
333
|
+
* target.bindTo( source ).using( item => {
|
|
334
|
+
* if ( item.hidden ) {
|
|
335
|
+
* return null;
|
|
336
|
+
* }
|
|
373
337
|
*
|
|
374
|
-
*
|
|
375
|
-
*
|
|
338
|
+
* return item;
|
|
339
|
+
* } );
|
|
376
340
|
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
341
|
+
* source.add( { hidden: true } );
|
|
342
|
+
* source.add( { hidden: false } );
|
|
379
343
|
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
344
|
+
* console.log( source.length ); // 2
|
|
345
|
+
* console.log( target.length ); // 1
|
|
346
|
+
* ```
|
|
382
347
|
*
|
|
383
348
|
* **Note**: {@link #clear} can be used to break the binding.
|
|
384
349
|
*
|
|
385
|
-
* @
|
|
386
|
-
* @
|
|
350
|
+
* @typeParam S The type of `externalCollection` element.
|
|
351
|
+
* @param externalCollection A collection to be bound.
|
|
352
|
+
* @returns The binding chain object.
|
|
387
353
|
*/
|
|
388
354
|
bindTo(externalCollection) {
|
|
389
355
|
if (this._bindToCollection) {
|
|
@@ -410,16 +376,13 @@ export default class Collection extends Emitter {
|
|
|
410
376
|
};
|
|
411
377
|
}
|
|
412
378
|
/**
|
|
413
|
-
* Finalizes and activates a binding initiated by {#bindTo}.
|
|
379
|
+
* Finalizes and activates a binding initiated by {@link #bindTo}.
|
|
414
380
|
*
|
|
415
|
-
* @
|
|
416
|
-
* @param {Function} factory A function which produces collection items.
|
|
381
|
+
* @param factory A function which produces collection items.
|
|
417
382
|
*/
|
|
418
383
|
_setUpBindToBinding(factory) {
|
|
419
384
|
const externalCollection = this._bindToCollection;
|
|
420
385
|
// Adds the item to the collection once a change has been done to the external collection.
|
|
421
|
-
//
|
|
422
|
-
// @private
|
|
423
386
|
const addItem = (evt, externalItem, index) => {
|
|
424
387
|
const isExternalBoundToThis = externalCollection._bindToCollection == this;
|
|
425
388
|
const externalItemBound = externalCollection._bindToInternalToExternalMap.get(externalItem);
|
|
@@ -523,9 +486,7 @@ export default class Collection extends Emitter {
|
|
|
523
486
|
*
|
|
524
487
|
* The method will generate new id and assign it to the `item` if it doesn't have any.
|
|
525
488
|
*
|
|
526
|
-
* @
|
|
527
|
-
* @param {Object} item Item to be added.
|
|
528
|
-
* @returns {String}
|
|
489
|
+
* @param item Item to be added.
|
|
529
490
|
*/
|
|
530
491
|
_getItemIdBeforeAdding(item) {
|
|
531
492
|
const idProperty = this._idProperty;
|
|
@@ -559,9 +520,8 @@ export default class Collection extends Emitter {
|
|
|
559
520
|
*
|
|
560
521
|
* In contrast this method **does not** fire the {@link #event:change} event.
|
|
561
522
|
*
|
|
562
|
-
* @
|
|
563
|
-
* @
|
|
564
|
-
* @returns {Array} Returns an array with the removed item and its index.
|
|
523
|
+
* @param subject The item to remove, its id or index in the collection.
|
|
524
|
+
* @returns Returns an array with the removed item and its index.
|
|
565
525
|
* @fires remove
|
|
566
526
|
*/
|
|
567
527
|
_remove(subject) {
|
|
@@ -608,8 +568,6 @@ export default class Collection extends Emitter {
|
|
|
608
568
|
}
|
|
609
569
|
/**
|
|
610
570
|
* Iterable interface.
|
|
611
|
-
*
|
|
612
|
-
* @returns {Iterator.<*>}
|
|
613
571
|
*/
|
|
614
572
|
[Symbol.iterator]() {
|
|
615
573
|
return this._items[Symbol.iterator]();
|
package/src/comparearrays.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
@@ -11,15 +11,17 @@
|
|
|
11
11
|
* a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
|
|
12
12
|
* it means that arrays differ.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* compareArrays( [ 0, 2 ], [ 0, 2 ] ); // 'same'
|
|
16
|
+
* compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // 'prefix'
|
|
17
|
+
* compareArrays( [ 0, 2 ], [ 0 ] ); // 'extension'
|
|
18
|
+
* compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
|
|
19
|
+
* compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
|
|
20
|
+
* ```
|
|
19
21
|
*
|
|
20
|
-
* @param
|
|
21
|
-
* @param
|
|
22
|
-
* @returns
|
|
22
|
+
* @param a Array that is compared.
|
|
23
|
+
* @param b Array to compare with.
|
|
24
|
+
* @returns How array `a` is related to `b`.
|
|
23
25
|
*/
|
|
24
26
|
export default function compareArrays(a, b) {
|
|
25
27
|
const minLen = Math.min(a.length, b.length);
|
package/src/config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
@@ -8,21 +8,17 @@
|
|
|
8
8
|
import { isPlainObject, isElement, cloneDeepWith } from 'lodash-es';
|
|
9
9
|
/**
|
|
10
10
|
* Handles a configuration dictionary.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam Cfg A type of the configuration dictionary.
|
|
11
13
|
*/
|
|
12
14
|
export default class Config {
|
|
13
15
|
/**
|
|
14
16
|
* Creates an instance of the {@link ~Config} class.
|
|
15
17
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
18
|
+
* @param configurations The initial configurations to be set. Usually, provided by the user.
|
|
19
|
+
* @param defaultConfigurations The default configurations. Usually, provided by the system.
|
|
18
20
|
*/
|
|
19
21
|
constructor(configurations, defaultConfigurations) {
|
|
20
|
-
/**
|
|
21
|
-
* Store for the whole configuration.
|
|
22
|
-
*
|
|
23
|
-
* @private
|
|
24
|
-
* @member {Object}
|
|
25
|
-
*/
|
|
26
22
|
this._config = {};
|
|
27
23
|
// Set default configuration.
|
|
28
24
|
if (defaultConfigurations) {
|
|
@@ -35,57 +31,9 @@ export default class Config {
|
|
|
35
31
|
this._setObjectToTarget(this._config, configurations);
|
|
36
32
|
}
|
|
37
33
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Set configuration values.
|
|
40
|
-
*
|
|
41
|
-
* It accepts both a name/value pair or an object, which properties and values will be used to set
|
|
42
|
-
* configurations.
|
|
43
|
-
*
|
|
44
|
-
* It also accepts setting a "deep configuration" by using dots in the name. For example, `'resize.width'` sets
|
|
45
|
-
* the value for the `width` configuration in the `resize` subset.
|
|
46
|
-
*
|
|
47
|
-
* config.set( 'width', 500 );
|
|
48
|
-
* config.set( 'toolbar.collapsed', true );
|
|
49
|
-
*
|
|
50
|
-
* // Equivalent to:
|
|
51
|
-
* config.set( {
|
|
52
|
-
* width: 500
|
|
53
|
-
* toolbar: {
|
|
54
|
-
* collapsed: true
|
|
55
|
-
* }
|
|
56
|
-
* } );
|
|
57
|
-
*
|
|
58
|
-
* Passing an object as the value will amend the configuration, not replace it.
|
|
59
|
-
*
|
|
60
|
-
* config.set( 'toolbar', {
|
|
61
|
-
* collapsed: true,
|
|
62
|
-
* } );
|
|
63
|
-
*
|
|
64
|
-
* config.set( 'toolbar', {
|
|
65
|
-
* color: 'red',
|
|
66
|
-
* } );
|
|
67
|
-
*
|
|
68
|
-
* config.get( 'toolbar.collapsed' ); // true
|
|
69
|
-
* config.get( 'toolbar.color' ); // 'red'
|
|
70
|
-
*
|
|
71
|
-
* @param {String|Object} name The configuration name or an object from which take properties as
|
|
72
|
-
* configuration entries. Configuration names are case-sensitive.
|
|
73
|
-
* @param {*} value The configuration value. Used if a name is passed.
|
|
74
|
-
*/
|
|
75
34
|
set(name, value) {
|
|
76
35
|
this._setToTarget(this._config, name, value);
|
|
77
36
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Does exactly the same as {@link #set} with one exception – passed configuration extends
|
|
80
|
-
* existing one, but does not overwrite already defined values.
|
|
81
|
-
*
|
|
82
|
-
* This method is supposed to be called by plugin developers to setup plugin's configurations. It would be
|
|
83
|
-
* rarely used for other needs.
|
|
84
|
-
*
|
|
85
|
-
* @param {String|Object} name The configuration name or an object from which take properties as
|
|
86
|
-
* configuration entries. Configuration names are case-sensitive.
|
|
87
|
-
* @param {*} value The configuration value. Used if a name is passed.
|
|
88
|
-
*/
|
|
89
37
|
define(name, value) {
|
|
90
38
|
const isDefine = true;
|
|
91
39
|
this._setToTarget(this._config, name, value, isDefine);
|
|
@@ -93,22 +41,24 @@ export default class Config {
|
|
|
93
41
|
/**
|
|
94
42
|
* Gets the value for a configuration entry.
|
|
95
43
|
*
|
|
96
|
-
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* config.get( 'name' );
|
|
46
|
+
* ```
|
|
97
47
|
*
|
|
98
48
|
* Deep configurations can be retrieved by separating each part with a dot.
|
|
99
49
|
*
|
|
100
|
-
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* config.get( 'toolbar.collapsed' );
|
|
52
|
+
* ```
|
|
101
53
|
*
|
|
102
|
-
* @param
|
|
103
|
-
* @returns
|
|
54
|
+
* @param name The configuration name. Configuration names are case-sensitive.
|
|
55
|
+
* @returns The configuration value or `undefined` if the configuration entry was not found.
|
|
104
56
|
*/
|
|
105
57
|
get(name) {
|
|
106
58
|
return this._getFromSource(this._config, name);
|
|
107
59
|
}
|
|
108
60
|
/**
|
|
109
61
|
* Iterates over all top level configuration names.
|
|
110
|
-
*
|
|
111
|
-
* @returns {Iterable.<String>}
|
|
112
62
|
*/
|
|
113
63
|
*names() {
|
|
114
64
|
for (const name of Object.keys(this._config)) {
|
|
@@ -118,12 +68,11 @@ export default class Config {
|
|
|
118
68
|
/**
|
|
119
69
|
* Saves passed configuration to the specified target (nested object).
|
|
120
70
|
*
|
|
121
|
-
* @
|
|
122
|
-
* @param
|
|
123
|
-
* @param {String|Object} name The configuration name or an object from which take properties as
|
|
71
|
+
* @param target Nested config object.
|
|
72
|
+
* @param name The configuration name or an object from which take properties as
|
|
124
73
|
* configuration entries. Configuration names are case-sensitive.
|
|
125
|
-
* @param
|
|
126
|
-
* @param
|
|
74
|
+
* @param value The configuration value. Used if a name is passed.
|
|
75
|
+
* @param isDefine Define if passed configuration should overwrite existing one.
|
|
127
76
|
*/
|
|
128
77
|
_setToTarget(target, name, value, isDefine = false) {
|
|
129
78
|
// In case of an object, iterate through it and call `_setToTarget` again for each property.
|
|
@@ -164,10 +113,9 @@ export default class Config {
|
|
|
164
113
|
/**
|
|
165
114
|
* Get specified configuration from specified source (nested object).
|
|
166
115
|
*
|
|
167
|
-
* @
|
|
168
|
-
* @param
|
|
169
|
-
* @
|
|
170
|
-
* @returns {*} The configuration value or `undefined` if the configuration entry was not found.
|
|
116
|
+
* @param source level of nested object.
|
|
117
|
+
* @param name The configuration name. Configuration names are case-sensitive.
|
|
118
|
+
* @returns The configuration value or `undefined` if the configuration entry was not found.
|
|
171
119
|
*/
|
|
172
120
|
_getFromSource(source, name) {
|
|
173
121
|
// The configuration name should be split into parts if it has dots. E.g. `resize.width` -> [`resize`, `width`].
|
|
@@ -189,10 +137,9 @@ export default class Config {
|
|
|
189
137
|
/**
|
|
190
138
|
* Iterates through passed object and calls {@link #_setToTarget} method with object key and value for each property.
|
|
191
139
|
*
|
|
192
|
-
* @
|
|
193
|
-
* @param
|
|
194
|
-
* @param
|
|
195
|
-
* @param {Boolean} [isDefine] Defines if passed configuration is default configuration or not.
|
|
140
|
+
* @param target Nested config object.
|
|
141
|
+
* @param configuration Configuration data set
|
|
142
|
+
* @param isDefine Defines if passed configuration is default configuration or not.
|
|
196
143
|
*/
|
|
197
144
|
_setObjectToTarget(target, configuration, isDefine) {
|
|
198
145
|
Object.keys(configuration).forEach(key => {
|
|
@@ -200,17 +147,16 @@ export default class Config {
|
|
|
200
147
|
});
|
|
201
148
|
}
|
|
202
149
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
150
|
+
/**
|
|
151
|
+
* Clones configuration object or value.
|
|
152
|
+
*/
|
|
206
153
|
function cloneConfig(source) {
|
|
207
154
|
return cloneDeepWith(source, leaveDOMReferences);
|
|
208
155
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
// @returns {Element|undefined}
|
|
156
|
+
/**
|
|
157
|
+
* A customized function for cloneDeepWith.
|
|
158
|
+
* It will leave references to DOM Elements instead of cloning them.
|
|
159
|
+
*/
|
|
214
160
|
function leaveDOMReferences(value) {
|
|
215
161
|
return isElement(value) ? value : undefined;
|
|
216
162
|
}
|
package/src/count.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
/**
|
|
9
9
|
* Returns the number of items return by the iterator.
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* count( [ 1, 2, 3, 4, 5 ] ); // 5;
|
|
13
|
+
* ```
|
|
12
14
|
*
|
|
13
|
-
* @param
|
|
14
|
-
* @returns
|
|
15
|
+
* @param iterable Any iterable.
|
|
16
|
+
* @returns Number of items returned by that iterable.
|
|
15
17
|
*/
|
|
16
18
|
export default function count(iterable) {
|
|
17
19
|
let count = 0;
|