-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: clamp hoverX interaction handler to nearest rounded timestamp
- Loading branch information
1 parent
f636956
commit b6f9a93
Showing
6 changed files
with
91 additions
and
5 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
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
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
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
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,25 @@ | ||
export const nearestTimestamp = (timestamps, rawValue) => { | ||
if (timestamps.length === 0) { | ||
return rawValue | ||
} | ||
|
||
if (timestamps.length === 1) { | ||
return timestamps[0] | ||
} | ||
|
||
const midPoint = timestamps.length / 2 | ||
const firstHalf = timestamps.slice(0, midPoint) | ||
const secondHalf = timestamps.slice(midPoint) | ||
|
||
const firstPivotPoint = firstHalf[firstHalf.length - 1] | ||
const secondPivotPoint = secondHalf[0] | ||
|
||
const firstHalfDistance = Math.abs(firstPivotPoint - rawValue) | ||
const secondHalfDistance = Math.abs(secondPivotPoint - rawValue) | ||
|
||
if (firstHalfDistance > secondHalfDistance) { | ||
return nearestTimestamp(secondHalf, rawValue) | ||
} | ||
|
||
return nearestTimestamp(firstHalf, rawValue) | ||
} |
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,43 @@ | ||
import {nearestTimestamp} from './nearestTimestamp' | ||
|
||
describe('finding the nearest timestamp', () => { | ||
const tens = [10, 20, 30, 40, 50, 60, 70, 80, 90] | ||
|
||
it('returns the nearest timestamp', () => { | ||
expect(nearestTimestamp(tens, 8)).toBe(10) | ||
expect(nearestTimestamp(tens, 29)).toBe(30) | ||
expect(nearestTimestamp(tens, 36)).toBe(40) | ||
expect(nearestTimestamp(tens, 52)).toBe(50) | ||
expect(nearestTimestamp(tens, 60)).toBe(60) | ||
expect(nearestTimestamp(tens, 74)).toBe(70) | ||
expect(nearestTimestamp(tens, 99)).toBe(90) | ||
}) | ||
|
||
it('rounds down in the case of ties', () => { | ||
expect(nearestTimestamp(tens, 55)).toBe(50) | ||
}) | ||
|
||
it('handles negative numbers', () => { | ||
expect(nearestTimestamp(tens, -5)).toBe(10) | ||
}) | ||
|
||
it('handles very big numbers', () => { | ||
expect(nearestTimestamp(tens, Number.MAX_SAFE_INTEGER)).toBe(90) | ||
}) | ||
|
||
it('handles non integers', () => { | ||
expect(nearestTimestamp(tens, 44.87628090023)).toBe(40) | ||
}) | ||
|
||
it('handles single item arrays', () => { | ||
expect(nearestTimestamp([25], 55)).toBe(25) | ||
}) | ||
|
||
it('handles dual item arrays', () => { | ||
expect(nearestTimestamp([25, 75], 55)).toBe(75) | ||
}) | ||
|
||
it('returns the hover value when the array is empty', () => { | ||
expect(nearestTimestamp([], 55)).toBe(55) | ||
}) | ||
}) |