Skip to content

Commit 8e6c221

Browse files
committed
#216 - bugfix in sorting by metadata when one or both metadata values are boolean
1 parent 59e8090 commit 8e6c221

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/custom-sort/custom-sort.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ export const sorterByMetadataField = (reverseOrder?: boolean, trueAlphabetical?:
105105
if (reverseOrder) {
106106
[amdata, bmdata] = [bmdata, amdata]
107107
}
108-
if (amdata && bmdata) {
108+
if (amdata!==undefined && bmdata!==undefined) {
109109
const sortResult: number = collatorCompareFn(amdata, bmdata)
110110
return sortResult
111111
}
112112
// Item with metadata goes before the w/o metadata
113-
if (amdata) return reverseOrder ? 1 : -1
114-
if (bmdata) return reverseOrder ? -1 : 1
113+
if (amdata!==undefined) return reverseOrder ? 1 : -1
114+
if (bmdata!==undefined) return reverseOrder ? -1 : 1
115115

116116
return EQUAL_OR_UNCOMPARABLE
117117
}

src/test/unit/custom-sort.spec.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => {
26212621
})
26222622
})
26232623

2624-
describe('sorterByMetadataField', () => {
2624+
describe('sorterByMetadataField - string metadata', () => {
26252625
it.each([
26262626
[true,'abc','def',-1, 'a', 'a'],
26272627
[true,'xyz','klm',1, 'b', 'b'],
@@ -2656,6 +2656,51 @@ describe('sorterByMetadataField', () => {
26562656
})
26572657
})
26582658

2659+
describe('sorterByMetadataField - boolean metadata', () => {
2660+
const ASC = true
2661+
const DESC = false
2662+
it.each([
2663+
[ASC,true,false,1, 'a', 'a'],
2664+
[ASC,false,true,-1, 'a', 'a'],
2665+
[DESC,true,false,-1, 'a', 'a'],
2666+
[DESC,false,true,1, 'a', 'a'],
2667+
[ASC,true,undefined,-1, 'a', 'a'],
2668+
[ASC,false,undefined,-1, 'a', 'a'],
2669+
[DESC,true,undefined,-1, 'a', 'a'],
2670+
[DESC,false,undefined,-1, 'a', 'a'],
2671+
[ASC,undefined,true,1, 'a', 'a'],
2672+
[ASC,undefined,false,1, 'a', 'a'],
2673+
[DESC,undefined,true,1, 'a', 'a'],
2674+
[DESC,undefined,false,1, 'a', 'a'],
2675+
[ASC,true,true,EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2676+
[ASC,false,false,EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2677+
[DESC,true,true,EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2678+
[DESC,false,false,EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2679+
[ASC,true,'false',1, 'a', 'a'],
2680+
[ASC,'false',true,-1, 'a', 'a'],
2681+
[DESC,true,'false',-1, 'a', 'a'],
2682+
[DESC,'false',true,1, 'a', 'a'],
2683+
[ASC,'true',false,1, 'a', 'a'],
2684+
[ASC,false,'true',-1, 'a', 'a'],
2685+
[DESC,'true',false,-1, 'a', 'a'],
2686+
[DESC,false,'true',1, 'a', 'a'],
2687+
[ASC,true,'true',EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2688+
[ASC,'false',false,EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2689+
[DESC,'true',true,EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2690+
[DESC,false,'false',EQUAL_OR_UNCOMPARABLE, 'a', 'b'],
2691+
2692+
])('straight order %s, comparing %s and %s should return %s for sortStrings %s and %s',
2693+
(straight: boolean, metadataA: boolean|string|undefined, metadataB: boolean|string|undefined, order: number, sortStringA: string, sortStringB) => {
2694+
const sorterFn = sorterByMetadataField(!straight, false)
2695+
const itemA: Partial<FolderItemForSorting> = {metadataFieldValue: metadataA as any, sortString: sortStringA}
2696+
const itemB: Partial<FolderItemForSorting> = {metadataFieldValue: metadataB as any, sortString: sortStringB}
2697+
const result = sorterFn(itemA as FolderItemForSorting, itemB as FolderItemForSorting)
2698+
2699+
// then
2700+
expect(result).toBe(order)
2701+
})
2702+
})
2703+
26592704
describe('sorterByBookmarkOrder', () => {
26602705
it.each([
26612706
[true,10,20,-1, 'a', 'a'],

0 commit comments

Comments
 (0)