Typed Array Function
Validates if every element of a
typed array
passes a test condition.
Installation
$ npm install validate.io-typed-array-function
For use in the browser, use browserify.
Usage
var validate = ;
validate( fcn, value )
Validates if every element of a typed array
passes a test condition. Given an input typed array
, the function returns true
if all elements pass the test and false
otherwise.
var arr1 = 1 3 5 7arr2 = 3 5 6 8 ;{return x % 2 === 1;}var out = ;// returns truevar out = ;// returns false
===
Create
To facilitate using typed array
validation functions within an application, a method to create minimal typed array
validation functions is provided.
validate.create( fcn )
Creates a validation function
which validates whether every element of a typed array
passes a test condition.
var isOddTypedArray = validateout;out = ;// returns trueout = ;// returns false{return x % 2 === 1;}
===
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 a typed array
passes a test condition. Given an input typed array
, the function returns true
if all elements pass the test and false
otherwise.
var arr = 1 1 1 1 1 ;var out = validate;// returns true{return x % 2 === 1;}
Notes
- A provided test function should accept a single argument: a
typed array
element. If thetyped array
element satisfies a test condition, the function should returntrue
; otherwise, the function should returnfalse
. - The validation functions will return
false
for an emptytyped 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 validateTypedArray = ;{return x % 2 === 0;}var arr1 arr2outi;arr1 = 25 ;for i = 0; i < arr1length; i++arr1 i = i;arr2 = 25 ;for i = 0; i < arr2length; i++arr2 i = 2 * i;out = ;console;out = ;console;
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
Copyright
Copyright © 2015. The Compute.io Authors.