Sfoglia il codice sorgente

Introduced objectToMap and mapToObject.

Piotr Jasiun 10 anni fa
parent
commit
aef390899b

+ 18 - 0
packages/ckeditor5-utils/src/utils.js

@@ -91,6 +91,24 @@ const utils = {
 		}
 	},
 
+	objectToMap( obj ) {
+		const map = new Map();
+
+		for ( let key in obj ) {
+			map.set( key, obj[ key ] );
+		}
+
+		return map;
+	},
+
+	mapToObject( map ) {
+		const obj = {};
+
+		map.forEach( ( value, key ) => obj[ key ] = value );
+
+		return obj;
+	},
+
 	/**
 	 * Returns `nth` (starts from `0` of course) item of an `iterable`.
 	 *

+ 19 - 0
packages/ckeditor5-utils/tests/utils.js

@@ -116,6 +116,25 @@ describe( 'utils', () => {
 		} );
 	} );
 
+	describe( 'objectToMap', () => {
+		it( 'should convert object to map', () => {
+			const map = utils.objectToMap( { 'foo': 1, 'bar': 2 } );
+
+			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( map.get( 'foo' ) ).to.equal( 1 );
+			expect( map.get( 'bar' ) ).to.equal( 2 );
+		} );
+	} );
+
+	describe( 'mapToObject', () => {
+		it( 'should convert map to object', () => {
+			const map = new Map( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
+			const obj = utils.mapToObject( map );
+
+			expect( obj ).to.deep.equal( { 'foo': 1, 'bar': 2 } );
+		} );
+	} );
+
 	describe( 'nth', () => {
 		it( 'should return 0th item', () => {
 			expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );