From 9c3314ce4756dc1589f6e543fa5e056e4038251a Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Tue, 15 Jul 2025 18:55:34 +0530 Subject: [PATCH 1/2] folder sequencing sort by name and then sequence --- packages/bruno-cli/src/utils/collection.js | 44 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/bruno-cli/src/utils/collection.js b/packages/bruno-cli/src/utils/collection.js index 4b698ced9..f68b0033b 100644 --- a/packages/bruno-cli/src/utils/collection.js +++ b/packages/bruno-cli/src/utils/collection.js @@ -55,7 +55,7 @@ const createCollectionJsonFromPathname = (collectionPath) => { } } let currentDirFolderItems = currentDirItems?.filter((iter) => iter.type === 'folder'); - let sortedFolderItems = currentDirFolderItems?.sort((a, b) => a.seq - b.seq); + let sortedFolderItems = sortByNameThenSequence(currentDirFolderItems); let currentDirRequestItems = currentDirItems?.filter((iter) => iter.type !== 'folder'); let sortedRequestItems = currentDirRequestItems?.sort((a, b) => a.seq - b.seq); @@ -512,6 +512,48 @@ const processCollectionItems = async (items = [], currentPath) => { } }; +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 = { createCollectionJsonFromPathname, mergeHeaders, From 5c47e1f405e291e035fa8b88d5f4cb9cb845c058 Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Tue, 15 Jul 2025 19:10:19 +0530 Subject: [PATCH 2/2] updated validations --- packages/bruno-app/src/utils/common/index.js | 2 +- packages/bruno-cli/src/utils/collection.js | 2 +- packages/bruno-electron/src/utils/collection.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index 6402d5a1e..1c647ae28 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -242,7 +242,7 @@ export const sortByNameThenSequence = items => { // Check if there's already an item with the same sequence number const hasItemWithSameSeq = Array.isArray(existingItem) - ? existingItem[0].seq === item.seq + ? existingItem?.[0]?.seq === item.seq : existingItem?.seq === item.seq; if (hasItemWithSameSeq) { diff --git a/packages/bruno-cli/src/utils/collection.js b/packages/bruno-cli/src/utils/collection.js index f68b0033b..09d78506c 100644 --- a/packages/bruno-cli/src/utils/collection.js +++ b/packages/bruno-cli/src/utils/collection.js @@ -533,7 +533,7 @@ const sortByNameThenSequence = items => { // Check if there's already an item with the same sequence number const hasItemWithSameSeq = Array.isArray(existingItem) - ? existingItem[0].seq === item.seq + ? existingItem?.[0]?.seq === item.seq : existingItem?.seq === item.seq; if (hasItemWithSameSeq) { diff --git a/packages/bruno-electron/src/utils/collection.js b/packages/bruno-electron/src/utils/collection.js index dc330dc97..c7120779e 100644 --- a/packages/bruno-electron/src/utils/collection.js +++ b/packages/bruno-electron/src/utils/collection.js @@ -489,7 +489,7 @@ const sortByNameThenSequence = items => { // Check if there's already an item with the same sequence number const hasItemWithSameSeq = Array.isArray(existingItem) - ? existingItem[0].seq === item.seq + ? existingItem?.[0]?.seq === item.seq : existingItem?.seq === item.seq; if (hasItemWithSameSeq) {