1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-23 19:32:11 +00:00

fix: set order not following the old way (#465)

This commit is contained in:
Florian Bouillon 2024-01-03 12:16:57 +01:00 committed by GitHub
parent ef23029d24
commit b4dbdef4fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,12 +63,13 @@ export function betterSorter(a: string, b: string) {
*/ */
export function validateItem(validator: any | Array<any>, value: any): boolean { export function validateItem(validator: any | Array<any>, value: any): boolean {
if (typeof value === 'object') { if (typeof value === 'object') {
return objectLoop(value, (v) => { // invert signal so that an early exit mean that a match was found!
return !objectLoop(value, (v) => {
// early exit to not infinitively loop through objects // early exit to not infinitively loop through objects
if (typeof v === 'object') return true if (typeof v === 'object') return true
// check for each childs // check for each childs until one match
return validateItem(validator, v) return !validateItem(validator, v)
}) })
} }
@ -104,10 +105,14 @@ export function validateItem(validator: any | Array<any>, value: any): boolean {
* @returns the sorted data * @returns the sorted data
*/ */
export function handleSort(data: Array<any>, query: Query<any>) { export function handleSort(data: Array<any>, query: Query<any>) {
const sort: Query<any>['sort'] = query.sort ?? {field: 'id', order: 'ASC'} // handle when data has no entries
if (data.length === 0) {
return data
}
const firstEntry = data[0]
const sort: Query<any>['sort'] = query.sort ?? {field: 'releaseDate' in firstEntry ? 'releaseDate' : 'id', order: 'ASC'}
const field = sort.field const field = sort.field
const order = sort.order ?? 'ASC' const order = sort.order ?? 'ASC'
const firstEntry = data[0]
// 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') {
@ -142,7 +147,7 @@ export function handleSort(data: Array<any>, query: Query<any>) {
* @returns the data that is in the paginated query * @returns the data that is in the paginated query
*/ */
export function handlePagination(data: Array<any>, query: Query<any>) { export function handlePagination(data: Array<any>, query: Query<any>) {
if (!query.pagination) { if (!query.pagination || data.length === 0) {
return data return data
} }
const itemsPerPage = query.pagination.itemsPerPage ?? 100 const itemsPerPage = query.pagination.itemsPerPage ?? 100