time-to-botec

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

arraylikefcn.js (2032B)


      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 isArrayLike = require( './../../../is-array-like' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Returns a function which tests if every element in an array-like object passes a test condition.
     30 *
     31 * @param {Function} predicate - function to apply
     32 * @throws {TypeError} must provide a function
     33 * @returns {Function} an array-like object function
     34 *
     35 * @example
     36 * var isOdd = require( '@stdlib/assert/is-odd' );
     37 *
     38 * var arr1 = [ 1, 3, 5, 7 ];
     39 * var arr2 = [ 3, 5, 8 ];
     40 *
     41 * var validate = arraylikefcn( isOdd );
     42 *
     43 * var bool = validate( arr1 );
     44 * // returns true
     45 *
     46 * bool = validate( arr2 );
     47 * // returns false
     48 */
     49 function arraylikefcn( predicate ) {
     50 	if ( typeof predicate !== 'function' ) {
     51 		throw new TypeError( 'invalid argument. Must provide a function. Value: `' + predicate + '`.' );
     52 	}
     53 	return every;
     54 
     55 	/**
     56 	* Tests if every element in an array-like object passes a test condition.
     57 	*
     58 	* @private
     59 	* @param {*} value - value to test
     60 	* @returns {boolean} boolean indicating whether a value is an array-like object for which all elements pass a test condition
     61 	*/
     62 	function every( value ) {
     63 		var len;
     64 		var i;
     65 		if ( !isArrayLike( value ) ) {
     66 			return false;
     67 		}
     68 		len = value.length;
     69 		if ( len === 0 ) {
     70 			return false;
     71 		}
     72 		for ( i = 0; i < len; i++ ) {
     73 			if ( predicate( value[ i ] ) === false ) {
     74 				return false;
     75 			}
     76 		}
     77 		return true;
     78 	}
     79 }
     80 
     81 
     82 // EXPORTS //
     83 
     84 module.exports = arraylikefcn;