Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add utility method to swap array element inline

Tianzhou Chen88babdbf28a7
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index fb3fe503ef..a9acbf613a 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -18,3 +18,22 @@ export function urlfy(str: string) {
}
return result;
}
+
+// Performs inline swap, also handles negative index (counting from the end)
+// array_swap([1, 2, 3, 4], 1, 2) => [1, 3, 2, 4]
+// array_swap([1, 2, 3, 4], -1, -2) => [1, 2, 4, 3]
+export function array_swap(arr: any[], old_index: number, new_index: number) {
+ while (old_index < 0) {
+ old_index += arr.length;
+ }
+ while (new_index < 0) {
+ new_index += arr.length;
+ }
+ if (new_index >= arr.length) {
+ var k = new_index - arr.length + 1;
+ while (k--) {
+ arr.push(undefined);
+ }
+ }
+ arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
+}