Last active
January 17, 2026 16:43
-
-
Save Skateside/13e0f65cf9208b739d2cf5e1e2f64f50 to your computer and use it in GitHub Desktop.
Justify every early return
This file contains hidden or 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
| /** | |
| * Swaps the items in the array at `indexA` and `indexB`. If either `indexA` or | |
| * `indexB` are less than `0`, more-than-or-equal-to the length of `array`, or | |
| * not an integer, then no action is taken. | |
| * | |
| * @param array Array whose items should be swapped. | |
| * @param indexA Index of one item to swap. | |
| * @param indexB Index of the other item to swap. | |
| */ | |
| const swap = (array: any[], indexA: number, indexB: number) => { | |
| if (indexA === indexB) { | |
| return; // no need to swap, items already in desired place. | |
| } | |
| const { length } = array; | |
| if ( | |
| indexA < 0 | |
| || indexA >= length | |
| || Math.trunc(indexA) !== indexA | |
| || indexB < 0 | |
| || indexB >= length | |
| || Math.trunc(indexB) !== indexB | |
| ) { | |
| return console.warn( | |
| "can't swap, indices make no sense", | |
| { indexA, indexB, length }, | |
| ); | |
| } | |
| [array[indexA], array[indexB]] = [array[indexB], array[indexA]]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment