-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is based on code climate suggestions
- Loading branch information
Showing
3 changed files
with
162 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Maps the items to items with an active property | ||
*/ | ||
export const mapToActiveIndex = <GItem extends {}>( | ||
items: GItem[], | ||
activeIndex: number, | ||
): Array<GItem & { active: boolean }> => { | ||
return items.map((item, index) => ({ | ||
...item, | ||
active: index === activeIndex, | ||
})); | ||
}; | ||
|
||
interface CalculateNewIndexFromArrowPressParams { | ||
/** | ||
* Whether the arrow key was the up key or the down key | ||
*/ | ||
direction: 'up' | 'down'; | ||
|
||
/** | ||
* The total number of matches | ||
*/ | ||
matchLength: number; | ||
|
||
/** | ||
* The previously matched index | ||
*/ | ||
prevIndex: number; | ||
} | ||
export const calculateNewIndexFromArrowPress = ({ | ||
direction, | ||
matchLength, | ||
prevIndex, | ||
}: CalculateNewIndexFromArrowPressParams) => | ||
direction === 'down' | ||
? prevIndex + 1 > matchLength - 1 | ||
? 0 | ||
: prevIndex + 1 | ||
: prevIndex - 1 < 0 | ||
? matchLength - 1 | ||
: prevIndex - 1; |