Skip to content

Commit

Permalink
fix: Deal with malformed docs
Browse files Browse the repository at this point in the history
A user, that shall remain nameless, experienced a broken search when
searching on the specific "test" string.
This is because some users really *love* testing edges cases with
malformed documents.
Thus, we now better protect such situations.
  • Loading branch information
paultranvan committed Dec 19, 2024
1 parent 4e8bfa2 commit c758aed
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/cozy-dataproxy-lib/src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ export class SearchEngine {
}

compareStrings(str1: string, str2: string): number {
if (!str1 && !str2) {
return 0
} else if (!str1) {
return 1
} else if (!str2) {
return -1
}
return str1.localeCompare(str2, undefined, { numeric: true })
}

Expand All @@ -412,13 +419,13 @@ export class SearchEngine {
isIOCozyApp(a.doc) &&
isIOCozyApp(b.doc)
) {
return this.compareStrings(a.doc.slug, b.doc.slug)
return this.compareStrings(a.doc?.slug, b.doc?.slug)
} else if (
a.doctype === CONTACTS_DOCTYPE &&
isIOCozyContact(a.doc) &&
isIOCozyContact(b.doc)
) {
return this.compareStrings(a.doc.displayName, b.doc.displayName)
return this.compareStrings(a.doc?.displayName, b.doc?.displayName)
} else if (
a.doctype === FILES_DOCTYPE &&
isIOCozyFile(a.doc) &&
Expand All @@ -444,7 +451,7 @@ export class SearchEngine {
return aRes.doc.type === 'directory' ? -1 : 1
}
// Then name
return this.compareStrings(aRes.doc.name, bRes.doc.name)
return this.compareStrings(aRes.doc?.name, bRes.doc?.name)
}

limitSearchResults(searchResults: RawSearchResult[]): RawSearchResult[] {
Expand Down

0 comments on commit c758aed

Please sign in to comment.