Skip to content

Commit 700b12b

Browse files
committed
Auto-generated commit
1 parent 168f65b commit 700b12b

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ var arr = iterator2arrayview( iter, new Float64Array( 4 ), fcn );
132132

133133
The invoked function is provided three arguments:
134134

135-
- `value`: iterated value
136-
- `index`: destination index
137-
- `n`: iteration count (zero-based)
135+
- **value**: iterated value.
136+
- **index**: destination index.
137+
- **n**: iteration count (zero-based).
138138

139139
```javascript
140140
var Uint8Array = require( '@stdlib/array-uint8' );
@@ -183,6 +183,7 @@ var count = ctx.count;
183183
## Notes
184184

185185
- Iteration stops when an output array view is full **or** an iterator finishes; whichever comes first.
186+
- The function supports output array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/complex64`][@stdlib/array/complex64]).
186187

187188
</section>
188189

@@ -306,6 +307,8 @@ Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
306307

307308
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/iter-to-array-view/main/LICENSE
308309

310+
[@stdlib/array/complex64]: https://github.com/stdlib-js/array-complex64
311+
309312
<!-- <related-links> -->
310313

311314
[@stdlib/array/from-iterator]: https://github.com/stdlib-js/array-from-iterator

lib/main.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var isFunction = require( '@stdlib/assert-is-function' );
2424
var isCollection = require( '@stdlib/assert-is-collection' );
2525
var isIteratorLike = require( '@stdlib/assert-is-iterator-like' );
2626
var isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;
27+
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
2728

2829

2930
// MAIN //
@@ -61,6 +62,7 @@ function iterator2arrayview( iterator, out ) {
6162
var begin;
6263
var end;
6364
var fcn;
65+
var set;
6466
var i;
6567
var v;
6668

@@ -138,6 +140,7 @@ function iterator2arrayview( iterator, out ) {
138140
begin = 0;
139141
}
140142
}
143+
set = arraylike2object( out ).setter;
141144
i = begin - 1;
142145
if ( fcn ) {
143146
while ( i < end-1 ) {
@@ -146,7 +149,7 @@ function iterator2arrayview( iterator, out ) {
146149
if ( v.done ) {
147150
break;
148151
}
149-
out[ i ] = fcn.call( thisArg, v.value, i, i-begin );
152+
set( out, i, fcn.call( thisArg, v.value, i, i-begin ) );
150153
}
151154
return out;
152155
}
@@ -156,7 +159,7 @@ function iterator2arrayview( iterator, out ) {
156159
if ( v.done ) {
157160
break;
158161
}
159-
out[ i ] = v.value;
162+
set( out, i, v.value );
160163
}
161164
return out;
162165
}

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,22 @@
3737
"url": "https://github.com/stdlib-js/stdlib/issues"
3838
},
3939
"dependencies": {
40+
"@stdlib/array-base-arraylike2object": "^0.0.x",
4041
"@stdlib/assert-is-collection": "^0.0.x",
4142
"@stdlib/assert-is-function": "^0.0.x",
4243
"@stdlib/assert-is-integer": "^0.0.x",
4344
"@stdlib/assert-is-iterator-like": "^0.0.x",
4445
"@stdlib/types": "^0.0.x"
4546
},
4647
"devDependencies": {
48+
"@stdlib/array-complex128": "^0.0.x",
4749
"@stdlib/array-float64": "^0.0.x",
4850
"@stdlib/array-to-iterator": "^0.0.x",
4951
"@stdlib/bench": "^0.0.x",
52+
"@stdlib/complex-float64": "^0.0.x",
5053
"@stdlib/math-base-assert-is-nan": "^0.0.x",
5154
"@stdlib/random-iter-randu": "^0.0.x",
55+
"@stdlib/strided-base-reinterpret-complex128": "^0.0.x",
5256
"@stdlib/utils-noop": "^0.0.x",
5357
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
5458
"istanbul": "^0.4.1",

test/test.js

+50
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ var tape = require( 'tape' );
2424
var noop = require( '@stdlib/utils-noop' );
2525
var randu = require( '@stdlib/random-iter-randu' );
2626
var Float64Array = require( '@stdlib/array-float64' );
27+
var Complex128Array = require( '@stdlib/array-complex128' );
28+
var Complex128 = require( '@stdlib/complex-float64' );
29+
var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' );
2730
var array2iterator = require( '@stdlib/array-to-iterator' );
2831
var iterator2arrayview = require( './../lib' );
2932

@@ -509,6 +512,30 @@ tape( 'the function fills an array-like object with values returned from an iter
509512
t.end();
510513
});
511514

515+
tape( 'the function fills an array-like object with values returned from an iterator (accessors)', function test( t ) {
516+
var expected;
517+
var values;
518+
var actual;
519+
var out;
520+
var it;
521+
522+
values = [
523+
new Complex128( 1.0, 2.0 ),
524+
new Complex128( 3.0, 4.0 ),
525+
new Complex128( 5.0, 6.0 ),
526+
new Complex128( 7.0, 8.0 )
527+
];
528+
it = array2iterator( values );
529+
530+
out = new Complex128Array( values.length );
531+
actual = iterator2arrayview( it, out );
532+
expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
533+
534+
t.equal( actual, out, 'returns expected value' );
535+
t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' );
536+
t.end();
537+
});
538+
512539
tape( 'the function fills an array-like object view with values returned from an iterator (begin)', function test( t ) {
513540
var expected;
514541
var values;
@@ -722,6 +749,29 @@ tape( 'the function supports providing a callback to be invoked for each iterate
722749
}
723750
});
724751

752+
tape( 'the function supports providing a callback to be invoked for each iterated value (accessors)', function test( t ) {
753+
var expected;
754+
var values;
755+
var actual;
756+
var out;
757+
var it;
758+
759+
values = [ 1, 2, 3, 4 ];
760+
it = array2iterator( values );
761+
762+
out = new Complex128Array( values.length );
763+
actual = iterator2arrayview( it, out, clbk );
764+
expected = new Float64Array( [ 1, 0, 2, 1, 3, 2, 4, 3 ] );
765+
766+
t.equal( actual, out, 'returns expected value' );
767+
t.deepEqual( reinterpret128( actual, 0 ), expected, 'returns expected value' );
768+
t.end();
769+
770+
function clbk( v, i ) {
771+
return new Complex128( v, i );
772+
}
773+
});
774+
725775
tape( 'the function supports providing a callback to be invoked for each iterated value (begin)', function test( t ) {
726776
var expected;
727777
var values;

0 commit comments

Comments
 (0)