Skip to content

Commit

Permalink
[FIX] negative indices.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Mar 3, 2015
1 parent 8de38a5 commit d1ced8f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Notes about the notation:

``` javascript
var arr = indexspace( '[:-2]', 5 );
// returns [ 0, 1 ]
// returns [ 0, 1, 2 ]
```

The function also recognizes the `end` keyword, which refers to the last index; i.e., `len-1`. If specified as the `stop` index, `end` is inclusive.
Expand Down Expand Up @@ -108,7 +108,7 @@ arr = indexspace( '[:10:3]', 20 );
// returns [ 0, 3, 6, 9 ]

arr = indexspace( '[:-2]', 5 );
// returns [ 0, 1 ]
// returns [ 0, 1, 2 ]

arr = indexspace( '[:-1:2]', 5 );
// returns [ 0, 2 ]
Expand All @@ -117,7 +117,7 @@ arr = indexspace( '[-4:-1:2]', 5 );
// returns [ 0, 2 ]

arr = indexspace( '[-5:-1]', 5 );
// returns [ 0, 1, 2 ]
// returns [ 0, 1, 2, 3 ]

arr = indexspace( '[::-1]', 5 );
// returns [ 4, 3, 2, 1, 0 ]
Expand Down
4 changes: 2 additions & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ console.log( arr );
arr = indexspace( '[:-2]', 5 );
console.log( '\n[:-2]' );
console.log( arr );
// returns [ 0, 1 ]
// returns [ 0, 1, 2 ]

arr = indexspace( '[:-1:2]', 5 );
console.log( '\n[:-1:2]' );
Expand All @@ -55,7 +55,7 @@ console.log( arr );
arr = indexspace( '[-5:-1]', 5 );
console.log( '\n[-5:-1]' );
console.log( arr );
// returns [ 0, 1, 2 ]
// returns [ 0, 1, 2, 3 ]

arr = indexspace( '[::-1]', 5 );
console.log( '\n[::-1]' );
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function indexspace( str, len ) {
}
// Handle negative index...
else if ( x2 < 0 ) {
x2 = len - 2 + x2; // len-1-x2 = (end)-x2
x2 = len - 1 + x2; // len-1-x2 = (end)-x2
if ( x2 < 0 ) {
// WARNING: forgive the user for exceeding index bounds...
x2 = 0;
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ describe( 'compute-indexspace', function tests() {
assert.deepEqual( actual, expected, '[2::2]' );

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

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

actual = indexspace( '[:-1:2]', 5 );
Expand All @@ -135,7 +135,7 @@ describe( 'compute-indexspace', function tests() {
assert.deepEqual( actual, expected, '[-4:-1:2]' );

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

actual = indexspace( '[::-1]', 5 );
Expand Down

0 comments on commit d1ced8f

Please sign in to comment.