@@ -34,18 +34,16 @@ var nextGraphemeClusterBreak = require( '@stdlib/string/next-grapheme-cluster-br
34
34
* @param {string } str - input string
35
35
* @param {Function } clbk - function to invoke
36
36
* @param {* } [thisArg] - execution context
37
- * @throws {TypeError } first argument must be a string primitive
37
+ * @throws {TypeError } first argument must be a string
38
38
* @throws {TypeError } second argument must be a function
39
39
* @returns {string } input string
40
40
*
41
41
* @example
42
- * function log( value, index, str ) {
43
- * console.log( '%s : %d ', index, value );
42
+ * function log( value, index ) {
43
+ * console.log( '%d : %s ', index, value );
44
44
* }
45
45
*
46
- * var str = 'Hello';
47
- *
48
- * forEach( str, log );
46
+ * forEach( 'Hello', log );
49
47
*/
50
48
function forEach ( str , clbk , thisArg ) {
51
49
var len ;
@@ -57,25 +55,14 @@ function forEach( str, clbk, thisArg ) {
57
55
if ( ! isFunction ( clbk ) ) {
58
56
throw new TypeError ( format ( 'invalid argument. Second argument must be a function. Value: `%s`.' , clbk ) ) ;
59
57
}
60
- if ( str === '' ) {
61
- return '' ;
62
- }
63
-
64
- idx = 0 ;
65
58
len = str . length ;
59
+ idx = 0 ;
66
60
while ( idx < len ) {
67
- // Find out where the next grapheme cluster begins:
68
61
brk = nextGraphemeClusterBreak ( str , idx ) ;
69
-
70
- // -1 only when end of string is reached...
71
- if ( brk === - 1 ) {
62
+ if ( brk === - 1 ) {
72
63
brk = len ;
73
64
}
74
-
75
- // Invoke the clbk on the cluster:
76
- clbk . call ( thisArg , str . substring ( idx , brk ) , idx , str ) ;
77
-
78
- // Hop onto the beginning of the next cluster:
65
+ clbk . call ( thisArg , str . substring ( idx , brk ) , idx , str ) ;
79
66
idx = brk ;
80
67
}
81
68
return str ;
0 commit comments