Skip to content

Commit

Permalink
[UPDATE] README, tests [FIX] index.js indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Jun 25, 2015
1 parent 2aa108a commit a6ec4b6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -22,7 +22,7 @@ var multiply = require( 'compute-multiply' );

#### multiply( x, y[, opts] )

Computes an element-wise multiplication. `x` can be a [`number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) or [`matrix`](https://github.com/dstructs/matrix). `y` has to be either an `array` or `matrix` of equal dimensions as `x` or a single number. The function returns either an `array` with length equal to that of the input `array`, a `matrix` with equal dimensions as input `x` or a single number.
Computes an element-wise multiplication. `x` can be a [`number`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number), [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) or [`matrix`](https://github.com/dstructs/matrix). `y` has to be either an `array` or `matrix` of equal dimensions as `x` or a single number. The function returns either an `array` with the same length as the input `array`, a `matrix` with the same dimensions as the input `matrix` or a single number.

``` javascript
var matrix = require( 'dstructs-matrix' ),
Expand Down Expand Up @@ -290,6 +290,21 @@ __Note__: mutation is the `array` or `matrix` equivalent of an __times-equal__ (
// returns Int8Array( [0,0,0] );
```

* When calling the function with a numeric value as the first argument and a `matrix` or `array` as the second argument, only the `dtype` option is applicable.

``` javascript
// Valid:
var out = multiply( 2.1, [ 0, 1, 2 ], {
'dtype': 'int8'
});
// returns Int8Array( [0,2,4] )

// Not valid:
var out = add( 0.5, [ 0, 1, 2 ], {
'copy': false
});
// throws an error
```

## Examples

Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Expand Up @@ -44,11 +44,12 @@ function multiply( x, y, options ) {
out,
dt,
d;
// Handle cases where first argument is a number
if ( isNumber( x ) || isnan( x ) ) {
for ( var key in options ) {
if ( key !== 'dtype' ){
throw new Error( 'multiply()::only dtype option is applicable when first argument is not array- or matrix-like. Keys: `' + Object.keys( options ) + '`.' );
}
if ( key !== 'dtype' ){
throw new Error( 'multiply()::only dtype option is applicable when first argument is not array- or matrix-like. Keys: `' + Object.keys( options ) + '`.' );
}
}
if ( isArrayLike( y ) ) {
return options ? multiply( y, x, options ) : multiply( y, x );
Expand Down
32 changes: 16 additions & 16 deletions test/test.js
Expand Up @@ -90,22 +90,22 @@ describe( 'compute-multiply', function tests() {
}
});

it( 'should throw an error if provided a number as the first argument and an option', function test() {
var values = [
{'accessor': function getValue( d ) { return d; } },
{'copy': false},
{'path': 'x'},
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( Error );
}
function badValue( value ) {
return function() {
multiply( 1, [1,2,3], value );
};
}
});
it( 'should throw an error if provided a number as the first argument and an not applicable option', function test() {
var values = [
{'accessor': function getValue( d ) { return d; } },
{'copy': false},
{'path': 'x'},
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( Error );
}
function badValue( value ) {
return function() {
multiply( 1, [1,2,3], value );
};
}
});

it( 'should return NaN if the first argument is neither a number, array-like, or matrix-like', function test() {
var values = [
Expand Down

0 comments on commit a6ec4b6

Please sign in to comment.