You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -42,16 +42,14 @@ var forEach = require( '@stdlib/string/for-each' );
42
42
43
43
#### forEach( str, clbk\[, thisArg ] )
44
44
45
-
Invokes a `function` for each (visual) character of a `string`.
45
+
Invokes a function for each (visual) character of a string.
46
46
47
47
```javascript
48
48
functionlog( value, index ) {
49
-
console.log( '%s: %d', index, value );
49
+
console.log( '%d: %s', index, value );
50
50
}
51
51
52
-
var str ='Hello World!';
53
-
54
-
forEach( str, log );
52
+
forEach( 'Hello, world!', log );
55
53
/* =>
56
54
0: H
57
55
1: e
@@ -68,76 +66,28 @@ forEach( str, log );
68
66
*/
69
67
```
70
68
71
-
The invoked `function` is provided three arguments:
72
-
73
-
-`value`: visual character.
74
-
-`index`: starting character index.
75
-
-`str`: input string.
76
-
77
-
```javascript
78
-
functionlog1( value, index ) {
79
-
var repeatVal = value + value;
80
-
console.log( '%s: %d', index, repeatVal );
81
-
}
69
+
The invoked function is provided three arguments:
82
70
83
-
var str1 ='Hello World!';
84
-
85
-
forEach( str1, log1 );
86
-
/* =>
87
-
0: HH
88
-
1: ee
89
-
2: ll
90
-
3: ll
91
-
4: oo
92
-
5:
93
-
6: WW
94
-
7: oo
95
-
8: rr
96
-
9: ll
97
-
10: dd
98
-
11: !!
99
-
*/
100
-
101
-
functionlog2( value, index ) {
102
-
var upperCased =value.toUpperCase();
103
-
console.log( '%s: %d', index, upperCased );
104
-
}
105
-
106
-
var str2 ='hello world!';
107
-
108
-
forEach( str2, log2 );
109
-
/* =>
110
-
0: H
111
-
1: E
112
-
2: L
113
-
3: L
114
-
4: O
115
-
5:
116
-
6: W
117
-
7: O
118
-
8: R
119
-
9: L
120
-
10: D
121
-
11: !
122
-
*/
123
-
```
71
+
-`value`: visual character
72
+
-`index`: starting character index
73
+
-`str`: input string
124
74
125
75
To set the function execution context, provide a `thisArg`.
126
76
127
77
```javascript
128
-
functioncount( value) {
78
+
functionclbk() {
129
79
this.count+=1;
130
80
}
131
81
132
-
varstr3='hello world!';
82
+
varstr='Hello, world!';
133
83
134
-
varcontext= {
84
+
varctx= {
135
85
'count':0
136
86
};
137
87
138
-
forEach( str3, count, context );
88
+
forEach( str, clbk, ctx );
139
89
140
-
var bool =str3.length===context.count;
90
+
var bool =( str.length===ctx.count );
141
91
// returns true
142
92
```
143
93
@@ -149,27 +99,6 @@ var bool = str3.length === context.count;
149
99
150
100
<sectionclass="notes">
151
101
152
-
## Notes
153
-
154
-
- The function differs from traditional string iterations in the following ways:
155
-
156
-
- The function returns the input `str`.
157
-
158
-
- The `clbk` is executed on each _visual_ character as opposed to being executed on each character.
159
-
160
-
```javascript
161
-
functionlog( value, index ) {
162
-
console.log( '%s: %s', index, value );
163
-
}
164
-
165
-
var str ='\uD834\uDD1E';
166
-
167
-
forEach( str, log );
168
-
/* =>
169
-
0: 𝄞
170
-
*/
171
-
```
172
-
173
102
</section>
174
103
175
104
<!-- /.notes -->
@@ -183,19 +112,16 @@ var bool = str3.length === context.count;
183
112
<!-- eslint no-undef: "error" -->
184
113
185
114
```javascript
186
-
var isEven = require( '@stdlib/assert/is-even' ).isPrimitive;
187
115
var forEach =require( '@stdlib/string/for-each' );
188
116
189
117
functionlog( value, index ) {
190
-
if ( isEven( index ) ) {
191
-
console.log( '%s: %d', index, '*' );
192
-
} else {
193
-
console.log( '%s: %d', index, value );
194
-
}
118
+
console.log( '%d: %s', index, value );
195
119
}
196
120
197
-
var str4 = 'Javascript';
198
-
forEach( str4, log );
121
+
forEach( 'presidential election', log );
122
+
forEach( 'Iñtërnâtiônàlizætiøn', log );
123
+
forEach( '🌷🍕', log );
124
+
forEach( '\uD834\uDD1E', log );
199
125
```
200
126
201
127
</section>
@@ -214,13 +140,6 @@ forEach( str4, log );
214
140
215
141
<sectionclass="related">
216
142
217
-
***
218
-
219
-
## See Also
220
-
221
-
-<span class="package-name">[`@stdlib/utils/for-each`][@stdlib/utils/for-each]</span><span class="delimiter">:</span><span class="description">invoke a function once for each element in a collection.</span>
222
-
- <span class="package-name">[`@stdlib/utils/async/for-each`][@stdlib/utils/async/for-each]</span><span class="delimiter">: </span><span class="description">invoke an asyncfunction once for each element in a collection.</span>
0 commit comments