Merge branch 'master' into 74-integration-with-bookmarks-core-plugin

# Conflicts:
#	src/custom-sort/custom-sort.ts
This commit is contained in:
SebastianMC 2023-08-29 00:21:47 +02:00
commit a3190cff62
2 changed files with 17 additions and 21 deletions

View File

@ -2276,7 +2276,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabetical', () => {
expect(result1).toBe(SORT_FIRST_GOES_EARLIER)
expect(result2).toBe(SORT_FIRST_GOES_LATER)
})
it('should correctly fallback to alphabetical by name when metadata on both items is present and equal', () => {
it('should correctly compare when metadata on both items is present and equal', () => {
// given
const itemA: Partial<FolderItemForSorting> = {
metadataFieldValue: 'Aaa',
@ -2294,9 +2294,9 @@ describe('CustomSortOrder.byMetadataFieldAlphabetical', () => {
const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting)
// then
expect(result1).toBe(SORT_FIRST_GOES_LATER)
expect(result2).toBe(SORT_FIRST_GOES_EARLIER)
expect(result3).toBe(SORT_ITEMS_ARE_EQUAL)
expect(result1).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result2).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result3).toBe(EQUAL_OR_UNCOMPARABLE)
})
it('should put the item with metadata earlier if the second one has no metadata ', () => {
// given
@ -2358,7 +2358,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => {
expect(result1).toBe(SORT_FIRST_GOES_LATER)
expect(result2).toBe(SORT_FIRST_GOES_EARLIER)
})
it('should correctly fallback to alphabetical reverse by name when metadata on both items is present and equal', () => {
it('should correctly compare when metadata on both items is present and equal', () => {
// given
const itemA: Partial<FolderItemForSorting> = {
metadataFieldValue: 'Aaa',
@ -2376,9 +2376,9 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => {
const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting)
// then
expect(result1).toBe(SORT_FIRST_GOES_EARLIER)
expect(result2).toBe(SORT_FIRST_GOES_LATER)
expect(result3).toBe(SORT_ITEMS_ARE_EQUAL)
expect(result1).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result2).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result3).toBe(EQUAL_OR_UNCOMPARABLE)
})
it('should put the item with metadata later if the second one has no metadata (reverse order)', () => {
// given
@ -2425,9 +2425,9 @@ describe('sorterByMetadataField', () => {
it.each([
[true,'abc','def',-1, 'a', 'a'],
[true,'xyz','klm',1, 'b', 'b'],
[true,'mmm','mmm',0, 'c', 'c'],
[true,'mmm','mmm',-1, 'd', 'e'],
[true,'mmm','mmm',1, 'e', 'd'],
[true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'c', 'c'],
[true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'd', 'e'],
[true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'e', 'd'],
[true,'abc',undefined,-1, 'a','a'],
[true,undefined,'klm',1, 'b','b'],
[true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'],
@ -2435,9 +2435,9 @@ describe('sorterByMetadataField', () => {
[true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'd','c'],
[false,'abc','def',1, 'a', 'a'],
[false,'xyz','klm',-1, 'b', 'b'],
[false,'mmm','mmm',0, 'c', 'c'],
[false,'mmm','mmm',1, 'd', 'e'],
[false,'mmm','mmm',-1, 'e', 'd'],
[false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'c', 'c'],
[false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'd', 'e'],
[false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'e', 'd'],
[false,'abc',undefined,1, 'a','a'],
[false,undefined,'klm',-1, 'b','b'],
[false,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'],

View File

@ -5,7 +5,8 @@ import {
requireApiVersion,
TAbstractFile,
TFile,
TFolder} from 'obsidian';
TFolder
} from 'obsidian';
import {
determineStarredStatusOf,
Starred_PluginInstance
@ -86,12 +87,7 @@ export const sorterByMetadataField:(reverseOrder?: boolean, trueAlphabetical?: b
}
if (a.metadataFieldValue && b.metadataFieldValue) {
const sortResult: number = collatorCompareFn(a.metadataFieldValue, b.metadataFieldValue)
if (sortResult === 0) {
// Fallback -> requested sort by metadata and both items have the same metadata value
return collatorCompareFn(a.sortString, b.sortString) // switch to alphabetical sort by note/folder titles
} else {
return sortResult
}
return sortResult
}
// Item with metadata goes before the w/o metadata
if (a.metadataFieldValue) return -1