time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

while_each.js (2442B)


      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 * @param {Collection} collection - input collection
     33 * @param {Function} predicate - function which indicates whether to continue iterating over a collection
     34 * @param {Function} fcn - function to invoke
     35 * @param {*} [thisArg] - execution context for the applied function
     36 * @throws {TypeError} first argument must be a collection
     37 * @throws {TypeError} second argument must be a function
     38 * @throws {TypeError} third argument must be a function
     39 * @returns {Collection} input collection
     40 *
     41 * @example
     42 * function predicate( v, index, collection ) {
     43 *     return ( v === v );
     44 * }
     45 *
     46 * function log( v, index, collection ) {
     47 *     console.log( '%s: %d', index, v );
     48 * }
     49 *
     50 * var arr = [ 1, 2, 3, 4, NaN, 5 ];
     51 *
     52 * whileEach( arr, predicate, log );
     53 */
     54 function whileEach( collection, predicate, fcn, thisArg ) {
     55 	var len;
     56 	var i;
     57 	if ( !isCollection( collection ) ) {
     58 		throw new TypeError( 'invalid argument. First argument must be a collection. Value: `'+collection+'`.' );
     59 	}
     60 	if ( !isFunction( predicate ) ) {
     61 		throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+predicate+'`.' );
     62 	}
     63 	if ( !isFunction( fcn ) ) {
     64 		throw new TypeError( 'invalid argument. Third argument must be a function. Value: `'+fcn+'`.' );
     65 	}
     66 	len = collection.length;
     67 	i = 0;
     68 	while (
     69 		i < len &&
     70 		predicate( collection[ i ], i, collection )
     71 	) {
     72 		fcn.call( thisArg, collection[ i ], i, collection );
     73 		i += 1;
     74 		len = collection.length; // ...account for dynamically resizing a collection
     75 	}
     76 	return collection;
     77 }
     78 
     79 
     80 // EXPORTS //
     81 
     82 module.exports = whileEach;