forked from pomber/git-history
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function nextIndex(list, currentIndex) { | ||
return Math.min(list.length - 1, Math.floor(currentIndex + 1)); | ||
} | ||
|
||
export function prevIndex(list, currentIndex) { | ||
return Math.max(0, Math.ceil(currentIndex - 1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { nextIndex, prevIndex } from "./utils"; | ||
|
||
describe("nextIndex", () => { | ||
const fiveItems = [1, 2, 3, 4, 5]; | ||
test("works with middle index", () => { | ||
expect(nextIndex(fiveItems, 2)).toBe(3); | ||
}); | ||
test("works with last index", () => { | ||
expect(nextIndex(fiveItems, 4)).toBe(4); | ||
}); | ||
test("works with fractions", () => { | ||
expect(nextIndex(fiveItems, 1.1)).toBe(2); | ||
expect(nextIndex(fiveItems, 1.9)).toBe(2); | ||
}); | ||
}); | ||
|
||
describe("prevIndex", () => { | ||
const fiveItems = [1, 2, 3, 4, 5]; | ||
test("works with middle index", () => { | ||
expect(prevIndex(fiveItems, 2)).toBe(1); | ||
}); | ||
test("works with start index", () => { | ||
expect(prevIndex(fiveItems, 0)).toBe(0); | ||
}); | ||
test("works with fractions", () => { | ||
expect(prevIndex(fiveItems, 1.1)).toBe(1); | ||
expect(prevIndex(fiveItems, 1.9)).toBe(1); | ||
}); | ||
}); |