Browse Source

Merge pull request #11 from ckeditor/t/10

Added count() method to utils.js.
Piotr Jasiun 9 years ago
parent
commit
25ca236800

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

@@ -176,6 +176,25 @@ const utils = {
 	},
 
 	/**
+	 * Returns the number of items return by the iterator.
+	 *
+	 *		utils.count( [ 1, 2, 3, 4, 5 ] ); // 5;
+	 *
+	 * @memberOf utils.utils
+	 * @param {Iterable.<*>} iterator Any iterator.
+	 * @returns {Number} Number of items returned by that iterator.
+	 */
+	count( iterator ) {
+		let count = 0;
+
+		for ( let _ of iterator ) { // jshint ignore:line
+			count++;
+		}
+
+		return count;
+	},
+
+	/**
 	 * Copies enumerable properties and symbols from the objects given as 2nd+ parameters to the
 	 * prototype of first object (a constructor).
 	 *

+ 0 - 18
packages/ckeditor5-utils/tests/_utils/utils.js

@@ -11,24 +11,6 @@ import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
 
 const utils = {
 	/**
-	 * Returns the number of elements return by the iterator.
-	 *
-	 *		testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
-	 *
-	 * @param {Iterable.<*>} iterator Any iterator.
-	 * @returns {Number} Number of elements returned by that iterator.
-	 */
-	getIteratorCount( iterator ) {
-		let count = 0;
-
-		for ( let _ of iterator ) { // jshint ignore:line
-			count++;
-		}
-
-		return count;
-	},
-
-	/**
 	 * Creates an instance inheriting from {@link utils.EmitterMixin} with one additional method `observe()`.
 	 * It allows observing changes to attributes in objects being {@link utils.Observable observable}.
 	 *

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

@@ -12,13 +12,6 @@ import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
 
 testUtils.createSinonSandbox();
 
-describe( 'utilsTestUtils.getIteratorCount()', () => {
-	it( 'should returns number of editable items', () => {
-		const count = utilsTestUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] );
-		expect( count ).to.equal( 5 );
-	} );
-} );
-
 describe( 'utilsTestUtils.createObserver()', () => {
 	let observable, observable2, observer;
 

+ 11 - 5
packages/ckeditor5-utils/tests/utils.js

@@ -6,9 +6,8 @@
 'use strict';
 
 import utils from '/ckeditor5/utils/utils.js';
-import utilsTestUtils from '/tests/utils/_utils/utils.js';
 
-const getIteratorCount = utilsTestUtils.getIteratorCount;
+const count = utils.count;
 
 describe( 'utils', () => {
 	describe( 'spy', () => {
@@ -123,7 +122,7 @@ describe( 'utils', () => {
 		it( 'should create map from object', () => {
 			const map = utils.toMap( { foo: 1, bar: 2 } );
 
-			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( count( map ) ).to.equal( 2 );
 			expect( map.get( 'foo' ) ).to.equal( 1 );
 			expect( map.get( 'bar' ) ).to.equal( 2 );
 		} );
@@ -131,7 +130,7 @@ describe( 'utils', () => {
 		it( 'should create map from iterator', () => {
 			const map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
 
-			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( count( map ) ).to.equal( 2 );
 			expect( map.get( 'foo' ) ).to.equal( 1 );
 			expect( map.get( 'bar' ) ).to.equal( 2 );
 		} );
@@ -141,7 +140,7 @@ describe( 'utils', () => {
 
 			const map = utils.toMap( data );
 
-			expect( getIteratorCount( map ) ).to.equal( 2 );
+			expect( count( map ) ).to.equal( 2 );
 			expect( map.get( 'foo' ) ).to.equal( 1 );
 			expect( map.get( 'bar' ) ).to.equal( 2 );
 		} );
@@ -190,6 +189,13 @@ describe( 'utils', () => {
 		}
 	} );
 
+	describe( 'count', () => {
+		it( 'should returns number of editable items', () => {
+			const count = utils.count( [ 1, 2, 3, 4, 5 ] );
+			expect( count ).to.equal( 5 );
+		} );
+	} );
+
 	describe( 'mix', () => {
 		const MixinA = {
 			a() {