compute-nanprod

1.0.0 • Public • Published

nanprod

NPM version Build Status Coverage Status Dependencies

Computes the product of an array ignoring any non-numeric values.

Installation

$ npm install compute-nanprod

For use in the browser, use browserify.

Usage

var nanprod = require( 'compute-nanprod' );

nanprod( arr[, accessor] )

Computes the product of an array ignoring any non-numeric values. For primitive arrays,

var arr = [ 1, NaN, 2, NaN, 1 ];
 
var value = nanprod( arr );
// returns 2

For object arrays, provide an accessor function for accessing array values

var arr = [
    [1,4],
    [NaN,1],
    [2,2],
    [NaN,0],
    [1,3]
];
 
function getValue( d ) {
    return d[ 0 ];
}
 
var value = nanprod( arr, getValue );
// returns 2

Notes

  • The product of an array containing non-numeric values is equal to the product of an equivalent array containing only the numeric values. Hence,
var arr1 = [ 1, NaN, 2, 3, NaN ],
    arr2 = [ 1, 2, 3 ];
 
console.log( nanprod( arr1 ) === nanprod( arr2 ) );
// returns true
  • If provided an empty array, the method returns null.
  • If provided an array with only non-numeric values, the method returns NaN.

Examples

var nanprod = require( 'compute-nanprod' );
 
var data = new Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
    if ( i%5 === 0 ) {
        data[ i ] = NaN;
    } else {
        data[ i ] = Math.round( Math.random()*10 ) + 1;
    }
}
 
console.log( nanprod( data ) );

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. Philipp Burckhardt.

Package Sidebar

Install

npm i compute-nanprod

Weekly Downloads

78

Version

1.0.0

License

none

Last publish

Collaborators

  • kgryte
  • planeshifter