Files
lucent-laravel/front/js/svelte/edges/sortEdges.js
T

21 lines
743 B
JavaScript
Raw Normal View History

2024-03-25 21:26:21 +02:00
export function sortByField(from, to, queryRecords, fieldName) {
2023-10-02 23:10:49 +03:00
if (from === to) {
2024-03-25 21:26:21 +02:00
return queryRecords;
2023-10-02 23:10:49 +03:00
}
2024-03-25 21:26:21 +02:00
let edgesTosort = queryRecords?.filter((qr) => qr.edge.field === fieldName && qr.edge.depth === 1 ) ?? [];
let remainingEdge = queryRecords?.filter((qr) => !(qr.edge.field === fieldName && qr.edge.depth === 1)) ?? [];
2023-10-02 23:10:49 +03:00
2023-10-30 14:33:35 +02:00
edgesTosort = array_move(edgesTosort,from, to);
2023-10-02 23:10:49 +03:00
return [...remainingEdge, ...edgesTosort];
}
2023-10-30 14:33:35 +02:00
function array_move(arr, old_index, new_index) {
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]);
return arr; // for testing
};