From a6ec4b6bc36308ba0108f86733ffa6384fb63c89 Mon Sep 17 00:00:00 2001 From: Planeshifter Date: Thu, 25 Jun 2015 21:06:00 +0200 Subject: [PATCH] [UPDATE] README, tests [FIX] index.js indentation --- README.md | 17 ++++++++++++++++- lib/index.js | 7 ++++--- test/test.js | 32 ++++++++++++++++---------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7a6991a..cfda777 100644 --- a/README.md +++ b/README.md @@ -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' ), @@ -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 diff --git a/lib/index.js b/lib/index.js index ba71d2a..cef4cfa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 ); diff --git a/test/test.js b/test/test.js index 41801a7..6e7aa8c 100644 --- a/test/test.js +++ b/test/test.js @@ -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 = [