1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-22 10:52:10 +00:00

fix: Ordering failing when one value is null (#490)

This commit is contained in:
Florian Bouillon 2024-05-19 01:32:23 +02:00 committed by GitHub
parent 16fe072e7e
commit d48971c95e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 4 deletions

View File

@ -0,0 +1,17 @@
meta {
name: 489 - Crash When sorting with Null values
type: http
seq: 6
}
get {
url: {{BASE_URL}}/v2/en/cards?name=pikachu&sort:field=hp&sort:order=DESC
body: none
auth: none
}
query {
name: pikachu
sort:field: hp
sort:order: DESC
}

1
server/cards-database Submodule

@ -0,0 +1 @@
Subproject commit c476d826181afdd121cc80331abfa94d5f06785e

View File

@ -150,9 +150,21 @@ export function handleSort(data: Array<any>, query: Query<any>) {
* @param order the base ordering * @param order the base ordering
* @returns a function that is feed in the `sort` function * @returns a function that is feed in the `sort` function
*/ */
const advSort = (a: string | number, b: string | number, order: 'ASC' | 'DESC' = 'ASC') => { const advSort = (a?: string | number, b?: string | number, order: 'ASC' | 'DESC' = 'ASC') => {
a = tryParse(a) ?? a const isANull = isNull(a)
b = tryParse(b) ?? b const isBNull = isNull(b)
if (isANull && isBNull) {
return 0
}
if (isANull) {
return order === 'ASC' ? -1 : 1
}
if (isBNull) {
return order === 'ASC' ? 1 : -1
}
a = tryParse(a!) ?? a
b = tryParse(b!) ?? b
if (order === 'DESC') { if (order === 'DESC') {
const tmp = a const tmp = a
@ -164,7 +176,7 @@ const advSort = (a: string | number, b: string | number, order: 'ASC' | 'DESC' =
return a - b return a - b
} }
return a.toString().localeCompare(b.toString()) return a!.toString().localeCompare(b!.toString())
} }
function tryParse(value: string | number): number | null { function tryParse(value: string | number): number | null {