浏览代码

Introduced Utils.toMap.

Piotr Jasiun 10 年之前
父节点
当前提交
e5555f9c3e

+ 2 - 7
packages/ckeditor5-engine/src/treemodel/element.js

@@ -7,7 +7,6 @@
 
 import Node from './node.js';
 import NodeList from './nodelist.js';
-import langUtils from '../lib/lodash/lang.js';
 import utils from '../utils.js';
 
 /**
@@ -131,14 +130,10 @@ export default class Element extends Node {
 	/**
 	 * Removes all attributes from the element and sets given attributes.
 	 *
-	 * @param {Iterable.<*>} attrs Iterable object containing attributes to be set. See {@link treeModel.Node#getAttributes}.
+	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set. See {@link treeModel.Node#getAttributes}.
 	 */
 	setAttributesTo( attrs ) {
-		if ( langUtils.isPlainObject( attrs ) ) {
-			this._attrs = utils.objectToMap( attrs );
-		} else {
-			this._attrs = new Map( attrs );
-		}
+		this._attrs = utils.toMap( attrs );
 	}
 
 	/**

+ 2 - 6
packages/ckeditor5-engine/src/treemodel/node.js

@@ -21,7 +21,7 @@ export default class Node {
 	 *
 	 * This is an abstract class, so this constructor should not be used directly.
 	 *
-	 * @param {Iterable} attrs Iterable collection of attributes.
+	 * @param {Iterable|Object} attrs Iterable collection of attributes.
 	 * @constructor
 	 */
 	constructor( attrs ) {
@@ -42,11 +42,7 @@ export default class Node {
 		 * @protected
 		 * @property {Map} _attrs
 		 */
-		if ( langUtils.isPlainObject( attrs ) ) {
-			this._attrs = utils.objectToMap( attrs );
-		} else {
-			this._attrs = new Map( attrs );
-		}
+		this._attrs = utils.toMap( attrs );
 	}
 
 	/**

+ 2 - 7
packages/ckeditor5-engine/src/treemodel/selection.js

@@ -8,7 +8,6 @@
 import LiveRange from './liverange.js';
 import EmitterMixin from '../emittermixin.js';
 import CKEditorError from '../ckeditorerror.js';
-import langUtils from '../lib/lodash/lang.js';
 import utils from '../utils.js';
 
 /**
@@ -210,14 +209,10 @@ export default class Selection {
 	/**
 	 * Removes all attributes from the selection and sets given attributes.
 	 *
-	 * @param {Iterable.<*>} attrs Iterable object containing attributes to be set.
+	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
 	 */
 	setAttributesTo( attrs ) {
-		if ( langUtils.isPlainObject( attrs ) ) {
-			this._attrs = utils.objectToMap( attrs );
-		} else {
-			this._attrs = new Map( attrs );
-		}
+		this._attrs = utils.toMap( attrs );
 	}
 
 	/**

+ 2 - 7
packages/ckeditor5-engine/src/treemodel/text.js

@@ -5,7 +5,6 @@
 
 'use strict';
 
-import langUtils from '../lib/lodash/lang.js';
 import utils from '../utils.js';
 
 /**
@@ -21,7 +20,7 @@ export default class Text {
 	 * Creates a text with attributes.
 	 *
 	 * @param {String} text Described text.
-	 * @param {Iterable} attrs Iterable collection of attributes.
+	 * @param {Iterable|Object} attrs Iterable collection of attributes.
 	 * @constructor
 	 */
 	constructor( text, attrs ) {
@@ -39,10 +38,6 @@ export default class Text {
 		 * @protected
 		 * @property {Map}
 		 */
-		if ( langUtils.isPlainObject( attrs ) ) {
-			this._attrs = utils.objectToMap( attrs );
-		} else {
-			this._attrs = new Map( attrs );
-		}
+		this._attrs = utils.toMap( attrs );
 	}
 }

+ 21 - 1
packages/ckeditor5-engine/src/utils.js

@@ -5,6 +5,8 @@
 
 'use strict';
 
+import langUtils from './lib/lodash/lang.js';
+
 /**
  * An index at which arrays differ. If arrays are same at all indexes, it represents how arrays are related.
  * In this case, possible values are: 'SAME', 'PREFIX' or 'EXTENSION'.
@@ -92,7 +94,7 @@ const utils = {
 	},
 
 	/**
-	 * Transform object to map.
+	 * Transforms object to map.
 	 *
 	 *		const map = utils.objectToMap( { 'foo': 1, 'bar': 2 } );
 	 *		map.get( 'foo' ); // 1
@@ -111,6 +113,24 @@ const utils = {
 	},
 
 	/**
+	 * Transforms object or iterable to map. Iterable needs to be in the format acceptable by the `Map` constructor.
+	 *
+	 *		map = utils.toMap( { 'foo': 1, 'bar': 2 } );
+	 *		map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
+	 *		map = utils.toMap( anotherMap );
+	 *
+	 * @param {Object|Iterable} data Object or iterable to transform.
+	 * @returns {Map} Map created from data.
+	 */
+	toMap( data ) {
+		if ( langUtils.isPlainObject( data ) ) {
+			return utils.objectToMap( data );
+		} else {
+			return new Map( data );
+		}
+	},
+
+	/**
 	 * Checks whether given {Map}s are equal, that is has same size and same key-value pairs.
 	 *
 	 * @returns {Boolean} `true` if given maps are equal, `false` otherwise.

+ 31 - 0
packages/ckeditor5-engine/tests/utils.js

@@ -6,6 +6,9 @@
 'use strict';
 
 import utils from '/ckeditor5/core/utils.js';
+import coreTestUtils from '/tests/core/_utils/utils.js';
+
+const getIteratorCount = coreTestUtils.getIteratorCount;
 
 describe( 'utils', () => {
 	describe( 'spy', () => {
@@ -116,6 +119,34 @@ describe( 'utils', () => {
 		} );
 	} );
 
+	describe( 'toMap', () => {
+		it( 'should create map from object', () => {
+			const map = utils.toMap( { foo: 1, bar: 2 } );
+
+			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( map.get( 'foo' ) ).to.equal( 1 );
+			expect( map.get( 'bar' ) ).to.equal( 2 );
+		} );
+
+		it( 'should create map from iterator', () => {
+			const map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
+
+			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( map.get( 'foo' ) ).to.equal( 1 );
+			expect( map.get( 'bar' ) ).to.equal( 2 );
+		} );
+
+		it( 'should create map from another map', () => {
+			const data = new Map( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
+
+			const map = utils.toMap( data );
+
+			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( map.get( 'foo' ) ).to.equal( 1 );
+			expect( map.get( 'bar' ) ).to.equal( 2 );
+		} );
+	} );
+
 	describe( 'mapsEqual', () => {
 		it( 'should return true if maps have exactly same entries (order of adding does not matter)', () => {
 			let mapA = new Map();