Browse Source

Implemented normalizeToolbarConfig() utility.

Aleksander Nowodzinski 8 years ago
parent
commit
cfc25c3c4a

+ 35 - 0
packages/ckeditor5-ui/src/toolbar/normalizetoolbarconfig.js

@@ -0,0 +1,35 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module ui/toolbar/normalizetoolbarconfig
+ */
+
+/**
+ * Normalizes the toolbar configuration (`config.toolbar`), which may be defined as an `Array`
+ *
+ * 		toolbar: [ 'headings', 'bold', 'italic', 'link', 'unlink', ... ]
+ *
+ * or an `Object`
+ *
+ *		toolbar: {
+ *			items: [ 'headings', 'bold', 'italic', 'link', 'unlink', ... ],
+ *			...
+ *		}
+ *
+ * and returns it in the object form.
+ *
+ * @param {Array|Object} config The value of `config.toolbar`.
+ * @returns {Object} A normalized toolbar config object.
+ */
+export default function normalizeToolbarConfig( config ) {
+	if ( config instanceof Array ) {
+		config = {
+			items: config
+		};
+	}
+
+	return config;
+}

+ 28 - 0
packages/ckeditor5-ui/tests/toolbar/normalizetoolbarconfig.js

@@ -0,0 +1,28 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import normalizeToolbarConfig from '../../src/toolbar/normalizeToolbarConfig';
+
+describe( 'normalizeToolbarConfig()', () => {
+	it( 'normalizes the config specified as an Array', () => {
+		const cfg = [ 'foo', 'bar' ];
+		const normalized = normalizeToolbarConfig( cfg );
+
+		expect( normalized ).to.be.an( 'object' );
+		expect( normalized.items ).to.equal( cfg );
+	} );
+
+	it( 'passes through an already normalized config', () => {
+		const cfg = {
+			items: [ 'foo', 'bar' ],
+			foo: 'bar'
+		};
+		const normalized = normalizeToolbarConfig( cfg );
+
+		expect( normalized ).to.equal( cfg );
+		expect( normalized.items ).to.equal( cfg.items );
+		expect( normalized.foo ).to.equal( cfg.foo );
+	} );
+} );