1.0.0 • Published 9 years ago

validate.io-array-function v1.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
9 years ago

Array Function

NPM version Build Status Coverage Status Dependencies

Validates if every element of an array passes a test condition.

Installation

$ npm install validate.io-array-function

For use in the browser, use browserify.

Usage

var validate = require( 'validate.io-array-function' );

validate( fcn, value )

Validates if every element of an array passes a test condition. Given an input array, the function returns true if all elements pass the test and false otherwise.

var arr1 = [ 1, 3, 5, 7 ],
	arr2 = [ 3, 5, 'c' ];

function isOdd( x ) {
	return ( typeof x === 'number' && x % 2 === 1 );
}

var out = validate( isOdd, arr1 );
// returns true

var out = validate( isOdd, arr2 );
// returns false

===

Create

To facilitate using array validation functions within an application, a method to create minimal array validation functions is provided.

validate.create( fcn )

Creates a validation function which validates whether every element of an array passes a test condition.

function isOdd( x ) {
	return ( typeof x === 'number' && x % 2 === 1 );
}

var isOddArray = validate.create( isOdd ),
	out;

out = isOddArray( [1,3,5] );
// returns true

out = isOddArray( [2,3,4] );
// returns false

===

Raw

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.

validate.raw( fcn, value )

Validates if every element of an array passes a test condition. Given an input array, the function returns true if all elements pass the test and false otherwise.

var arr = [ 1, 1, 1, 1, 1 ]

var out = validate.raw( isOdd, arr );
// returns true

function isOdd( val ) {
	return ( typeof x === 'number' && val % 2 === 1 );
}

Notes

  • A provided test function should accept a single argument: an [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) element. If the [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) element satisfies a test condition, the function should return `true`; otherwise, the function should return `false`.
  • The validation functions will return false for an empty array.
  • The .create() method uses dynamic code evaluation. Beware when using it in the browser as it may violate your content security policy (CSP).

Examples

var validateArray = require( 'validate.io-array-function' );

var arr1, arr2,
	out,
	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;
}

function isEven( x ) {
	return ( typeof x === 'number' && val % 2 === 0 );
}

out = validateArray( isEven, arr1 );
console.log( out );

out = validateArray( isEven, arr2 );
console.log( out );

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2015. The Compute.io Authors.