Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set order not following the old way #465

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions server/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ export function betterSorter(a: string, b: string) {
*/
export function validateItem(validator: any | Array<any>, value: any): boolean {
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
if (typeof v === 'object') return true

// check for each childs
return validateItem(validator, v)
// check for each childs until one match
return !validateItem(validator, v)
})
}

Expand Down Expand Up @@ -104,10 +105,14 @@ export function validateItem(validator: any | Array<any>, value: any): boolean {
* @returns the sorted data
*/
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 order = sort.order ?? 'ASC'
const firstEntry = data[0]

// early exit if the order is not correctly set
if (order !== 'ASC' && order !== 'DESC') {
Expand Down Expand Up @@ -142,7 +147,7 @@ export function handleSort(data: Array<any>, query: Query<any>) {
* @returns the data that is in the paginated query
*/
export function handlePagination(data: Array<any>, query: Query<any>) {
if (!query.pagination) {
if (!query.pagination || data.length === 0) {
return data
}
const itemsPerPage = query.pagination.itemsPerPage ?? 100
Expand Down