compute-cprod
v2.0.0
Published
Computes the cumulative product of an array.
Downloads
939
Readme
Cumulative Product
Computes the cumulative product of an array.
Installation
$ npm install compute-cprod
For use in the browser, use browserify.
Usage
var cprod = require( 'compute-cprod' );
cprod( arr[, options] )
Computes the cumulative product of an array
. For primitive arrays
,
var data = [ 1, 2, 3, 4 ];
var arr = cprod( data );
// returns [ 1, 2, 6, 24 ]
The function accepts two options
:
- copy:
boolean
indicating whether to return a newarray
containing the cumulative products. Default:true
. - accessor: accessor
function
for accessing numeric values in objectarrays
.
To mutate the input array
(e.g., when input values can be discarded or when optimizing memory usage), set the copy
option to false
.
var data = [ 1, 2, 3, 4 ];
var arr = cprod( data, {
'copy': false
});
// returns [ 1, 2, 6, 24 ]
console.log( data === arr );
// returns true
For object arrays
, provide an accessor function
for accessing numeric array
values.
var data = [
['beep', 1],
['boop', 2],
['bap', 3],
['baz', 4]
];
function getValue( d, i ) {
return d[ 1 ];
}
var arr = cprod( arr, {
'accessor': getValue
});
// returns [ 1, 2, 6, 24 ]
Note: the function returns an array
with a length equal to the original input array
.
Examples
var cprod = require( 'compute-cprod' );
var data = new Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 10 ) + 1;
}
console.log( cprod( data ) );
// returns [...]
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. Philipp Burckhardt.