Socket
Socket
Sign inDemoInstall

@ckeditor/ckeditor5-utils

Package Overview
Dependencies
Maintainers
1
Versions
647
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ckeditor/ckeditor5-utils - npm Package Compare versions

Comparing version 11.1.0 to 12.0.0

23

CHANGELOG.md
Changelog
=========
## [12.0.0](https://github.com/ckeditor/ckeditor5-utils/compare/v11.1.0...v12.0.0) (2019-02-28)
### Features
* Introduce `Collection.has()` method. Closes [#266](https://github.com/ckeditor/ckeditor5-utils/issues/266). ([312d55e](https://github.com/ckeditor/ckeditor5-utils/commit/312d55e))
### Bug fixes
* Prevent infinite loops on `.once()`. Closes [#272](https://github.com/ckeditor/ckeditor5-utils/issues/272). Closes [#204](https://github.com/ckeditor/ckeditor5-utils/issues/204). ([54b8108](https://github.com/ckeditor/ckeditor5-utils/commit/54b8108))
* Fixed memory leaks during editor initialization and destruction (see [ckeditor/ckeditor5#1341](https://github.com/ckeditor/ckeditor5/issues/1341)). ([94bc59e](https://github.com/ckeditor/ckeditor5-utils/commit/94bc59e))
### Other changes
* Configuration options should be cloned to prevent features from altering the original values. Closes [#257](https://github.com/ckeditor/ckeditor5-utils/issues/257). ([7981d4e](https://github.com/ckeditor/ckeditor5-utils/commit/7981d4e))
* DOM Elements will not be cloned when returned from config.get. Closes [#264](https://github.com/ckeditor/ckeditor5-utils/issues/264). ([4ad23b1](https://github.com/ckeditor/ckeditor5-utils/commit/4ad23b1))
* Optimized `diff()` function to use `fastDiff()` function internally for large data sets. Closes [#269](https://github.com/ckeditor/ckeditor5-utils/issues/269). ([ee9bed0](https://github.com/ckeditor/ckeditor5-utils/commit/ee9bed0))
* Replaced `for..of` statement in `EventEmitter` with `Array.prototype.forEach`. This changes allows building a React application using `create-react-app@2`. Closes [ckeditor/ckeditor5-react#40](https://github.com/ckeditor/ckeditor5-react/issues/40). ([900b54b](https://github.com/ckeditor/ckeditor5-utils/commit/900b54b))
### BREAKING CHANGES
* Upgraded minimal versions of Node to `8.0.0` and npm to `5.7.1`. See: [ckeditor/ckeditor5#1507](https://github.com/ckeditor/ckeditor5/issues/1507). ([612ea3c](https://github.com/ckeditor/ckeditor5-cloud-services/commit/612ea3c))
## [11.1.0](https://github.com/ckeditor/ckeditor5-utils/compare/v11.0.0...v11.1.0) (2018-12-05)

@@ -5,0 +28,0 @@

2

LICENSE.md

@@ -5,3 +5,3 @@ Software License Agreement

**CKEditor 5 utilities** – https://github.com/ckeditor/ckeditor5-utils <br>
Copyright (c) 2003-2018, [CKSource](http://cksource.com) Frederico Knabben. All rights reserved.
Copyright (c) 2003-2019, [CKSource](http://cksource.com) Frederico Knabben. All rights reserved.

@@ -8,0 +8,0 @@ Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html).

{
"name": "@ckeditor/ckeditor5-utils",
"version": "11.1.0",
"version": "12.0.0",
"description": "Miscellaneous utils used by CKEditor 5.",

@@ -12,21 +12,21 @@ "keywords": [

"dependencies": {
"ckeditor5": "^11.2.0",
"ckeditor5": "^12.0.0",
"lodash-es": "^4.17.10"
},
"devDependencies": {
"@ckeditor/ckeditor5-core": "^11.1.0",
"@ckeditor/ckeditor5-engine": "^12.0.0",
"@ckeditor/ckeditor5-core": "^12.0.0",
"@ckeditor/ckeditor5-engine": "^13.0.0",
"del": "^2.2.0",
"eslint": "^5.5.0",
"eslint-config-ckeditor5": "^1.0.7",
"husky": "^0.14.3",
"eslint-config-ckeditor5": "^1.0.11",
"husky": "^1.3.1",
"lint-staged": "^7.0.0"
},
"engines": {
"node": ">=6.9.0",
"npm": ">=3.0.0"
"node": ">=8.0.0",
"npm": ">=5.7.1"
},
"author": "CKSource (http://cksource.com/)",
"license": "GPL-2.0-or-later",
"homepage": "https://ckeditor.com",
"homepage": "https://ckeditor.com/ckeditor-5",
"bugs": "https://github.com/ckeditor/ckeditor5-utils/issues",

@@ -43,4 +43,3 @@ "repository": {

"scripts": {
"lint": "eslint --quiet '**/*.js'",
"precommit": "lint-staged"
"lint": "eslint --quiet '**/*.js'"
},

@@ -55,3 +54,8 @@ "lint-staged": {

"packages/**"
]
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -213,15 +213,32 @@ */

/**
* Returns a boolean indicating whether the collection contains an item.
*
* @param {Object|String} itemOrId The item or its id in the collection.
* @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
*/
has( itemOrId ) {
if ( typeof itemOrId == 'string' ) {
return this._itemMap.has( itemOrId );
} else { // Object
const idProperty = this._idProperty;
const id = itemOrId[ idProperty ];
return this._itemMap.has( id );
}
}
/**
* Gets index of item in the collection.
* When item is not defined in the collection then index will be equal -1.
*
* @param {String|Object} idOrItem The item or its id in the collection.
* @param {Object|String} itemOrId The item or its id in the collection.
* @returns {Number} Index of given item.
*/
getIndex( idOrItem ) {
getIndex( itemOrId ) {
let item;
if ( typeof idOrItem == 'string' ) {
item = this._itemMap.get( idOrItem );
if ( typeof itemOrId == 'string' ) {
item = this._itemMap.get( itemOrId );
} else {
item = idOrItem;
item = itemOrId;
}

@@ -437,4 +454,3 @@

* @returns {Object}
* @returns {module:utils/collection~Collection#bindTo#as} return.as
* @returns {module:utils/collection~Collection#bindTo#using} return.using
* @returns {module:utils/collection~CollectionBindToChain} The binding chain object.
*/

@@ -454,8 +470,2 @@ bindTo( externalCollection ) {

return {
/**
* Creates the class factory binding.
*
* @static
* @param {Function} Class Specifies which class factory is to be initialized.
*/
as: Class => {

@@ -465,10 +475,2 @@ this._setUpBindToBinding( item => new Class( item ) );

/**
* Creates callback or property binding.
*
* @static
* @param {Function|String} callbackOrProperty When the function is passed, it is used to
* produce the items. When the string is provided, the property value is used to create
* the bound collection items.
*/
using: callbackOrProperty => {

@@ -635,1 +637,26 @@ if ( typeof callbackOrProperty == 'function' ) {

mix( Collection, EmitterMixin );
/**
* An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
* providing functions that specify the type of the binding.
*
* See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
*
* @interface module:utils/collection~CollectionBindToChain
*/
/**
* Creates a callback or a property binding.
*
* @method #using
* @param {Function|String} callbackOrProperty When the function is passed, it should return
* the collection items. When the string is provided, the property value is used to create the bound collection items.
*/
/**
* Creates the class factory binding in which items of the source collection are passed to
* the constructor of the specified class.
*
* @method #as
* @param {Function} Class The class constructor used to create instances in the factory.
*/
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -10,3 +10,3 @@ */

import { isPlainObject } from 'lodash-es';
import { isPlainObject, isElement, cloneDeepWith } from 'lodash-es';

@@ -201,4 +201,4 @@ /**

// Always returns undefined for non existing configuration
return source ? source[ name ] : undefined;
// Always returns undefined for non existing configuration.
return source ? cloneConfig( source[ name ] ) : undefined;
}

@@ -220,1 +220,17 @@

}
// Clones configuration object or value.
// @param {*} source Source configuration
// @returns {*} Cloned configuration value.
function cloneConfig( source ) {
return cloneDeepWith( source, leaveDOMReferences );
}
// A customizer function for cloneDeepWith.
// It will leave references to DOM Elements instead of cloning them.
//
// @param {*} value
// @returns {Element|undefined}
function leaveDOMReferences( value ) {
return isElement( value ) ? value : undefined;
}
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -10,2 +10,4 @@ */

import fastDiff from '../src/fastdiff';
// The following code is based on the "O(NP) Sequence Comparison Algorithm"

@@ -20,2 +22,7 @@ // by Sun Wu, Udi Manber, Gene Myers, Webb Miller.

*
* This function is based on the "O(NP) Sequence Comparison Algorithm" by Sun Wu, Udi Manber, Gene Myers, Webb Miller.
* Unfortunately, while it gives the most precise results, its to complex for longer strings/arrow (above 200 items).
* Therefore, `diff()` automatically switches to {@link module:utils/fastdiff~fastDiff `fastDiff()`} when detecting
* such a scenario. The return formats of both functions are identical.
*
* @param {Array|String} a Input array or string.

@@ -32,2 +39,10 @@ * @param {Array|String} b Output array or string.

const aLength = a.length;
const bLength = b.length;
// Perform `fastDiff` for longer strings/arrays (see #269).
if ( aLength > 200 || bLength > 200 || aLength + bLength > 300 ) {
return diff.fastDiff( a, b, cmp, true );
}
// Temporary action type statics.

@@ -37,3 +52,3 @@ let _insert, _delete;

// Swapped the arrays to use the shorter one as the first one.
if ( b.length < a.length ) {
if ( bLength < aLength ) {
const tmp = a;

@@ -124,1 +139,5 @@

}
// Store the API in static property to easily overwrite it in tests.
// Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
diff.fastDiff = fastDiff;
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -35,8 +35,16 @@ */

once( event, callback, options ) {
let wasFired = false;
const onceCallback = function( event, ...args ) {
// Go off() at the first call.
event.off();
// Ensure the callback is called only once even if the callback itself leads to re-firing the event
// (which would call the callback again).
if ( !wasFired ) {
wasFired = true;
// Go with the original callback.
callback.call( this, event, ...args );
// Go off() at the first call.
event.off();
// Go with the original callback.
callback.call( this, event, ...args );
}
};

@@ -241,3 +249,5 @@

for ( const eventName of events ) {
// Originally there was a for..of loop which unfortunately caused an error in Babel that didn't allow
// build an application. See: https://github.com/ckeditor/ckeditor5-react/issues/40.
events.forEach( eventName => {
const destinations = this._delegations.get( eventName );

@@ -250,3 +260,3 @@

}
}
} );
}

@@ -253,0 +263,0 @@ };

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -11,4 +11,3 @@ */

/**
* Finds position of the first and last change in the given strings and generates set of changes. Set of changes
* can be applied to the input text in order to transform it into the output text, for example:
* Finds positions of the first and last change in the given string/array and generates a set of changes:
*

@@ -24,14 +23,22 @@ * fastDiff( '12a', '12xyza' );

*
* fastDiff( '12aa', '12a' );
* fastDiff( [ '1', '2', 'a', 'a' ], [ '1', '2', 'a' ] );
* // [ { index: 3, type: 'delete', howMany: 1 } ]
*
* fastDiff( '12abc3', '2ab' );
* fastDiff( [ '1', '2', 'a', 'b', 'c', '3' ], [ '2', 'a', 'b' ] );
* // [ { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] }, { index: 3, type: 'delete', howMany: 6 } ]
*
* Using returned results you can modify `oldText` to transform it into `newText`:
* Passed arrays can contain any type of data, however to compare them correctly custom comparator function
* should be passed as a third parameter:
*
* let input = '12abc3';
* const output = '2ab';
* const changes = fastDiff( input, output );
* fastDiff( [ { value: 1 }, { value: 2 } ], [ { value: 1 }, { value: 3 } ], ( a, b ) => {
* return a.value === b.value;
* } );
* // [ { index: 1, type: 'insert', values: [ { value: 3 } ] }, { index: 2, type: 'delete', howMany: 1 } ]
*
* The resulted set of changes can be applied to the input in order to transform it into the output, for example:
*
* let input = '12abc3';
* const output = '2ab';
* const changes = fastDiff( input, output );
*
* changes.forEach( change => {

@@ -45,62 +52,116 @@ * if ( change.type == 'insert' ) {

*
* input === output; // -> true
* // input equals output now
*
* The output format of this function is compatible with {@link module:utils/difftochanges~diffToChanges} output format.
* or in case of arrays:
*
* @param {String} oldText Input string.
* @param {String} newText Input string.
* let input = [ '1', '2', 'a', 'b', 'c', '3' ];
* const output = [ '2', 'a', 'b' ];
* const changes = fastDiff( input, output );
*
* changes.forEach( change => {
* if ( change.type == 'insert' ) {
* input = input.slice( 0, change.index ).concat( change.values, input.slice( change.index ) );
* } else if ( change.type == 'delete' ) {
* input = input.slice( 0, change.index ).concat( input.slice( change.index + change.howMany ) );
* }
* } );
*
* // input equals output now
*
* By passing `true` as the fourth parameter (`atomicChanges`) the output of this function will become compatible with
* the {@link module:utils/diff~diff `diff()`} function:
*
* fastDiff( '12a', '12xyza' );
* // [ 'equal', 'equal', 'insert', 'insert', 'insert', 'equal' ]
*
* The default output format of this function is compatible with the output format of
* {@link module:utils/difftochanges~diffToChanges `diffToChanges()`}. The `diffToChanges()` input format is, in turn,
* compatible with the output of {@link module:utils/diff~diff `diff()`}:
*
* const a = '1234';
* const b = '12xyz34';
*
* // Both calls will return the same results (grouped changes format).
* fastDiff( a, b );
* diffToChanges( diff( a, b ) );
*
* // Again, both calls will return the same results (atomic changes format).
* fastDiff( a, b, null, true );
* diff( a, b );
*
*
* @param {Array|String} a Input array or string.
* @param {Array|String} b Input array or string.
* @param {Function} [cmp] Optional function used to compare array values, by default `===` (strict equal operator) is used.
* @param {Boolean} [atomicChanges=false] Whether an array of `inset|delete|equal` operations should
* be returned instead of changes set. This makes this function compatible with {@link module:utils/diff~diff `diff()`}.
* @returns {Array} Array of changes.
*/
export default function fastDiff( oldText, newText ) {
// Check if both texts are equal.
if ( oldText === newText ) {
return [];
export default function fastDiff( a, b, cmp, atomicChanges = false ) {
// Set the comparator function.
cmp = cmp || function( a, b ) {
return a === b;
};
// Transform text or any iterable into arrays for easier, consistent processing.
if ( !Array.isArray( a ) ) {
a = Array.from( a );
}
const changeIndexes = findChangeBoundaryIndexes( oldText, newText );
if ( !Array.isArray( b ) ) {
b = Array.from( b );
}
return changeIndexesToChanges( newText, changeIndexes );
// Find first and last change.
const changeIndexes = findChangeBoundaryIndexes( a, b, cmp );
// Transform into changes array.
return atomicChanges ? changeIndexesToAtomicChanges( changeIndexes, b.length ) : changeIndexesToChanges( b, changeIndexes );
}
// Finds position of the first and last change in the given strings. For example:
// Finds position of the first and last change in the given arrays. For example:
//
// const indexes = findChangeBoundaryIndexes( '1234', '13424' );
// const indexes = findChangeBoundaryIndexes( [ '1', '2', '3', '4' ], [ '1', '3', '4', '2', '4' ] );
// console.log( indexes ); // { firstIndex: 1, lastIndexOld: 3, lastIndexNew: 4 }
//
// The above indexes means that in `oldText` modified part is `1[23]4` and in the `newText` it is `1[342]4`.
// Based on such indexes, array with `insert`/`delete` operations which allows transforming
// old text to the new one can be generated.
// 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`.
// Based on such indexes, array with `insert`/`delete` operations which allows transforming first value into the second one
// can be generated.
//
// It is expected that `oldText` and `newText` are different.
//
// @param {String} oldText
// @param {String} newText
// @param {Array} arr1
// @param {Array} arr2
// @param {Function} cmp Comparator function.
// @returns {Object}
// @returns {Number} return.firstIndex Index of the first change in both strings (always the same for both).
// @returns {Number} result.lastIndexOld Index of the last common character in `oldText` string.
// @returns {Number} result.lastIndexNew Index of the last common character in `newText` string.
function findChangeBoundaryIndexes( oldText, newText ) {
// Find the first difference between texts.
const firstIndex = findFirstDifferenceIndex( oldText, newText );
// @returns {Number} return.firstIndex Index of the first change in both values (always the same for both).
// @returns {Number} result.lastIndexOld Index of the last common value in `arr1`.
// @returns {Number} result.lastIndexNew Index of the last common value in `arr2`.
function findChangeBoundaryIndexes( arr1, arr2, cmp ) {
// Find the first difference between passed values.
const firstIndex = findFirstDifferenceIndex( arr1, arr2, cmp );
// Remove the common part of texts and reverse them to make it simpler to find the last difference between texts.
const oldTextReversed = cutAndReverse( oldText, firstIndex );
const newTextReversed = cutAndReverse( newText, firstIndex );
// If arrays are equal return -1 indexes object.
if ( firstIndex === -1 ) {
return { firstIndex: -1, lastIndexOld: -1, lastIndexNew: -1 };
}
// Find the first difference between reversed texts.
// It should be treated as "how many characters from the end the last difference occurred".
// Remove the common part of each value and reverse them to make it simpler to find the last difference between them.
const oldArrayReversed = cutAndReverse( arr1, firstIndex );
const newArrayReversed = cutAndReverse( arr2, firstIndex );
// Find the first difference between reversed values.
// It should be treated as "how many elements from the end the last difference occurred".
//
// For example:
//
// initial -> after cut -> reversed:
// oldText: '321ba' -> '21ba' -> 'ab12'
// newText: '31xba' -> '1xba' -> 'abx1'
// lastIndex: -> 2
// initial -> after cut -> reversed:
// oldValue: '321ba' -> '21ba' -> 'ab12'
// newValue: '31xba' -> '1xba' -> 'abx1'
// lastIndex: -> 2
//
// So the last change occurred two characters from the end of the texts.
const lastIndex = findFirstDifferenceIndex( oldTextReversed, newTextReversed );
// So the last change occurred two characters from the end of the arrays.
const lastIndex = findFirstDifferenceIndex( oldArrayReversed, newArrayReversed, cmp );
// Use `lastIndex` to calculate proper offset, starting from the beginning (`lastIndex` kind of starts from the end).
const lastIndexOld = oldText.length - lastIndex;
const lastIndexNew = newText.length - lastIndex;
const lastIndexOld = arr1.length - lastIndex;
const lastIndexNew = arr2.length - lastIndex;

@@ -110,24 +171,25 @@ return { firstIndex, lastIndexOld, lastIndexNew };

// Returns a first index on which `oldText` and `newText` differ.
// Returns a first index on which given arrays differ. If both arrays are the same, -1 is returned.
//
// @param {String} oldText
// @param {String} newText
// @param {Array} arr1
// @param {Array} arr2
// @param {Function} cmp Comparator function.
// @returns {Number}
function findFirstDifferenceIndex( oldText, newText ) {
for ( let i = 0; i < Math.max( oldText.length, newText.length ); i++ ) {
if ( oldText[ i ] !== newText[ i ] ) {
function findFirstDifferenceIndex( arr1, arr2, cmp ) {
for ( let i = 0; i < Math.max( arr1.length, arr2.length ); i++ ) {
if ( arr1[ i ] === undefined || arr2[ i ] === undefined || !cmp( arr1[ i ], arr2[ i ] ) ) {
return i;
}
}
// No "backup" return cause we assume that `oldText` and `newText` differ. This means that they either have a
// difference or they have a different lengths. This means that the `if` condition will always be met eventually.
return -1; // Return -1 if arrays are equal.
}
// Removes `howMany` characters from the given `text` string starting from the beginning, then reverses and returns it.
// Returns a copy of the given array with `howMany` elements removed starting from the beginning and in reversed order.
//
// @param {String} text Text to be processed.
// @param {Number} howMany How many characters from text beginning to cut.
// @returns {String} Shortened and reversed text.
function cutAndReverse( text, howMany ) {
return text.substring( howMany ).split( '' ).reverse().join( '' );
// @param {Array} arr Array to be processed.
// @param {Number} howMany How many elements from array beginning to remove.
// @returns {Array} Shortened and reversed array.
function cutAndReverse( arr, howMany ) {
return arr.slice( howMany ).reverse();
}

@@ -138,6 +200,6 @@

//
// @param {String} newText New text for which change indexes were calculated.
// @param {Array} newArray New array for which change indexes were calculated.
// @param {Object} changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
// @returns {Array.<Object>} Array of changes compatible with {@link module:utils/difftochanges~diffToChanges} format.
function changeIndexesToChanges( newText, changeIndexes ) {
function changeIndexesToChanges( newArray, changeIndexes ) {
const result = [];

@@ -153,3 +215,3 @@ const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;

type: 'insert',
values: newText.substring( firstIndex, lastIndexNew ).split( '' )
values: newArray.slice( firstIndex, lastIndexNew )
} );

@@ -168,1 +230,34 @@ }

}
// Generates array with set `equal|insert|delete` operations based on change indexes from `findChangeBoundaryIndexes` function.
//
// @param {Object} changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
// @param {Number} newLength Length of the new array on which `findChangeBoundaryIndexes` calculated change indexes.
// @returns {Array.<String>} Array of changes compatible with {@link module:utils/diff~diff} format.
function changeIndexesToAtomicChanges( changeIndexes, newLength ) {
const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
// No changes.
if ( firstIndex === -1 ) {
return Array( newLength ).fill( 'equal' );
}
let result = [];
if ( firstIndex > 0 ) {
result = result.concat( Array( firstIndex ).fill( 'equal' ) );
}
if ( lastIndexNew - firstIndex > 0 ) {
result = result.concat( Array( lastIndexNew - firstIndex ).fill( 'insert' ) );
}
if ( lastIndexOld - firstIndex > 0 ) {
result = result.concat( Array( lastIndexOld - firstIndex ).fill( 'delete' ) );
}
if ( lastIndexNew < newLength ) {
result = result.concat( Array( newLength - lastIndexNew ).fill( 'equal' ) );
}
return result;
}
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -98,2 +98,11 @@ */

/**
* Destroys the focus tracker by:
* - Disabling all event listeners attached to tracked elements.
* - Removing all tracked elements that were previously added.
*/
destroy() {
this.stopListening();
}
/**
* Stores currently focused element and set {#isFocused} as `true`.

@@ -100,0 +109,0 @@ *

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.

@@ -4,0 +4,0 @@ */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc