瀏覽代碼

Added count() method to utils.js.

Szymon Kupś 9 年之前
父節點
當前提交
e882325152
共有 1 個文件被更改,包括 18 次插入0 次删除
  1. 18 0
      packages/ckeditor5-utils/src/utils.js

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

@@ -176,6 +176,24 @@ const utils = {
 	},
 
 	/**
+	 * Returns the number of items return by the iterator.
+	 *
+	 *		utils.count( [ 1, 2, 3, 4, 5 ] ); // 5;
+	 *
+	 * @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).
 	 *