Test if every element of an array-like object passes a test condition.
var validate = require( '@stdlib/tools/array-like-function' );
Tests if every element of an array
-like object passes a test condition. Given an input array
-like object, the function returns true
if all elements pass the test and false
otherwise.
var isOdd = require( '@stdlib/assert/is-odd' );
var arr1 = [ 1, 3, 5, 7 ];
var arr2 = [ 3, 5, 'c' ];
var bool = validate( isOdd, arr1 );
// returns true
bool = validate( isOdd, arr2 );
// returns false
To facilitate using array
-like object validation functions within an application, a method to create minimal array
-like object validation functions is provided.
Creates a validation function
which validates whether every element of an array
-like object passes a test condition.
var isOdd = require( '@stdlib/assert/is-odd' );
var isOddArray = validate.create( isOdd );
var bool = isOddArray( [ 1, 3, 5 ] );
// returns true
bool = isOddArray( [ 2, 3, 4 ] );
// returns false
A lower-level API is provided which forgoes some of the guarantees of the above APIs, such as input argument validation. While use of the above APIs is encouraged in REPL environments, use of the lower-level interface may be warranted when arguments are of a known type or when performance is paramount.
Validates if every element of an array
-like object passes a test condition. Given an input array
-like object, the function returns true
if all elements pass the test and false
otherwise.
var isOdd = require( '@stdlib/assert/is-odd' );
var arr = [ 1, 1, 1, 1, 1 ];
var bool = validate.raw( isOdd, arr );
// returns true
- A provided test function should accept a single argument: an
array
-like object element. If thearray
-like object element satisfies a test condition, the function should returntrue
; otherwise, the function should returnfalse
. - The validation functions will return
false
for an emptyarray
-like object. - The
.create()
method uses dynamic code evaluation. Beware when using it in the browser as it may violate your content security policy (CSP).
var isEven = require( '@stdlib/assert/is-even' );
var validateArray = require( '@stdlib/tools/array-like-function' );
var arr1;
var arr2;
var bool;
var i;
arr1 = new Array( 25 );
for ( i = 0; i < arr1.length; i++ ) {
arr1[ i ] = i;
}
arr2 = new Array( 25 );
for ( i = 0; i < arr2.length; i++ ) {
arr2[ i ] = 2 * i;
}
bool = validateArray( isEven, arr1 );
// returns false
bool = validateArray( isEven, arr2 );
// returns true