-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
1 parent
b0412a9
commit 12f25fe
Showing
4 changed files
with
26 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,18 @@ | ||
export const reorder = (input: unknown[], from: number, to: number) => { | ||
"worklet"; | ||
const offsets = [...input]; | ||
while (from < 0) { | ||
from += offsets.length; | ||
} | ||
while (to < 0) { | ||
to += offsets.length; | ||
} | ||
if (to >= offsets.length) { | ||
let k = to - offsets.length; | ||
while (k-- + 1) { | ||
offsets.push(); | ||
} | ||
} | ||
offsets.splice(to, 0, offsets.splice(from, 1)[0]); | ||
return offsets; | ||
}; |
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,6 @@ | ||
import { reorder } from "../Array"; | ||
|
||
test("reorder()", () => { | ||
expect(reorder([0, 1, 2, 3, 4], 2, 0)).toStrictEqual([2, 0, 1, 3, 4]); | ||
expect(reorder([0, 1, 2, 3, 4], 3, 2)).toStrictEqual([0, 1, 3, 2, 4]); | ||
}); |
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