Skip to content

Commit

Permalink
[FIX] edge cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Mar 3, 2015
1 parent 1c9cab4 commit baa11d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ function indexspace( str, len ) {
if ( x2 < 0 ) {
// WARNING: forgive the user for exceeding the range bounds...
x2 = 0;
} else if ( x2 === x1 ) {
return [];
}
} else {
x2 = (len-1) / parseInt( tmp[ 2 ], 10 );
Expand Down Expand Up @@ -221,6 +223,8 @@ function indexspace( str, len ) {
if ( x2 < 0 ) {
// WARNING: forgive the user for exceeding index bounds...
x2 = 0;
} else if ( x2 === x1 ) {
return [];
}
}
// Handle positive index...
Expand All @@ -241,10 +245,6 @@ function indexspace( str, len ) {

arr = [];

// Handle equal indices...
if ( x1 === x2 ) {
return arr;
}
if ( inc < 0 ) {
if ( x2 > x1 ) {
return arr;
Expand Down
7 changes: 5 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ describe( 'compute-indexspace', function tests() {
it( 'should return an empty array if start and end indices are equal', function test() {
var actual;

actual = indexspace( '[2:]', 3 );
actual = indexspace( '[2:end-2]', 5 );
assert.deepEqual( actual, [] );

actual = indexspace( '[:2:-1]', 3 );
actual = indexspace( '[2:-2]', 5 );
assert.deepEqual( actual, [] );

actual = indexspace( '[2:2]', 5 );
assert.deepEqual( actual, [] );
});

Expand Down

0 comments on commit baa11d6

Please sign in to comment.