mirror of
https://github.com/tcgdex/cards-database.git
synced 2025-04-23 03:12:10 +00:00
fix: better sorting defaults
Signed-off-by: Avior <github@avior.me>
This commit is contained in:
parent
28fcb66fc9
commit
8fd7afeb32
@ -109,10 +109,12 @@ export function handleSort(data: Array<any>, query: Query<any>) {
|
|||||||
if (data.length === 0) {
|
if (data.length === 0) {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultSortPriority = ['releaseDate', 'localId', 'id']
|
||||||
|
|
||||||
const firstEntry = data[0]
|
const firstEntry = data[0]
|
||||||
const sort: Query<any>['sort'] = query.sort ?? {field: 'releaseDate' in firstEntry ? 'releaseDate' : 'id', order: 'ASC'}
|
const field = query.sort?.field ?? defaultSortPriority.find((it) => it in firstEntry) ?? 'id'
|
||||||
const field = sort.field
|
const order = query.sort?.order ?? 'ASC'
|
||||||
const order = sort.order ?? 'ASC'
|
|
||||||
|
|
||||||
// early exit if the order is not correctly set
|
// early exit if the order is not correctly set
|
||||||
if (order !== 'ASC' && order !== 'DESC') {
|
if (order !== 'ASC' && order !== 'DESC') {
|
||||||
@ -121,22 +123,43 @@ export function handleSort(data: Array<any>, query: Query<any>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(field in firstEntry)) {
|
if (!(field in firstEntry)) {
|
||||||
|
console.warn('can\'t sort using the field', field)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
const sortType = typeof data[0][field]
|
|
||||||
if (sortType === 'number') {
|
return data.sort((a, b) => advSort(a[field], b[field], order))
|
||||||
if (order === 'ASC') {
|
}
|
||||||
return data.sort((a, b) => a[field] - b[field])
|
|
||||||
} else {
|
/**
|
||||||
return data.sort((a, b) => b[field] - a[field])
|
*
|
||||||
|
* @param order the base ordering
|
||||||
|
* @returns a function that is feed in the `sort` function
|
||||||
|
*/
|
||||||
|
const advSort = (a: string | number, b: string | number, order: 'ASC' | 'DESC' = 'ASC') => {
|
||||||
|
a = tryParse(a) ?? a
|
||||||
|
b = tryParse(b) ?? b
|
||||||
|
|
||||||
|
if (order === 'DESC') {
|
||||||
|
const tmp = a
|
||||||
|
a = b
|
||||||
|
b = tmp
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (order === 'ASC') {
|
if (typeof a === 'number' && typeof b === 'number') {
|
||||||
return data.sort((a, b) => a[field] > b[field] ? 1 : -1)
|
return a - b
|
||||||
} else {
|
|
||||||
return data.sort((a, b) => a[field] > b[field] ? -1 : 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return a.toString().localeCompare(b.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
function tryParse(value: string | number): number | null {
|
||||||
|
if (typeof value === 'number') {
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
|
if (/^-?\d+$/.test(value)) {
|
||||||
|
return parseInt(value)
|
||||||
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user