diff --git a/README.md b/README.md index c9cf1c1..912b0ee 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,38 @@ To use the module, var range = require( 'compute-range' ); ``` -#### range( arr ) +#### range( arr[, accessor] ) -Returns the range of an `array` of values. +Computes the arithmetic range of an `array`. For primitive `arrays`, ``` javascript -var r = range( [2,3,4,1] ); +var arr = [ 2, 3, 4, 1 ]; + +var r = range( arr ); +// returns [1,4] +``` + +For object `arrays`, provide an accessor `function` for accessing `array` values + +``` javascript +var arr = [ + [1,2], + [3,3], + [4,4], + [6,1] +]; + +function getValue( d ) { + return d[ 1 ]; +} + +var r = range( arr, getValue ); // returns [1,4] ``` -Note: the first value of the returned `array` is always the minimum value and the second value is always the maximum value. +__Note__: if an input `array` does not contain any `numeric` values, the function returns `null`. +__Note__: the first value of the returned `array` is always the minimum value and the second value is always the maximum value. ## Examples @@ -84,7 +105,7 @@ $ open reports/coverage/lcov-report/index.html ## License -[MIT license](http://opensource.org/licenses/MIT). +[MIT license](http://opensource.org/licenses/MIT). --- @@ -109,4 +130,4 @@ Copyright © 2014. Athan Reines. [dev-dependencies-url]: https://david-dm.org/dev/compute-io/range [github-issues-image]: http://img.shields.io/github/issues/compute-io/range.svg -[github-issues-url]: https://github.com/compute-io/range/issues \ No newline at end of file +[github-issues-url]: https://github.com/compute-io/range/issues diff --git a/lib/index.js b/lib/index.js index 9994a76..a395e21 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,11 +8,11 @@ * * * NOTES: -* [1] +* [1] * * * TODO: -* [1] +* [1] * * * LICENSE: @@ -26,36 +26,46 @@ * */ -(function() { - 'use strict'; +'use strict'; - /** - * FUNCTION: range( arr ) - * Returns the arithmetic range of an array of values. - */ - function range( arr ) { - if ( !Array.isArray( arr ) ) { - throw new TypeError( 'range()::invalid input argument. Must provide an array.' ); - } - var len = arr.length, - min = arr[ 0 ], - max = min, - x; - - for ( var i = 1; i < len; i++ ) { - x = arr[ i ]; - if ( x < min ) { - min = x; - } else if ( x > max ) { - max = x; - } - } - return [ min, max ]; - } // end FUNCTION range() +/** +* FUNCTION: range( arr[, accessor] ) +* Returns the arithmetic range of an array of values. +* +* @param {Array} arr - input array +* @param {Function} [accessor] - accessor function for accessing array values +* @returns {Array|null} arithmetic range or null +*/ +function range( arr, clbk ) { + if ( !Array.isArray( arr ) ) { + throw new TypeError( 'range()::invalid input argument. Must provide an array.' ); + } + 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 = ( clbk ) ? clbk( arr[0] ) : arr[ 0 ], + max = min, + x; + + if ( len === 0 ) { + return null; + } + + for ( var i = 1; i < len; i++ ) { + x = ( clbk ) ? clbk( arr[i] ) : arr[ i ]; + if ( x < min ) { + min = x; + } else if ( x > max ) { + max = x; + } + } + return [ min, max ]; +} // end FUNCTION range() - // EXPORTS // - module.exports = range; +// EXPORTS // -})(); \ No newline at end of file +module.exports = range; diff --git a/test/test.js b/test/test.js index 7552db2..e357138 100644 --- a/test/test.js +++ b/test/test.js @@ -45,6 +45,33 @@ describe( 'compute-range', function tests() { } }); + 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() { + range( [], value ); + }; + } + }); + + it( 'should return null if provided an empty array', function test() { + var expected = null; + + assert.strictEqual( range( [] ), expected ); + }); + it( 'should return the arithmetic range', function test() { var data = [ 3,4,2,1,4 ], expected = [1,4]; @@ -52,4 +79,26 @@ describe( 'compute-range', function tests() { assert.deepEqual( range( data ), expected ); }); -}); \ No newline at end of file + it( 'should return the arithmetic range using an accessor function', function test() { + var data, expected, actual; + + data = [ + [1,3], + [3,4], + [4,2], + [5,1], + [7,4], + [8,6] + ]; + + expected = [ 1, 6 ]; + actual = range( data, getValue ); + + assert.deepEqual( actual, expected ); + + function getValue( d ) { + return d[ 1 ]; + } + }); + +});