Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/arrayf/arrayf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,23 @@ export class arrayf {
return output;
}


}
/**
* Given an index that could be out-of-range for an array of the given
* length, it will return a valid index, as if the array values looped
* infinitely.
*
* ```
* wrapIndex(2, 7); // Returns 2
* wrapIndex(10, 7); // Returns 3
* wrapIndex(21, 7); // Returns 0
* wrapIndex(-1, 7); // Returns 6
* ```
*/
static wrapIndex(index: number, length: number): number {
if (index < 0) {
return length + (index % length);
} else {
return index % length;
}
}
}