Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UPDATE] validate.io modules #1

Merged
merged 4 commits into from
May 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
69 changes: 27 additions & 42 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +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 @@ -40,40 +22,43 @@
* @returns {Array} linearly spaced numeric array
*/
function incrspace( x1, x2, inc ) {
var arr, len, i;
if ( typeof x1 !== 'number' || x1 !== x1 ) {
var arr,
len,
i;
if ( !isNumber( x1 ) ) {
throw new TypeError( 'incrspace()::invalid input argument. Start must be numeric. Value: `' + x1 + '`.' );
}
if ( typeof x2 !== 'number' || x2 !== x2 ) {
if ( !isNumber( x2 ) ) {
throw new TypeError( 'incrspace()::invalid input argument. Stop must be numeric. Value: `' + x2 + '`.' );
}
if ( arguments.length < 3 ) {
inc = 1;
} else {
if ( typeof inc !== 'number' || inc !== inc ) {
if ( !isNumber( inc ) ) {
throw new TypeError( 'incrspace()::invalid input argument. Increment must be numeric. Value: `' + inc + '`.' );
}
}
// Calculate the array length:
len = Math.ceil( ( x2-x1 ) / inc );
if ( len < 0 ) {

if ( len > MAXLENGTH ) {
throw new RangeError( 'incrspace()::invalid input arguments. Generated array exceeds maximum array length.' );
}
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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@
"linear",
"sequence",
"mathematics",
"math"
"math",
"increment"
],
"bugs": {
"url": "https://github.com/compute-io/incrspace/issues"
},
"dependencies": {
"validate.io-number-primitive": "^1.0.0"
},
"devDependencies": {
"chai": "2.x.x",
"compute-roundn": "^1.0.3",
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
"mocha": "2.x.x",
"jshint": "^2.6.3",
"jshint-stylish": "^1.0.1"
"jshint": "^2.6.3",
"jshint-stylish": "^1.0.1"
},
"licenses": [
{
Expand Down
27 changes: 24 additions & 3 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,6 +96,24 @@ describe( 'compute-incrspace', function tests() {
}
});

it( 'should throw an error if the maximum array length is exceeded', function test() {
var values = [
0.000000000000001,
0.00000000000000000000001,
0.000000000000000000000000001,
0
];

for ( var i = 0; i < values.length; i++ ) {
expect( badValue( values[ i ] ) ).to.throw( RangeError );
}
function badValue( value ) {
return function() {
incrspace( 0, 10, value );
};
}
});

it( 'should return a linearly spaced array', function test() {
var start, stop, expected, actual;

Expand Down