compute-eq v1.0.0
Equal
Computes an element-wise comparison (equality) of an array.
Installation
$ npm install compute-eqFor use in the browser, use browserify.
Usage
To use the module,
var eq = require( 'compute-eq' );eq( arr, x, opts )
Computes an element-wise comparison (equality) for each input array element. x may either be an array of equal length or a single value (of any type).
The function returns an array with length equal to that of the input array. Each output array element is either 0 or 1. A value of 1 means that an element is equal to a compared value and 0 means that an element is not equal to a compared value.
var arr = [ 5, 3, 8, 3, 2 ],
out;
// Single comparison value:
out = eq( arr, 3 );
// returns [ 0, 1, 0, 1, 0 ]
// Array of comparison values:
out = eq( arr, [ 5, 2, 8, 7, 3 ] );
// returns [ 1, 0, 1, 0, 0 ]By default, the function enforces strict equality. To turn off type equality, set the strict options flag to false.
var arr = [ 1, 0, 3, null, undefined ],
compare = [ true, false, 2, 0, 1 ],
out;
out = eq( arr, compare );
// returns [ 0, 0, 0, 0, 0 ];
out = eq( arr, compare, {'strict': false} );
// returns [ 1, 1, 0, 0, 0 ] // 0 != nullBy default, if provided a comparison array which has a length equal to the input array length, the function assumes an element-by-element comparison. To turn off element-by-element comparison for equal length arrays, set the array options flag to true.
var el = [ 1, 2 ],
arr = [ el, null ],
out;
out = eq( arr, el );
// returns [ 0, 0 ];
out = eq( arr, el, {'array': true} );
// returns [ 1, 0 ];Notes
- Currently, this function only computes shallow equality and does not compute deep equality when comparing
arraysorobjects.
Examples
var eq = require( 'compute-eq' ),
sum = require( 'compute-sum' );
// Simulate some data...
var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random()*20 );
}
var out = eq( data, 10 );
// Count the number of values equal to 10...
var count = sum( out );
console.log( 'Total: %d', count );To run the example code from the top-level application directory,
$ node ./examples/index.jsTests
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 testAll 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-covIstanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-covLicense
Copyright
Copyright © 2014. Athan Reines.