Skip to content

Commit

Permalink
Merge pull request #1 from compute-io/develop
Browse files Browse the repository at this point in the history
Fixes, fmt, and updates
  • Loading branch information
Planeshifter committed Feb 28, 2015
2 parents 329b231 + bd426e0 commit 1ec0ffd
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 69 deletions.
48 changes: 37 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ nanrange
===
[![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 arithmetic range of an array of values ignoring all non-numeric elements.
> Computes the arithmetic range of an array ignoring non-numeric values.
## Installation

Expand All @@ -19,31 +19,55 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
var nanrange = require( 'compute-nanrange' );
```

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

Returns the range of an `array` of values ignoring all non-numeric elements.
Computes the arithmetic range of an `array` ignoring non-numeric values. For primitive `arrays`,

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

var r = range( arr );
// returns [1,4]
```

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

``` javascript
var arr = [
[1,2],
[2,null],
[3,3],
[4,4],
[5,null],
[6,1]
];

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

var r = range( arr, getValue );
// returns [1,4]
```

__Note__: if an input `array` does not contain any `numeric` values, the function returns `null`.



## Examples

``` javascript
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;
}
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 @@ -52,6 +76,8 @@ To run the example code from the top-level application directory,
$ node ./examples/index.js
```



## Tests

### Unit
Expand Down
10 changes: 5 additions & 5 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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;
}
if ( i % 2 === 0 ){
data[ i ] = null;
} else {
data[ i ] = Math.random()*100;
}
}

console.log( nanrange( data ) );
69 changes: 41 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*
* DESCRIPTION:
* - Computes the arithmetic range of an array of values ignoring all non-numeric elements.
* - Computes the arithmetic range of an array ignoring non-numeric values.
*
*
* NOTES:
Expand All @@ -30,40 +30,53 @@

// MODULES //

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

// FUNCTIONS //

// NANRANGE //

/**
* FUNCTION: nanrange( arr )
* Computes the arithmetic range of an array of values ignoring all
* non-numeric elements
* FUNCTION: nanrange( arr[, accessor] )
* Computes the arithmetic range of an array ignoring non-numeric values.
*
* @param {Array} arr - input array
* @param {Function} [accessor] - accessor function for accessing array values
* @returns {Array|null} arithmetic range or null
*/
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 ];
function nanrange( arr, clbk ) {
if ( !isArray( arr ) ) {
throw new TypeError( 'range()::invalid input argument. Must provide an array. Value: `' + arr + '`.' );
}
if ( arguments.length > 1 && typeof clbk !== 'function' ) {
throw new TypeError( 'range()::invalid input argument. Accessor must be a function. Value: `' + clbk + '`.' );
}
var len = arr.length,
min = null,
max = min,
flg = true,
x;

if ( !isNumber( x ) ) {
continue;
}
if ( min === null || x < min ) {
min = x;
}
if ( max === null || x > max ) {
max = x;
}
}
return [ min, max ];
for ( var i = 0; i < len; i++ ) {
x = ( clbk ) ? clbk( arr[i] ) : arr[ i ];
if ( !isNumber( x ) ) {
continue;
}
if ( flg ) {
min = x;
max = x;
flg = false;
continue;
}
if ( x < min ) {
min = x;
} else if ( x > max ) {
max = x;
}
}
return ( flg ) ? null : [ min, max ];
} // end FUNCTION nanrange()


// EXPORTS //

module.exports = nanrange;
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "compute-nanrange",
"version": "0.9.0",
"description": "Computes the arithmetic range of an array of values ignoring all non-numeric elements.",
"description": "Computes the arithmetic range of an array ignoring non-numeric values.",
"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 @@ -28,8 +32,15 @@
"computation",
"statistics",
"stats",
"mathematics",
"math",
"range",
"arithmetic"
"arithmetic",
"min",
"max",
"minimum",
"maximum",
"extremes"
],
"bugs": {
"url": "https://github.com/compute-io/nanrange/issues"
Expand All @@ -39,8 +50,8 @@
"validate.io-number": "^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
121 changes: 100 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,112 @@ describe( 'compute-nanrange', function tests() {

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 ],
'5',
5,
null,
undefined,
NaN,
true,
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 throw an error if provided an accessor which is not a function', function test() {
var values = [
'5',
5,
null,
undefined,
NaN,
true,
[],
{}
];
for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( TypeError );
}
function badValue( value ) {
return function() {
nanrange( [], value );
};
}
});

it( 'should compute the arithmetic range', function test() {
var data, expected;

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 ],
var data, expected;

data = [ 3, null, 4, 2, 1, null, 4, 6 ];
expected = [ 1, 6 ];

assert.deepEqual( nanrange( data ), expected );
});

it( 'should compute the arithmetic range using an accessor function', function test() {
var data, expected, actual;

data = [
[1,3],
[2,null],
[3,4],
[4,2],
[5,1],
[6,null],
[7,4],
[8,6]
];

expected = [ 1, 6 ];
actual = nanrange( data, getValue );

assert.deepEqual( actual, expected );

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

it( 'should return null is provided either an empty array or an array without any numeric values', function test() {
var data, expected;

data = [
null,
false,
undefined,
'',
NaN,
[],
{}
];
expected = null;

assert.strictEqual( nanrange( [] ), expected );
assert.strictEqual( nanrange( data ), expected );
});

it( 'should compute an arithmetic range even if only provided an array containing a single numeric value', function test() {
var data, expected;

data = [ 5 ];
expected = [ 5, 5 ];

assert.deepEqual( nanrange( data ), expected );
});

Expand Down

0 comments on commit 1ec0ffd

Please sign in to comment.