Skip to content

Commit

Permalink
[UPDATE] remove preamble. move max length to outer scope. test for nu…
Browse files Browse the repository at this point in the history
…mber primitive.
  • Loading branch information
kgryte committed May 2, 2015
1 parent a1a9a47 commit d9e0b88
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 54 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Athan Reines.
Copyright (c) 2014-2015 Athan Reines.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ $ node ./examples/index.js

### Unit

Unit tests use the [Mocha](http://visionmedia.github.io/mocha) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
Unit tests use the [Mocha](http://mochajs.org) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:

``` bash
$ make test
Expand Down
61 changes: 18 additions & 43 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
/**
*
* COMPUTE: incrspace
*
*
* DESCRIPTION:
* - Generates a linearly spaced numeric array using a provided increment.
*
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2014-2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2014.
*
*/

'use strict';

// MODULES //

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


// VARIABLES //

var MAXLENGTH = Math.pow( 2, 32 ) - 1;


// INCRSPACE //

/**
Expand All @@ -44,7 +22,9 @@ var isNumber = require( 'validate.io-number-primitive' );
* @returns {Array} linearly spaced numeric array
*/
function incrspace( x1, x2, inc ) {
var arr, len, i, maxLength;
var arr,
len,
i;
if ( !isNumber( x1 ) ) {
throw new TypeError( 'incrspace()::invalid input argument. Start must be numeric. Value: `' + x1 + '`.' );
}
Expand All @@ -58,32 +38,27 @@ function incrspace( x1, x2, inc ) {
throw new TypeError( 'incrspace()::invalid input argument. Increment must be numeric. Value: `' + inc + '`.' );
}
}
// Calculate the array length:
len = Math.ceil( ( x2-x1 ) / inc );

maxLength = Math.pow(2,32) - 1;
if ( len >= maxLength ) {
throw new RangeError( 'incrspace()::invalid input arguments. (start - stop) / increment exceeds maximum Array length.' );
if ( len > MAXLENGTH ) {
throw new RangeError( 'incrspace()::invalid input arguments. Generated array exceeds maximum array length.' );
}

if ( len < 0 ) {
if ( len <= 1 ) {
return [ x1 ];
}

// Build the output array...
if ( len > 64000 ) {
// Ensure fast elements...
arr = [];
arr.push( x1 );
for ( i = 1; i < len; i++ ) {
arr.push( x1 + inc*i );
}
return arr;
}
arr = new Array( len );
arr[ 0 ] = x1;
for ( i = 1; i < len; i++ ) {
arr[ i ] = x1 + inc*i;
} else {
arr = new Array( len );
arr[ 0 ] = x1;
for ( i = 1; i < len; i++ ) {
arr[ i ] = x1 + inc*i;
}
}
return arr;
} // end FUNCTION incrspace()
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"linear",
"sequence",
"mathematics",
"math"
"math",
"increment"
],
"bugs": {
"url": "https://github.com/compute-io/incrspace/issues"
Expand Down
15 changes: 8 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ describe( 'compute-incrspace', function tests() {
expect( incrspace ).to.be.a( 'function' );
});

it( 'should throw an error if the `start` value is not a numeric value', function test() {
it( 'should throw an error if the `start` value is not a number primitive', function test() {
var values = [
'5',
new Number( 5 ),
null,
true,
undefined,
Expand All @@ -47,9 +48,10 @@ describe( 'compute-incrspace', function tests() {
}
});

it( 'should throw an error if the `stop` value is not a numeric value', function test() {
it( 'should throw an error if the `stop` value is not a number primitive', function test() {
var values = [
'5',
new Number( 5 ),
null,
true,
undefined,
Expand All @@ -70,9 +72,10 @@ describe( 'compute-incrspace', function tests() {
}
});

it( 'should throw an error if the `increment` is not a numeric value', function test() {
it( 'should throw an error if the `increment` is not a number primitive', function test() {
var values = [
'5',
new Number( 1 ),
null,
true,
undefined,
Expand All @@ -93,8 +96,7 @@ describe( 'compute-incrspace', function tests() {
}
});

it( 'should throw an error if the maximum Array length is exceeded', function test() {

it( 'should throw an error if the maximum array length is exceeded', function test() {
var values = [
0.000000000000001,
0.00000000000000000000001,
Expand All @@ -103,9 +105,8 @@ describe( 'compute-incrspace', function tests() {
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[i] ) ).to.throw( RangeError );
expect( badValue( values[ i ] ) ).to.throw( RangeError );
}

function badValue( value ) {
return function() {
incrspace( 0, 10, value );
Expand Down

0 comments on commit d9e0b88

Please sign in to comment.