Skip to content

Commit

Permalink
feat(🆕): reorder() (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Oct 9, 2020
1 parent b0412a9 commit 12f25fe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Array.ts
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;
};
6 changes: 6 additions & 0 deletions src/__tests__/Array.test.ts
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]);
});
1 change: 1 addition & 0 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import "./Coordinates.test";
import "./Math.test";
import "./Physics.test";
import "./Paths.test";
import "./Array.test";
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from "./Vectors";
export * from "./Colors";
export * from "./Paths";
export * from "./Physics";
export * from "./Array";
export { default as ReText } from "./ReText";

0 comments on commit 12f25fe

Please sign in to comment.