sort folders by name first and then sequence (#5063)

* sort folders by name first and then sequence
---------

Co-authored-by: lohit <lohit@usebruno.com>
This commit is contained in:
lohit
2025-07-14 22:17:11 +05:30
committed by GitHub
parent 31e555812c
commit 4e4c94d73f
13 changed files with 537 additions and 94 deletions

View File

@@ -346,7 +346,7 @@ const sortCollection = (collection) => {
let folderItems = filter(items, (item) => item.type === 'folder');
let requestItems = filter(items, (item) => item.type !== 'folder');
folderItems = folderItems.sort((a, b) => a.seq - b.seq);
folderItems = sortByNameThenSequence(folderItems);
requestItems = requestItems.sort((a, b) => a.seq - b.seq);
collection.items = folderItems.concat(requestItems);
@@ -361,7 +361,7 @@ const sortFolder = (folder = {}) => {
let folderItems = filter(items, (item) => item.type === 'folder');
let requestItems = filter(items, (item) => item.type !== 'folder');
folderItems = folderItems.sort((a, b) => a.seq - b.seq);
folderItems = sortByNameThenSequence(folderItems);
requestItems = requestItems.sort((a, b) => a.seq - b.seq);
folder.items = folderItems.concat(requestItems);
@@ -467,6 +467,47 @@ const mergeAuth = (collection, request, requestTreePath) => {
}
};
const sortByNameThenSequence = items => {
const isSeqValid = seq => Number.isFinite(seq) && Number.isInteger(seq) && seq > 0;
// Sort folders alphabetically by name
const alphabeticallySorted = [...items].sort((a, b) => a.name && b.name && a.name.localeCompare(b.name));
// Extract folders without 'seq'
const withoutSeq = alphabeticallySorted.filter(f => !isSeqValid(f['seq']));
// Extract folders with 'seq' and sort them by 'seq'
const withSeq = alphabeticallySorted.filter(f => isSeqValid(f['seq'])).sort((a, b) => a.seq - b.seq);
const sortedItems = withoutSeq;
// Insert folders with 'seq' at their specified positions
withSeq.forEach((item) => {
const position = item.seq - 1;
const existingItem = withoutSeq[position];
// Check if there's already an item with the same sequence number
const hasItemWithSameSeq = Array.isArray(existingItem)
? existingItem[0].seq === item.seq
: existingItem?.seq === item.seq;
if (hasItemWithSameSeq) {
// If there's a conflict, group items with same sequence together
const newGroup = Array.isArray(existingItem)
? [...existingItem, item]
: [existingItem, item];
withoutSeq.splice(position, 1, newGroup);
} else {
// Insert item at the specified position
withoutSeq.splice(position, 0, item);
}
});
// return flattened sortedItems
return sortedItems.flat();
};
module.exports = {
mergeHeaders,
mergeVars,
@@ -487,5 +528,6 @@ module.exports = {
sortFolder,
getAllRequestsInFolderRecursively,
getEnvVars,
getFormattedCollectionOauth2Credentials
getFormattedCollectionOauth2Credentials,
sortByNameThenSequence
};