Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
language: node_js
node_js:
- "0.10"
- '0.12'
- '0.11'
- '0.10'
- '0.8'
- 'iojs'
before_install:
- npm update -g npm
after_script:
- npm run coveralls
- npm run coveralls

29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
var cprod = require( 'compute-cprod' );
```

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

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

Expand All @@ -30,7 +30,26 @@ var arr = cprod( data );
// returns [ 1, 2, 6, 24 ]
```

For object `arrays`, provide an accessor `function` for accessing `array` values
The function accepts two `options`:

* __copy__: `boolean` indicating whether to return a new `array` containing the cumulative products. Default: `true`.
* __accessor__: accessor `function` for accessing numeric values in object `arrays`.

To mutate the input `array` (e.g. when input values can be discarded or when optimizing memory usage), set the `copy` option to `false`.

``` javascript
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.

``` javascript
var data = [
Expand All @@ -40,11 +59,13 @@ var data = [
['baz', 4]
];

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

var arr = cprod( arr, getValue );
var arr = cprod( arr, {
'accessor': getValue
});
// returns [ 1, 2, 6, 24 ]
```

Expand Down
113 changes: 42 additions & 71 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,75 @@
/**
*
* COMPUTE: cumprod
*
*
* DESCRIPTION:
* - Computes the cumulative product of an array.
*
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Philipp Burckhardt.
*
*
* AUTHOR:
* Philipp Burckhardt. pburckhardt@outlook.com. 2015.
*
*/

'use strict';

// MODULES //

var isArray = require( 'validate.io-array' );
var isArray = require( 'validate.io-array' ),
isObject = require( 'validate.io-object' ),
isBoolean = require( 'validate.io-boolean-primitive' ),
isFunction = require( 'validate.io-function' );


// CUMULATIVE PRODUCT //

/**
* FUNCTION: cprod( arr[, accessor] )
* FUNCTION: cprod( arr[, options] )
* Computes the cumulative product of an array.
*
* @param {Array} arr - numeric array
* @param {Function} [accessor] - accessor function for accessing array values
* @returns {Array} cumulative product
* @param {Array} arr - input array
* @param {Object} [options] - function options
* @param {Function} [options.accessor] - accessor function for accessing array values
* @param {Boolean} [options.copy=true] - boolean indicating whether to return new array
* @returns {Number[]} cumulative product array
*/

function cprod( arr, clbk ) {
function cprod( arr, opts ) {
var copy = true,
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 + '`.' );
if ( !isObject( opts ) ) {
throw new TypeError( 'cprod()::invalid input argument. Options argument must be an object. Value: `' + opts + '`.' );
}
if ( opts.hasOwnProperty( 'accessor' ) ) {
clbk = opts.accessor;
if ( !isFunction ( clbk ) ) {
throw new TypeError( 'cprod()::invalid option. Accessor must be a function. Value: `' + clbk + '`.' );
}
}
if ( opts.hasOwnProperty( 'copy' ) ) {
copy = opts.copy;
if ( !isBoolean( copy ) ) {
throw new TypeError( 'cprod()::invalid option. Copy option must be a boolean primitive. Value: `' + copy + '`.' );
}
}
}
var len = arr.length,
v = new Array( len ),
val,
flg,
out,
v,
i;

if ( copy === true ) {
out = new Array( len );
} else {
out = arr;
}
if ( clbk ) {
val = clbk( arr[ 0 ] );
if ( val === 0 ) {
flg = true;
}
v[ 0 ] = val;
out[ 0 ] = clbk( arr[ 0 ], 0 );
for ( i = 1; i < len; i++ ) {
if ( flg ) {
v[ i ] = 0;
continue;
}
val = clbk( arr[ i ] );
if ( val === 0 ) {
flg = true;
v[ i ] = 0;
v = out[ i-1 ];
if ( v === 0 ) {
out[ i ] = 0;
} else {
v[ i ] = v[ i-1 ] * val;
out[ i ] = v * clbk( arr[ i ], i );
}
}
} else {
val = arr[ 0 ];
if ( val === 0 ) {
flg = true;
}
v[ 0 ] = val;
out[ 0 ] = arr[ 0 ];
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;
}
out[ i ] = out[ i-1 ] * arr[ i ];
}
}
return v;
return out;
} // end FUNCTION cprod()


Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
"url": "https://github.com/compute-io/cprod/issues"
},
"dependencies": {
"validate.io-array": "^1.0.3"
"validate.io-array": "^1.0.3",
"validate.io-boolean-primitive": "^1.0.0",
"validate.io-function": "^1.0.2",
"validate.io-object": "^1.0.3"
},
"devDependencies": {
"chai": "2.x.x",
Expand Down
Loading