@ckeditor/ckeditor5-utils 35.3.2 → 35.4.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/package.json +5 -5
- package/src/areconnectedthroughproperties.js +5 -7
- package/src/ckeditorerror.js +51 -70
- package/src/collection.js +106 -148
- package/src/comparearrays.js +10 -8
- package/src/config.js +29 -83
- package/src/count.js +5 -3
- package/src/diff.js +7 -5
- package/src/difftochanges.js +17 -14
- package/src/dom/createelement.js +11 -9
- package/src/dom/emittermixin.js +43 -84
- package/src/dom/getancestors.js +2 -2
- package/src/dom/getborderwidths.js +2 -2
- package/src/dom/getcommonancestor.js +3 -3
- package/src/dom/getdatafromelement.js +2 -2
- package/src/dom/getpositionedancestor.js +1 -2
- package/src/dom/global.js +8 -10
- package/src/dom/indexof.js +2 -2
- package/src/dom/insertat.js +3 -3
- package/src/dom/iscomment.js +0 -3
- package/src/dom/isnode.js +0 -3
- package/src/dom/isrange.js +0 -3
- package/src/dom/istext.js +0 -3
- package/src/dom/isvisible.js +0 -3
- package/src/dom/iswindow.js +0 -3
- package/src/dom/position.js +110 -133
- package/src/dom/rect.js +42 -52
- package/src/dom/remove.js +1 -1
- package/src/dom/resizeobserver.js +10 -35
- package/src/dom/scroll.js +85 -91
- package/src/dom/setdatainelement.js +2 -2
- package/src/dom/tounit.js +1 -10
- package/src/elementreplacer.js +2 -2
- package/src/emittermixin.js +48 -48
- package/src/env.js +14 -75
- package/src/eventinfo.js +2 -2
- package/src/fastdiff.js +115 -96
- package/src/first.js +0 -3
- package/src/focustracker.js +10 -18
- package/src/index.js +17 -0
- package/src/inserttopriorityarray.js +2 -2
- package/src/isiterable.js +2 -2
- package/src/keyboard.js +20 -21
- package/src/keystrokehandler.js +26 -24
- package/src/language.js +1 -2
- package/src/locale.js +11 -14
- package/src/mapsequal.js +3 -3
- package/src/mix.js +15 -13
- package/src/nth.js +0 -4
- package/src/objecttomap.js +6 -4
- package/src/observablemixin.js +126 -150
- package/src/priorities.js +0 -9
- package/src/splicearray.js +12 -11
- package/src/spy.js +1 -1
- package/src/tomap.js +7 -5
- package/src/translation-service.js +70 -52
- package/src/uid.js +5 -3
- package/src/unicode.js +9 -15
- package/src/version.js +32 -26
package/src/emittermixin.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* @module utils/emittermixin
|
|
7
7
|
*/
|
|
8
|
-
/* eslint-disable new-cap */
|
|
9
8
|
import EventInfo from './eventinfo';
|
|
10
9
|
import uid from './uid';
|
|
11
10
|
import priorities from './priorities';
|
|
@@ -16,18 +15,11 @@ import CKEditorError from './ckeditorerror';
|
|
|
16
15
|
const _listeningTo = Symbol('listeningTo');
|
|
17
16
|
const _emitterId = Symbol('emitterId');
|
|
18
17
|
const _delegations = Symbol('delegations');
|
|
19
|
-
|
|
20
|
-
* Mixin that injects the {@link ~Emitter events API} into its host.
|
|
21
|
-
*
|
|
22
|
-
* Read more about the concept of emitters in the:
|
|
23
|
-
* * {@glink framework/guides/architecture/core-editor-architecture#event-system-and-observables Event system and observables}
|
|
24
|
-
* section of the {@glink framework/guides/architecture/core-editor-architecture Core editor architecture} guide.
|
|
25
|
-
* * {@glink framework/guides/deep-dive/event-system Event system} deep dive guide.
|
|
26
|
-
*
|
|
27
|
-
* @mixin EmitterMixin
|
|
28
|
-
* @implements module:utils/emittermixin~Emitter
|
|
29
|
-
*/
|
|
18
|
+
const defaultEmitterClass = EmitterMixin(Object);
|
|
30
19
|
export default function EmitterMixin(base) {
|
|
20
|
+
if (!base) {
|
|
21
|
+
return defaultEmitterClass;
|
|
22
|
+
}
|
|
31
23
|
class Mixin extends base {
|
|
32
24
|
on(event, callback, options) {
|
|
33
25
|
this.listenTo(this, event, callback, options);
|
|
@@ -250,24 +242,21 @@ export default function EmitterMixin(base) {
|
|
|
250
242
|
}
|
|
251
243
|
return Mixin;
|
|
252
244
|
}
|
|
253
|
-
export const Emitter = EmitterMixin(Object);
|
|
254
245
|
// Backward compatibility with `mix`
|
|
255
246
|
([
|
|
256
247
|
'on', 'once', 'off', 'listenTo',
|
|
257
248
|
'stopListening', 'fire', 'delegate', 'stopDelegating',
|
|
258
249
|
'_addEventListener', '_removeEventListener'
|
|
259
250
|
]).forEach(key => {
|
|
260
|
-
EmitterMixin[key] =
|
|
251
|
+
EmitterMixin[key] = defaultEmitterClass.prototype[key];
|
|
261
252
|
});
|
|
262
253
|
/**
|
|
263
254
|
* Checks if `listeningEmitter` listens to an emitter with given `listenedToEmitterId` and if so, returns that emitter.
|
|
264
255
|
* If not, returns `null`.
|
|
265
256
|
*
|
|
266
257
|
* @internal
|
|
267
|
-
* @
|
|
268
|
-
* @param
|
|
269
|
-
* @param {String} listenedToEmitterId Unique emitter id of emitter listened to.
|
|
270
|
-
* @returns {module:utils/emittermixin~Emitter|null}
|
|
258
|
+
* @param listeningEmitter An emitter that listens.
|
|
259
|
+
* @param listenedToEmitterId Unique emitter id of emitter listened to.
|
|
271
260
|
*/
|
|
272
261
|
export function _getEmitterListenedTo(listeningEmitter, listenedToEmitterId) {
|
|
273
262
|
const listeningTo = listeningEmitter[_listeningTo];
|
|
@@ -282,9 +271,8 @@ export function _getEmitterListenedTo(listeningEmitter, listenedToEmitterId) {
|
|
|
282
271
|
* **Note:** `_emitterId` can be set only once.
|
|
283
272
|
*
|
|
284
273
|
* @internal
|
|
285
|
-
* @
|
|
286
|
-
* @param
|
|
287
|
-
* @param {String} [id] Unique id to set. If not passed, random unique id will be set.
|
|
274
|
+
* @param emitter An emitter for which id will be set.
|
|
275
|
+
* @param id Unique id to set. If not passed, random unique id will be set.
|
|
288
276
|
*/
|
|
289
277
|
export function _setEmitterId(emitter, id) {
|
|
290
278
|
if (!emitter[_emitterId]) {
|
|
@@ -295,16 +283,16 @@ export function _setEmitterId(emitter, id) {
|
|
|
295
283
|
* Returns emitter's unique id.
|
|
296
284
|
*
|
|
297
285
|
* @internal
|
|
298
|
-
* @
|
|
299
|
-
* @param {module:utils/emittermixin~Emitter} emitter An emitter which id will be returned.
|
|
300
|
-
* @returns {String|undefined}
|
|
286
|
+
* @param emitter An emitter which id will be returned.
|
|
301
287
|
*/
|
|
302
288
|
export function _getEmitterId(emitter) {
|
|
303
289
|
return emitter[_emitterId];
|
|
304
290
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
291
|
+
/**
|
|
292
|
+
* Gets the internal `_events` property of the given object.
|
|
293
|
+
* `_events` property store all lists with callbacks for registered event names.
|
|
294
|
+
* If there were no events registered on the object, empty `_events` object is created.
|
|
295
|
+
*/
|
|
308
296
|
function getEvents(source) {
|
|
309
297
|
if (!source._events) {
|
|
310
298
|
Object.defineProperty(source, '_events', {
|
|
@@ -313,18 +301,22 @@ function getEvents(source) {
|
|
|
313
301
|
}
|
|
314
302
|
return source._events;
|
|
315
303
|
}
|
|
316
|
-
|
|
304
|
+
/**
|
|
305
|
+
* Creates event node for generic-specific events relation architecture.
|
|
306
|
+
*/
|
|
317
307
|
function makeEventNode() {
|
|
318
308
|
return {
|
|
319
309
|
callbacks: [],
|
|
320
310
|
childEvents: []
|
|
321
311
|
};
|
|
322
312
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Creates an architecture for generic-specific events relation.
|
|
315
|
+
* If needed, creates all events for given eventName, i.e. if the first registered event
|
|
316
|
+
* is foo:bar:abc, it will create foo:bar:abc, foo:bar and foo event and tie them together.
|
|
317
|
+
* It also copies callbacks from more generic events to more specific events when
|
|
318
|
+
* specific events are created.
|
|
319
|
+
*/
|
|
328
320
|
function createEventNamespace(source, eventName) {
|
|
329
321
|
const events = getEvents(source);
|
|
330
322
|
// First, check if the event we want to add to the structure already exists.
|
|
@@ -375,9 +367,11 @@ function createEventNamespace(source, eventName) {
|
|
|
375
367
|
events[name].childEvents.push(childEventName);
|
|
376
368
|
}
|
|
377
369
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
370
|
+
/**
|
|
371
|
+
* Gets an array containing callbacks list for a given event and it's more specific events.
|
|
372
|
+
* I.e. if given event is foo:bar and there is also foo:bar:abc event registered, this will
|
|
373
|
+
* return callback list of foo:bar and foo:bar:abc (but not foo).
|
|
374
|
+
*/
|
|
381
375
|
function getCallbacksListsForNamespace(source, eventName) {
|
|
382
376
|
const eventNode = getEvents(source)[eventName];
|
|
383
377
|
if (!eventNode) {
|
|
@@ -390,9 +384,11 @@ function getCallbacksListsForNamespace(source, eventName) {
|
|
|
390
384
|
}
|
|
391
385
|
return callbacksLists;
|
|
392
386
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
387
|
+
/**
|
|
388
|
+
* Get the list of callbacks for a given event, but only if there any callbacks have been registered.
|
|
389
|
+
* If there are no callbacks registered for given event, it checks if this is a specific event and looks
|
|
390
|
+
* for callbacks for it's more generic version.
|
|
391
|
+
*/
|
|
396
392
|
function getCallbacksForEvent(source, eventName) {
|
|
397
393
|
let event;
|
|
398
394
|
if (!source._events || !(event = source._events[eventName]) || !event.callbacks.length) {
|
|
@@ -409,13 +405,13 @@ function getCallbacksForEvent(source, eventName) {
|
|
|
409
405
|
}
|
|
410
406
|
return event.callbacks;
|
|
411
407
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
408
|
+
/**
|
|
409
|
+
* Fires delegated events for given map of destinations.
|
|
410
|
+
*
|
|
411
|
+
* @param destinations A map containing `[ {@link module:utils/emittermixin~Emitter}, "event name" ]` pair destinations.
|
|
412
|
+
* @param eventInfo The original event info object.
|
|
413
|
+
* @param fireArgs Arguments the original event was fired with.
|
|
414
|
+
*/
|
|
419
415
|
function fireDelegatedEvents(destinations, eventInfo, fireArgs) {
|
|
420
416
|
for (let [emitter, name] of destinations) {
|
|
421
417
|
if (!name) {
|
|
@@ -429,7 +425,9 @@ function fireDelegatedEvents(destinations, eventInfo, fireArgs) {
|
|
|
429
425
|
emitter.fire(delegatedInfo, ...fireArgs);
|
|
430
426
|
}
|
|
431
427
|
}
|
|
432
|
-
|
|
428
|
+
/**
|
|
429
|
+
* Helper for registering event callback on the emitter.
|
|
430
|
+
*/
|
|
433
431
|
function addEventListener(listener, emitter, event, callback, options) {
|
|
434
432
|
if (emitter._addEventListener) {
|
|
435
433
|
emitter._addEventListener(event, callback, options);
|
|
@@ -440,7 +438,9 @@ function addEventListener(listener, emitter, event, callback, options) {
|
|
|
440
438
|
(listener._addEventListener).call(emitter, event, callback, options);
|
|
441
439
|
}
|
|
442
440
|
}
|
|
443
|
-
|
|
441
|
+
/**
|
|
442
|
+
* Helper for removing event callback from the emitter.
|
|
443
|
+
*/
|
|
444
444
|
function removeEventListener(listener, emitter, event, callback) {
|
|
445
445
|
if (emitter._removeEventListener) {
|
|
446
446
|
emitter._removeEventListener(event, callback);
|
package/src/env.js
CHANGED
|
@@ -9,8 +9,6 @@
|
|
|
9
9
|
/**
|
|
10
10
|
* Safely returns `userAgent` from browser's navigator API in a lower case.
|
|
11
11
|
* If navigator API is not available it will return an empty string.
|
|
12
|
-
*
|
|
13
|
-
* @returns {String}
|
|
14
12
|
*/
|
|
15
13
|
export function getUserAgent() {
|
|
16
14
|
// In some environments navigator API might not be available.
|
|
@@ -24,73 +22,16 @@ export function getUserAgent() {
|
|
|
24
22
|
const userAgent = getUserAgent();
|
|
25
23
|
/**
|
|
26
24
|
* A namespace containing environment and browser information.
|
|
27
|
-
*
|
|
28
|
-
* @namespace
|
|
29
25
|
*/
|
|
30
26
|
const env = {
|
|
31
|
-
/**
|
|
32
|
-
* Indicates that the application is running on Macintosh.
|
|
33
|
-
*
|
|
34
|
-
* @static
|
|
35
|
-
* @type {Boolean}
|
|
36
|
-
*/
|
|
37
27
|
isMac: isMac(userAgent),
|
|
38
|
-
/**
|
|
39
|
-
* Indicates that the application is running on Windows.
|
|
40
|
-
*
|
|
41
|
-
* @static
|
|
42
|
-
* @type {Boolean}
|
|
43
|
-
*/
|
|
44
28
|
isWindows: isWindows(userAgent),
|
|
45
|
-
/**
|
|
46
|
-
* Indicates that the application is running in Firefox (Gecko).
|
|
47
|
-
*
|
|
48
|
-
* @static
|
|
49
|
-
* @type {Boolean}
|
|
50
|
-
*/
|
|
51
29
|
isGecko: isGecko(userAgent),
|
|
52
|
-
/**
|
|
53
|
-
* Indicates that the application is running in Safari.
|
|
54
|
-
*
|
|
55
|
-
* @static
|
|
56
|
-
* @type {Boolean}
|
|
57
|
-
*/
|
|
58
30
|
isSafari: isSafari(userAgent),
|
|
59
|
-
/**
|
|
60
|
-
* Indicates the the application is running in iOS.
|
|
61
|
-
*
|
|
62
|
-
* @static
|
|
63
|
-
* @type {Boolean}
|
|
64
|
-
*/
|
|
65
31
|
isiOS: isiOS(userAgent),
|
|
66
|
-
/**
|
|
67
|
-
* Indicates that the application is running on Android mobile device.
|
|
68
|
-
*
|
|
69
|
-
* @static
|
|
70
|
-
* @type {Boolean}
|
|
71
|
-
*/
|
|
72
32
|
isAndroid: isAndroid(userAgent),
|
|
73
|
-
/**
|
|
74
|
-
* Indicates that the application is running in a browser using the Blink engine.
|
|
75
|
-
*
|
|
76
|
-
* @static
|
|
77
|
-
* @type {Boolean}
|
|
78
|
-
*/
|
|
79
33
|
isBlink: isBlink(userAgent),
|
|
80
|
-
/**
|
|
81
|
-
* Environment features information.
|
|
82
|
-
*
|
|
83
|
-
* @memberOf module:utils/env~env
|
|
84
|
-
* @namespace
|
|
85
|
-
*/
|
|
86
34
|
features: {
|
|
87
|
-
/**
|
|
88
|
-
* Indicates that the environment supports ES2018 Unicode property escapes — like `\p{P}` or `\p{L}`.
|
|
89
|
-
* More information about unicode properties might be found
|
|
90
|
-
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
|
|
91
|
-
*
|
|
92
|
-
* @type {Boolean}
|
|
93
|
-
*/
|
|
94
35
|
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
|
|
95
36
|
}
|
|
96
37
|
};
|
|
@@ -98,8 +39,8 @@ export default env;
|
|
|
98
39
|
/**
|
|
99
40
|
* Checks if User Agent represented by the string is running on Macintosh.
|
|
100
41
|
*
|
|
101
|
-
* @param
|
|
102
|
-
* @returns
|
|
42
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
43
|
+
* @returns Whether User Agent is running on Macintosh or not.
|
|
103
44
|
*/
|
|
104
45
|
export function isMac(userAgent) {
|
|
105
46
|
return userAgent.indexOf('macintosh') > -1;
|
|
@@ -107,8 +48,8 @@ export function isMac(userAgent) {
|
|
|
107
48
|
/**
|
|
108
49
|
* Checks if User Agent represented by the string is running on Windows.
|
|
109
50
|
*
|
|
110
|
-
* @param
|
|
111
|
-
* @returns
|
|
51
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
52
|
+
* @returns Whether User Agent is running on Windows or not.
|
|
112
53
|
*/
|
|
113
54
|
export function isWindows(userAgent) {
|
|
114
55
|
return userAgent.indexOf('windows') > -1;
|
|
@@ -116,8 +57,8 @@ export function isWindows(userAgent) {
|
|
|
116
57
|
/**
|
|
117
58
|
* Checks if User Agent represented by the string is Firefox (Gecko).
|
|
118
59
|
*
|
|
119
|
-
* @param
|
|
120
|
-
* @returns
|
|
60
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
61
|
+
* @returns Whether User Agent is Firefox or not.
|
|
121
62
|
*/
|
|
122
63
|
export function isGecko(userAgent) {
|
|
123
64
|
return !!userAgent.match(/gecko\/\d+/);
|
|
@@ -125,8 +66,8 @@ export function isGecko(userAgent) {
|
|
|
125
66
|
/**
|
|
126
67
|
* Checks if User Agent represented by the string is Safari.
|
|
127
68
|
*
|
|
128
|
-
* @param
|
|
129
|
-
* @returns
|
|
69
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
70
|
+
* @returns Whether User Agent is Safari or not.
|
|
130
71
|
*/
|
|
131
72
|
export function isSafari(userAgent) {
|
|
132
73
|
return userAgent.indexOf(' applewebkit/') > -1 && userAgent.indexOf('chrome') === -1;
|
|
@@ -134,8 +75,8 @@ export function isSafari(userAgent) {
|
|
|
134
75
|
/**
|
|
135
76
|
* Checks if User Agent represented by the string is running in iOS.
|
|
136
77
|
*
|
|
137
|
-
* @param
|
|
138
|
-
* @returns
|
|
78
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
79
|
+
* @returns Whether User Agent is running in iOS or not.
|
|
139
80
|
*/
|
|
140
81
|
export function isiOS(userAgent) {
|
|
141
82
|
// "Request mobile site" || "Request desktop site".
|
|
@@ -144,8 +85,8 @@ export function isiOS(userAgent) {
|
|
|
144
85
|
/**
|
|
145
86
|
* Checks if User Agent represented by the string is Android mobile device.
|
|
146
87
|
*
|
|
147
|
-
* @param
|
|
148
|
-
* @returns
|
|
88
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
89
|
+
* @returns Whether User Agent is Safari or not.
|
|
149
90
|
*/
|
|
150
91
|
export function isAndroid(userAgent) {
|
|
151
92
|
return userAgent.indexOf('android') > -1;
|
|
@@ -153,8 +94,8 @@ export function isAndroid(userAgent) {
|
|
|
153
94
|
/**
|
|
154
95
|
* Checks if User Agent represented by the string is Blink engine.
|
|
155
96
|
*
|
|
156
|
-
* @param
|
|
157
|
-
* @returns
|
|
97
|
+
* @param userAgent **Lowercase** `navigator.userAgent` string.
|
|
98
|
+
* @returns Whether User Agent is Blink engine or not.
|
|
158
99
|
*/
|
|
159
100
|
export function isBlink(userAgent) {
|
|
160
101
|
// The Edge browser before switching to the Blink engine used to report itself as Chrome (and "Edge/")
|
|
@@ -165,8 +106,6 @@ export function isBlink(userAgent) {
|
|
|
165
106
|
* Checks if the current environment supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
|
|
166
107
|
* More information about unicode properties might be found
|
|
167
108
|
* [in Unicode Standard Annex #44](https://www.unicode.org/reports/tr44/#GC_Values_Table).
|
|
168
|
-
*
|
|
169
|
-
* @returns {Boolean}
|
|
170
109
|
*/
|
|
171
110
|
export function isRegExpUnicodePropertySupported() {
|
|
172
111
|
let isSupported = false;
|
package/src/eventinfo.js
CHANGED
|
@@ -12,8 +12,8 @@ import spy from './spy';
|
|
|
12
12
|
*/
|
|
13
13
|
export default class EventInfo {
|
|
14
14
|
/**
|
|
15
|
-
* @param
|
|
16
|
-
* @param
|
|
15
|
+
* @param source The emitter.
|
|
16
|
+
* @param name The event name.
|
|
17
17
|
*/
|
|
18
18
|
constructor(source, name) {
|
|
19
19
|
this.source = source;
|
package/src/fastdiff.js
CHANGED
|
@@ -2,94 +2,112 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2022, 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
|
+
/**
|
|
6
|
+
* @module utils/fastdiff
|
|
7
|
+
*/
|
|
5
8
|
/**
|
|
6
9
|
* Finds positions of the first and last change in the given string/array and generates a set of changes:
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* fastDiff( '12a', '12xyza' );
|
|
13
|
+
* // [ { index: 2, type: 'insert', values: [ 'x', 'y', 'z' ] } ]
|
|
10
14
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
15
|
+
* fastDiff( '12a', '12aa' );
|
|
16
|
+
* // [ { index: 3, type: 'insert', values: [ 'a' ] } ]
|
|
13
17
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
18
|
+
* fastDiff( '12xyza', '12a' );
|
|
19
|
+
* // [ { index: 2, type: 'delete', howMany: 3 } ]
|
|
16
20
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
21
|
+
* fastDiff( [ '1', '2', 'a', 'a' ], [ '1', '2', 'a' ] );
|
|
22
|
+
* // [ { index: 3, type: 'delete', howMany: 1 } ]
|
|
19
23
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
24
|
+
* fastDiff( [ '1', '2', 'a', 'b', 'c', '3' ], [ '2', 'a', 'b' ] );
|
|
25
|
+
* // [ { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] }, { index: 3, type: 'delete', howMany: 6 } ]
|
|
26
|
+
* ```
|
|
22
27
|
*
|
|
23
28
|
* Passed arrays can contain any type of data, however to compare them correctly custom comparator function
|
|
24
29
|
* should be passed as a third parameter:
|
|
25
30
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* fastDiff( [ { value: 1 }, { value: 2 } ], [ { value: 1 }, { value: 3 } ], ( a, b ) => {
|
|
33
|
+
* return a.value === b.value;
|
|
34
|
+
* } );
|
|
35
|
+
* // [ { index: 1, type: 'insert', values: [ { value: 3 } ] }, { index: 2, type: 'delete', howMany: 1 } ]
|
|
36
|
+
* ```
|
|
30
37
|
*
|
|
31
38
|
* The resulted set of changes can be applied to the input in order to transform it into the output, for example:
|
|
32
39
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* let input = '12abc3';
|
|
42
|
+
* const output = '2ab';
|
|
43
|
+
* const changes = fastDiff( input, output );
|
|
36
44
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
45
|
+
* changes.forEach( change => {
|
|
46
|
+
* if ( change.type == 'insert' ) {
|
|
47
|
+
* input = input.substring( 0, change.index ) + change.values.join( '' ) + input.substring( change.index );
|
|
48
|
+
* } else if ( change.type == 'delete' ) {
|
|
49
|
+
* input = input.substring( 0, change.index ) + input.substring( change.index + change.howMany );
|
|
50
|
+
* }
|
|
51
|
+
* } );
|
|
44
52
|
*
|
|
45
|
-
*
|
|
53
|
+
* // input equals output now
|
|
54
|
+
* ```
|
|
46
55
|
*
|
|
47
56
|
* or in case of arrays:
|
|
48
57
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* let input = [ '1', '2', 'a', 'b', 'c', '3' ];
|
|
60
|
+
* const output = [ '2', 'a', 'b' ];
|
|
61
|
+
* const changes = fastDiff( input, output );
|
|
52
62
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
63
|
+
* changes.forEach( change => {
|
|
64
|
+
* if ( change.type == 'insert' ) {
|
|
65
|
+
* input = input.slice( 0, change.index ).concat( change.values, input.slice( change.index ) );
|
|
66
|
+
* } else if ( change.type == 'delete' ) {
|
|
67
|
+
* input = input.slice( 0, change.index ).concat( input.slice( change.index + change.howMany ) );
|
|
68
|
+
* }
|
|
69
|
+
* } );
|
|
60
70
|
*
|
|
61
|
-
*
|
|
71
|
+
* // input equals output now
|
|
72
|
+
* ```
|
|
62
73
|
*
|
|
63
74
|
* By passing `true` as the fourth parameter (`atomicChanges`) the output of this function will become compatible with
|
|
64
75
|
* the {@link module:utils/diff~diff `diff()`} function:
|
|
65
76
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* fastDiff( '12a', '12xyza', undefined, true );
|
|
79
|
+
* // [ 'equal', 'equal', 'insert', 'insert', 'insert', 'equal' ]
|
|
80
|
+
* ```
|
|
68
81
|
*
|
|
69
82
|
* The default output format of this function is compatible with the output format of
|
|
70
83
|
* {@link module:utils/difftochanges~diffToChanges `diffToChanges()`}. The `diffToChanges()` input format is, in turn,
|
|
71
84
|
* compatible with the output of {@link module:utils/diff~diff `diff()`}:
|
|
72
85
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* @
|
|
87
|
-
* @
|
|
88
|
-
* @param
|
|
86
|
+
* ```ts
|
|
87
|
+
* const a = '1234';
|
|
88
|
+
* const b = '12xyz34';
|
|
89
|
+
*
|
|
90
|
+
* // Both calls will return the same results (grouped changes format).
|
|
91
|
+
* fastDiff( a, b );
|
|
92
|
+
* diffToChanges( diff( a, b ) );
|
|
93
|
+
*
|
|
94
|
+
* // Again, both calls will return the same results (atomic changes format).
|
|
95
|
+
* fastDiff( a, b, undefined, true );
|
|
96
|
+
* diff( a, b );
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @typeParam T The type of array elements.
|
|
100
|
+
* @typeParam AtomicChanges The type of `atomicChanges` parameter (selects the result type).
|
|
101
|
+
* @param a Input array or string.
|
|
102
|
+
* @param b Input array or string.
|
|
103
|
+
* @param cmp Optional function used to compare array values, by default `===` (strict equal operator) is used.
|
|
104
|
+
* @param atomicChanges Whether an array of `inset|delete|equal` operations should
|
|
89
105
|
* be returned instead of changes set. This makes this function compatible with {@link module:utils/diff~diff `diff()`}.
|
|
90
|
-
*
|
|
106
|
+
* Defaults to `false`.
|
|
107
|
+
* @returns Array of changes. The elements are either {@link module:utils/diff~DiffResult} or {@link module:utils/difftochanges~Change},
|
|
108
|
+
* depending on `atomicChanges` parameter.
|
|
91
109
|
*/
|
|
92
|
-
export default function fastDiff(a, b, cmp, atomicChanges
|
|
110
|
+
export default function fastDiff(a, b, cmp, atomicChanges) {
|
|
93
111
|
// Set the comparator function.
|
|
94
112
|
cmp = cmp || function (a, b) {
|
|
95
113
|
return a === b;
|
|
@@ -105,24 +123,23 @@ export default function fastDiff(a, b, cmp, atomicChanges = false) {
|
|
|
105
123
|
// Find first and last change.
|
|
106
124
|
const changeIndexes = findChangeBoundaryIndexes(arrayA, arrayB, cmp);
|
|
107
125
|
// Transform into changes array.
|
|
108
|
-
|
|
126
|
+
const result = atomicChanges ?
|
|
127
|
+
changeIndexesToAtomicChanges(changeIndexes, arrayB.length) :
|
|
128
|
+
changeIndexesToChanges(arrayB, changeIndexes);
|
|
129
|
+
return result;
|
|
109
130
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// @returns {Object}
|
|
123
|
-
// @returns {Number} return.firstIndex Index of the first change in both values (always the same for both).
|
|
124
|
-
// @returns {Number} result.lastIndexOld Index of the last common value in `arr1`.
|
|
125
|
-
// @returns {Number} result.lastIndexNew Index of the last common value in `arr2`.
|
|
131
|
+
/**
|
|
132
|
+
* Finds position of the first and last change in the given arrays. For example:
|
|
133
|
+
*
|
|
134
|
+
* ```ts
|
|
135
|
+
* const indexes = findChangeBoundaryIndexes( [ '1', '2', '3', '4' ], [ '1', '3', '4', '2', '4' ] );
|
|
136
|
+
* console.log( indexes ); // { firstIndex: 1, lastIndexOld: 3, lastIndexNew: 4 }
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* The above indexes means that in the first array the modified part is `1[23]4` and in the second array it is `1[342]4`.
|
|
140
|
+
* Based on such indexes, array with `insert`/`delete` operations which allows transforming first value into the second one
|
|
141
|
+
* can be generated.
|
|
142
|
+
*/
|
|
126
143
|
function findChangeBoundaryIndexes(arr1, arr2, cmp) {
|
|
127
144
|
// Find the first difference between passed values.
|
|
128
145
|
const firstIndex = findFirstDifferenceIndex(arr1, arr2, cmp);
|
|
@@ -150,12 +167,9 @@ function findChangeBoundaryIndexes(arr1, arr2, cmp) {
|
|
|
150
167
|
const lastIndexNew = arr2.length - lastIndex;
|
|
151
168
|
return { firstIndex, lastIndexOld, lastIndexNew };
|
|
152
169
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
// @param {Array} arr2
|
|
157
|
-
// @param {Function} cmp Comparator function.
|
|
158
|
-
// @returns {Number}
|
|
170
|
+
/**
|
|
171
|
+
* Returns a first index on which given arrays differ. If both arrays are the same, -1 is returned.
|
|
172
|
+
*/
|
|
159
173
|
function findFirstDifferenceIndex(arr1, arr2, cmp) {
|
|
160
174
|
for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
|
|
161
175
|
if (arr1[i] === undefined || arr2[i] === undefined || !cmp(arr1[i], arr2[i])) {
|
|
@@ -164,21 +178,24 @@ function findFirstDifferenceIndex(arr1, arr2, cmp) {
|
|
|
164
178
|
}
|
|
165
179
|
return -1; // Return -1 if arrays are equal.
|
|
166
180
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Returns a copy of the given array with `howMany` elements removed starting from the beginning and in reversed order.
|
|
183
|
+
*
|
|
184
|
+
* @param arr Array to be processed.
|
|
185
|
+
* @param howMany How many elements from array beginning to remove.
|
|
186
|
+
* @returns Shortened and reversed array.
|
|
187
|
+
*/
|
|
172
188
|
function cutAndReverse(arr, howMany) {
|
|
173
189
|
return arr.slice(howMany).reverse();
|
|
174
190
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
191
|
+
/**
|
|
192
|
+
* Generates changes array based on change indexes from `findChangeBoundaryIndexes` function. This function will
|
|
193
|
+
* generate array with 0 (no changes), 1 (deletion or insertion) or 2 records (insertion and deletion).
|
|
194
|
+
*
|
|
195
|
+
* @param newArray New array for which change indexes were calculated.
|
|
196
|
+
* @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
|
|
197
|
+
* @returns Array of changes compatible with {@link module:utils/difftochanges~diffToChanges} format.
|
|
198
|
+
*/
|
|
182
199
|
function changeIndexesToChanges(newArray, changeIndexes) {
|
|
183
200
|
const result = [];
|
|
184
201
|
const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
|
|
@@ -201,11 +218,13 @@ function changeIndexesToChanges(newArray, changeIndexes) {
|
|
|
201
218
|
}
|
|
202
219
|
return result;
|
|
203
220
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Generates array with set `equal|insert|delete` operations based on change indexes from `findChangeBoundaryIndexes` function.
|
|
223
|
+
*
|
|
224
|
+
* @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
|
|
225
|
+
* @param newLength Length of the new array on which `findChangeBoundaryIndexes` calculated change indexes.
|
|
226
|
+
* @returns Array of changes compatible with {@link module:utils/diff~diff} format.
|
|
227
|
+
*/
|
|
209
228
|
function changeIndexesToAtomicChanges(changeIndexes, newLength) {
|
|
210
229
|
const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
|
|
211
230
|
// No changes.
|