Skip to content

Commit

Permalink
[UPDATE] add accessor fcn. fmt. [TESTS] accessor fcn.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Feb 24, 2015
1 parent 0f632ba commit a52666d
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 44 deletions.
45 changes: 27 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cprod
Cumulative Product
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Computes the cumulative product of a numeric array
> Computes the cumulative product of an array.

## Installation
Expand All @@ -15,39 +15,53 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse

## Usage

To use the module,

``` javascript
var cprod = require( 'compute-cprod' );
```

#### cprod( arr )
#### cprod( arr[, accessor] )

Computes the cumulative product of a numeric `array`.
Computes the cumulative product of an `array`. For primitive `arrays`,

``` javascript
var data = [ 1, 2, 3, 4 ];

cprod( data );
var arr = cprod( data );
// returns [ 1, 2, 6, 24 ]
```

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

``` javascript
var data = [
['beep', 1],
['boop', 2],
['bap', 3],
['baz', 4]
];

function getValue( d ) {
return d[ 1 ];
}

var arr = cprod( arr, getValue );
// returns [ 1, 2, 6, 24 ]
```

__Note__: the function returns an `array` with a length equal to the original input `array`.



## Examples

``` javascript
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 ) ;
data[ i ] = Math.round( Math.random() * 10 ) + 1;
}

data.sort( function sort( a, b ) {
return a - b;
});

console.log( cprod( data ) );
// returns [...]
```
Expand All @@ -59,11 +73,6 @@ $ node ./examples/index.js
```


## Notes

The function returns an `array` with a length equal to the original input `array`.


## Tests

### Unit
Expand Down
8 changes: 2 additions & 6 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
'use strict';

var cprod = require( './../lib' );
var data = new Array( 10 );

var data = new Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 10 + 1 ) ;
data[ i ] = Math.round( Math.random()*10 ) + 1;
}

data.sort( function sort( a, b ) {
return a - b;
});

console.log( cprod( data ) );
// returns [...]
71 changes: 58 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*
* DESCRIPTION:
* - Computes the cumulative product of a numeric array
* - Computes the cumulative product of an array.
*
*
* NOTES:
Expand All @@ -30,33 +30,78 @@

// MODULES //

// var module_alias = require( 'module_name' );
var isArray = require( 'validate.io-array' );


// CUMULATIVE PRODUCT //

/**
* FUNCTION: cprod( arr )
* Computes the cumulative sum of a numeric array.
* FUNCTION: cprod( arr[, accessor] )
* Computes the cumulative product of an array.
*
* @param {Array} arr - numeric array
* @returns {Array} cumulative sum
* @param {Function} [accessor] - accessor function for accessing array values
* @returns {Array} cumulative product
*/

function cprod( arr ) {
if ( !Array.isArray( arr ) ) {
throw new TypeError( 'cprod()::invalid input argument. Must provide an array.' );
function cprod( arr, clbk ) {
if ( !isArray( arr ) ) {
throw new TypeError( 'cprod()::invalid input argument. Must provide an array. Value: `' + arr + '`.' );
}
if ( arguments.length > 1 ) {
if ( typeof clbk !== 'function' ) {
throw new TypeError( 'cprod()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' );
}
}
var len = arr.length,
v = new Array( len );
v = new Array( len ),
val,
flg,
i;

v[ 0 ] = arr[ 0 ];
for ( var i = 1; i < len; i++ ) {
v[ i ] = v[ i-1 ] * arr[ i ];
if ( clbk ) {
val = clbk( arr[ 0 ] );
if ( val === 0 ) {
flg = true;
}
v[ 0 ] = val;
for ( i = 1; i < len; i++ ) {
if ( flg ) {
v[ i ] = 0;
continue;
}
val = clbk( arr[ i ] );
if ( val === 0 ) {
flg = true;
v[ i ] = 0;
} else {
v[ i ] = v[ i-1 ] * val;
}
}
} else {
val = arr[ 0 ];
if ( val === 0 ) {
flg = true;
}
v[ 0 ] = val;
for ( i = 1; i < len; i++ ) {
if ( flg ) {
v[ i ] = 0;
continue;
}
val = arr[ i ];
if ( val === 0 ) {
flg = true;
v[ i ] = 0;
} else {
v[ i ] = v[ i-1 ] * val;
}
}
}
return v;
} // end FUNCTION cprod()



// EXPORTS //

module.exports = cprod;
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "compute-cprod",
"version": "0.0.0",
"description": "Computes the cumulative product of a numeric array",
"description": "Computes the cumulative product of an array.",
"author": {
"name": "Philipp Burckhardt",
"email": "pburckhardt@outlook.com"
Expand All @@ -10,6 +10,10 @@
{
"name": "Philipp Burckhardt",
"email": "pburckhardt@outlook.com"
},
{
"name": "Athan Reines",
"email": "kgryte@gmail.com"
}
],
"scripts": {
Expand All @@ -31,15 +35,20 @@
"mathematics",
"math",
"cumulative",
"product"
"product",
"prod",
"cumprod",
"array"
],
"bugs": {
"url": "https://github.com/compute-io/cprod/issues"
},
"dependencies": {},
"dependencies": {
"validate.io-array": "^1.0.3"
},
"devDependencies": {
"chai": "1.x.x",
"mocha": "1.x.x",
"chai": "2.x.x",
"mocha": "2.x.x",
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
"jshint": "2.x.x",
Expand Down
90 changes: 88 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global require, describe, it */
'use strict';

// MODULES //
Expand Down Expand Up @@ -36,7 +37,7 @@ describe( 'compute-cprod', function tests() {
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
expect( badValue( values[ i ] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
Expand All @@ -45,6 +46,28 @@ describe( 'compute-cprod', function tests() {
}
});

it( 'should throw an error if provided an accessor which is not a function', function test() {
var values = [
'5',
5,
true,
undefined,
null,
NaN,
[],
{}
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[ i ] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
cprod( [], value );
};
}
});

it( 'should compute the cumulative product', function test() {
var data, expected, results;

Expand All @@ -53,8 +76,71 @@ describe( 'compute-cprod', function tests() {

results = cprod( data );

assert.strictEqual( results.length, expected.length );
assert.strictEqual( results.length, data.length );
assert.deepEqual( results, expected );
});

it( 'should compute the cumulative product using an accessor function', function test() {
var data, expected, results;

data = [
{'x':2},
{'x':4},
{'x':5},
{'x':3},
{'x':8},
{'x':2}
];
expected = [ 2, 8, 40, 120, 960, 1920 ];

results = cprod( data, getValue );

assert.strictEqual( results.length, data.length );
assert.deepEqual( results, expected );

function getValue( d ) {
return d.x;
}
});

it( 'should zero the array once a zero is encountered', function test() {
var data, expected, results;

data = [ 2, 4, 0, 3, 8, 2 ];
expected = [ 2, 8, 0, 0, 0, 0 ];

results = cprod( data );

assert.deepEqual( results, expected );

data = [ 0, 4, 5, 3, 8, 2 ];
expected = [ 0, 0, 0, 0, 0, 0 ];

results = cprod( data );

assert.deepEqual( results, expected );
});

it( 'should zero the array once a zero is encountered when using an accessor function', function test() {
var data, expected, results;

data = [ 2, 4, 0, 3, 8, 2 ];
expected = [ 2, 8, 0, 0, 0, 0 ];

results = cprod( data, getValue );

assert.deepEqual( results, expected );

data = [ 0, 4, 5, 3, 8, 2 ];
expected = [ 0, 0, 0, 0, 0, 0 ];

results = cprod( data, getValue );

assert.deepEqual( results, expected );

function getValue( d ) {
return d;
}
});

});

0 comments on commit a52666d

Please sign in to comment.