Skip to content

Commit

Permalink
[UPDATE] nanrange, tests, README
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Feb 28, 2015
1 parent 42a6c6c commit 2eacc56
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ nanrange

> Computes the arithmetic range of an array of values ignoring all non-numeric elements.

## Installation

``` bash
Expand All @@ -17,18 +16,34 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
## Usage

``` javascript
var foo = require( 'compute-nanrange' );
var nanrange = require( 'compute-nanrange' );
```

#### foo( arr )
#### nanrange( arr )

What does this function do?
Returns the range of an `array` of values ignoring all non-numeric elements.

``` javascript
var r = range( [2, null, 3, 4, null, 1] );
// returns [1,4]
```

## Examples

``` javascript
var foo = require( 'compute-nanrange' );
var nanrange = require( 'compute-nanrange' );

var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
if( i % 2 === 0 ){
data[ i ] = null;
} else {
data[ i ] = Math.random()*100;
}
}

console.log( nanrange( data ) );

```

To run the example code from the top-level application directory,
Expand All @@ -37,7 +52,6 @@ To run the example code from the top-level application directory,
$ node ./examples/index.js
```


## Tests

### Unit
Expand Down Expand Up @@ -69,7 +83,7 @@ $ make view-cov
---
## License

[MIT license](http://opensource.org/licenses/MIT).
[MIT license](http://opensource.org/licenses/MIT).


## Copyright
Expand Down
13 changes: 12 additions & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
'use strict';

var module = require( './../lib' );
var nanrange = require( './../lib' );

var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
if( i % 2 === 0 ){
data[ i ] = null;
} else {
data[ i ] = Math.random()*100;
}
}

console.log( nanrange( data ) );
36 changes: 28 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,40 @@

// MODULES //

// var module_alias = require( 'module_name' );

var isArray = require( 'validate.io-array' );
var isNumber = require( 'validate.io-number' );

// FUNCTIONS //

/**
* FUNCTION: foo()
* {{ foo description }}.
* FUNCTION: nanrange( arr )
* Computes the arithmetic range of an array of values ignoring all
* non-numeric elements
*/
function foo() {
function nanrange( arr ) {
if ( !isArray( arr ) ) {
throw new TypeError( 'range()::invalid input argument. Must provide an array.' );
}
var len = arr.length,
min = null,
max = min,
x;
for ( var i = 1; i < len; i++ ) {
x = arr[ i ];

} // end FUNCTION foo()
if ( !isNumber( x ) ) {
continue;
}
if ( min === null || x < min ) {
min = x;
}
if ( max === null || x > max ) {
max = x;
}
}
return [ min, max ];
} // end FUNCTION nanrange()


// EXPORTS //

module.exports = foo;
module.exports = nanrange;
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@
"keywords": [
"compute.io",
"compute",
"computation"
"computation",
"statistics",
"stats",
"range",
"arithmetic"
],
"bugs": {
"url": "https://github.com/compute-io/nanrange/issues"
},
"dependencies": {},
"dependencies": {
"validate.io-array": "^1.0.3",
"validate.io-number": "^1.0.3"
},
"devDependencies": {
"chai": "1.x.x",
"mocha": "1.x.x",
Expand Down
36 changes: 33 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var // Expectation library:
chai = require( 'chai' ),

// Module to be tested:
lib = require( './../lib' );
nanrange = require( './../lib' );


// VARIABLES //
Expand All @@ -21,9 +21,39 @@ var expect = chai.expect,
describe( 'compute-nanrange', function tests() {

it( 'should export a function', function test() {
expect( lib ).to.be.a( 'function' );
expect( nanrange ).to.be.a( 'function' );
});

it( 'should do something' );
it( 'should throw an error if not provided an array', function test() {
var values = [
'5',
5,
null,
undefined,
NaN,
function(){},
{}
];
for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
nanrange( value );
};
}
});

it( 'should return the arithmetic range', function test() {
var data = [ 3, 4, 2, 1, 4 ],
expected = [ 1, 4 ];
assert.deepEqual( nanrange( data ), expected );
});

it( 'should ignore non-numeric values', function test() {
var data = [ 3, null, 4, 2, 1, null, 4, 6 ],
expected = [ 1, 6 ];
assert.deepEqual( nanrange( data ), expected );
});

});

0 comments on commit 2eacc56

Please sign in to comment.