do_while_each.js (2889B)
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 isCollection = require( '@stdlib/assert/is-collection' ); 24 var isFunction = require( '@stdlib/assert/is-function' ); 25 26 27 // MAIN // 28 29 /** 30 * While a test condition is true, invokes a function once for each element in a collection. 31 * 32 * ## Notes 33 * 34 * - The condition is evaluated **after** executing the function to invoke; thus, the provided function **always** executes at least once. 35 * - If provided an empty collection, the function invokes the provided function with the collection index set to `undefined`. 36 * 37 * 38 * @param {Collection} collection - input collection 39 * @param {Function} fcn - function to invoke 40 * @param {Function} predicate - function which indicates whether to continue iterating over a collection 41 * @param {*} [thisArg] - execution context for the applied function 42 * @throws {TypeError} first argument must be a collection 43 * @throws {TypeError} second argument must be a function 44 * @throws {TypeError} third argument must be a function 45 * @returns {Collection} input collection 46 * 47 * @example 48 * function predicate( v, index, collection ) { 49 * return ( v === v ); 50 * } 51 * 52 * function log( v, index, collection ) { 53 * console.log( '%s: %d', index, v ); 54 * } 55 * 56 * var arr = [ 1, 2, 3, 4, NaN, 5 ]; 57 * 58 * doWhileEach( arr, log, predicate ); 59 */ 60 function doWhileEach( collection, fcn, predicate, thisArg ) { 61 var len; 62 var i; 63 if ( !isCollection( collection ) ) { 64 throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' ); 65 } 66 if ( !isFunction( fcn ) ) { 67 throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+fcn+'`.' ); 68 } 69 if ( !isFunction( predicate ) ) { 70 throw new TypeError( 'invalid argument. Third argument must be a function. Value: `'+predicate+'`.' ); 71 } 72 len = collection.length; 73 if ( len === 0 ) { 74 fcn.call( thisArg, void 0, void 0, collection ); 75 len = collection.length; 76 if ( len === 0 ) { 77 return collection; 78 } 79 } 80 i = 0; 81 do { 82 fcn.call( thisArg, collection[ i ], i, collection ); 83 i += 1; 84 len = collection.length; // ...account for dynamically resizing a collection 85 } while ( 86 i < len && 87 predicate( collection[ i-1 ], i-1, collection ) 88 ); 89 return collection; 90 } 91 92 93 // EXPORTS // 94 95 module.exports = doWhileEach;