main.js (3267B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isFunction = require( '@stdlib/assert/is-function' ); 24 var isCollection = require( '@stdlib/assert/is-collection' ); 25 var isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); 26 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 27 28 29 // MAIN // 30 31 /** 32 * Creates (or fills) an array from an iterator. 33 * 34 * @param {Iterator} iterator - source iterator 35 * @param {Collection} [out] - output array 36 * @param {Function} [mapFcn] - function to invoke for each iterated value 37 * @param {*} [thisArg] - execution context 38 * @throws {TypeError} iterator argument must be an iterator 39 * @throws {TypeError} callback argument must be a function 40 * @returns {Collection} output array 41 * 42 * @example 43 * var randu = require( '@stdlib/random/iter/randu' ); 44 * 45 * var iter = randu({ 46 * 'iter': 10 47 * }); 48 * 49 * var arr = iterator2array( iter ); 50 * // returns <Array> 51 */ 52 function iterator2array() { 53 var iterator; 54 var thisArg; 55 var fcn; 56 var out; 57 var len; 58 var i; 59 var v; 60 61 iterator = arguments[ 0 ]; 62 if ( arguments.length > 1 ) { 63 if ( isCollection( arguments[ 1 ] ) ) { 64 out = arguments[ 1 ]; 65 if ( arguments.length > 2 ) { 66 fcn = arguments[ 2 ]; 67 if ( !isFunction( fcn ) ) { 68 throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + fcn + '`.' ); 69 } 70 thisArg = arguments[ 3 ]; 71 } 72 } else { 73 fcn = arguments[ 1 ]; 74 if ( !isFunction( fcn ) ) { 75 throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + fcn + '`.' ); 76 } 77 thisArg = arguments[ 2 ]; 78 } 79 } 80 if ( !isIteratorLike( iterator ) ) { 81 throw new TypeError( 'invalid argument. Iterator argument must be an iterator protocol-compliant object. Value: `' + iterator + '`.' ); 82 } 83 i = -1; 84 if ( out === void 0 ) { 85 out = []; 86 if ( fcn ) { 87 while ( true ) { 88 i += 1; 89 v = iterator.next(); 90 if ( hasOwnProp( v, 'value' ) ) { 91 out.push( fcn.call( thisArg, v.value, i ) ); 92 } 93 if ( v.done ) { 94 break; 95 } 96 } 97 return out; 98 } 99 while ( true ) { 100 v = iterator.next(); 101 if ( hasOwnProp( v, 'value' ) ) { 102 out.push( v.value ); 103 } 104 if ( v.done ) { 105 break; 106 } 107 } 108 return out; 109 } 110 len = out.length; 111 if ( fcn ) { 112 while ( i < len-1 ) { 113 i += 1; 114 v = iterator.next(); 115 if ( hasOwnProp( v, 'value' ) ) { 116 out[ i ] = fcn.call( thisArg, v.value, i ); 117 } 118 if ( v.done ) { 119 break; 120 } 121 } 122 return out; 123 } 124 while ( i < len-1 ) { 125 i += 1; 126 v = iterator.next(); 127 if ( hasOwnProp( v, 'value' ) ) { 128 out[ i ] = v.value; 129 } 130 if ( v.done ) { 131 break; 132 } 133 } 134 return out; 135 } 136 137 138 // EXPORTS // 139 140 module.exports = iterator2array;