Skip to content

Commit 25ee49a

Browse files
author
Steve Thompson
committed
Added a test.
Improved the README
1 parent fa09cdb commit 25ee49a

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

README.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
# errorIfIndexIsNegative(index): void
1+
# errorIfIndexNotValidAfterOffsetWasAdded(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayLength<br>): void
22

3-
Triggers error if `index` is negative number, saying "value not found in array".
4-
This function is intended to be called after you've called Array.indexOf(), or any
5-
function that returns a negative number if the value being searched for in the array
6-
was not found. You then pass that resulting index to this function to make sure if
7-
the value was found.
3+
Triggers error if `index` is not valid, assuming an offset was recently added to it.
4+
For example, if you're writing a getIndex() function that gives you the option of adding
5+
an offset to modify the index it returns, `errorIfIndexNotValidAfterOffsetWasAdded()`
6+
might come in handy. For more info, read the example below.
87

98
## Example
10-
```
11-
let arr = [1,2,3,4];
12-
let index = arr.indexOf(5);
13-
errorIfIndexIsNegative(index);
14-
// "Error: value not found in array"
9+
```ts
10+
function getIndexSlightlyOff(value, array, offset){
11+
let index = array.indexOf(value);
12+
index += offset;
13+
// Now check if index still valid:
14+
errorIfIndexNotValidAfterOffsetWasAdded(index, array.length);
15+
return index;
16+
}
17+
18+
let arr = [1,2,3,4,5];
19+
let index = getIndexSlightlyOff(5, arr, 1);
20+
// Error: "After the offset was added to the index, the resulting
21+
// index was too high for this array"
22+
23+
let arr = [1,2,3,4,5];
24+
let index = getIndexSlightlyOff(2, arr, -2);
25+
// Error: "After the offset was added to the index, the resulting
26+
// index was a negative number. This is not allowed."
1527
```
1628

1729
## Installation
18-
`npm install error-if-index-is-negative`
30+
`npm install error-if-index-not-valid-after-offset-was-added`
1931

2032
## Loading
21-
```
33+
```ts
2234
// if using TypeScript:
23-
import { errorIfIndexIsNegative } from 'error-if-index-is-negative';
35+
import { errorIfIndexNotValidAfterOffsetWasAdded }
36+
from 'error-if-index-not-valid-after-offset-was-added';
2437
// if using ES5 JavaScript:
25-
var errorIfIndexIsNegative =
26-
require('error-if-index-is-negative').errorIfIndexIsNegative;
38+
var errorIfIndexNotValidAfterOffsetWasAdded =
39+
require('error-if-index-not-valid-after-offset-was-added')
40+
.errorIfIndexNotValidAfterOffsetWasAdded;
2741
```
2842

2943
## License

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
var index_is_valid_1 = require("@writetome51/index-is-valid");
44
function errorIfIndexNotValidAfterOffsetWasAdded(index, arrayLength) {
5-
var errorPrefix = "After the offset was added to the index of the passed value, the resulting index ";
5+
var errorPrefix = "After the offset was added to the index, the resulting index ";
66
if (index < 0) {
77
throw new Error(errorPrefix + "was a negative number. This is not allowed.");
88
}

index.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
/********************
4+
function getIndexSlightlyOff(value, array, offset){
5+
let index = array.indexOf(value);
6+
index += offset;
7+
errorIfIndexNotValidAfterOffsetWasAdded(index, array.length);
8+
return index;
9+
}
10+
11+
//let arr = [1,2,3,4,5];
12+
//let index = getIndexSlightlyOff(5, arr, 1);
13+
// Error: "After the offset was added to the index, the resulting
14+
// index was too high for this array"
15+
16+
let arr = [1,2,3,4,5];
17+
let index = getIndexSlightlyOff(2, arr, -2);
18+
// Error: "After the offset was added to the index, the resulting
19+
// index was a negative number. This is not allowed."
20+
21+
22+
********************/

0 commit comments

Comments
 (0)