mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-12 13:53:03 -05:00
feat(challenge23): create table for testing table challenge
This commit is contained in:
1
libs/testing-table/backend/src/index.ts
Normal file
1
libs/testing-table/backend/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './lib/fake-backend.service';
|
||||
42
libs/testing-table/backend/src/lib/fake-backend.service.ts
Normal file
42
libs/testing-table/backend/src/lib/fake-backend.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SortDirection } from '@angular/material/sort';
|
||||
import { User, randUser } from '@ngneat/falso';
|
||||
import { map, take, timer } from 'rxjs';
|
||||
|
||||
type SortDirectionIndex = 1 | -1 | 0;
|
||||
|
||||
const sort: Record<SortDirection, SortDirectionIndex> = {
|
||||
asc: 1,
|
||||
desc: -1,
|
||||
'': 0,
|
||||
};
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class FakeBackendService {
|
||||
private readonly users = randUser({ length: 10 });
|
||||
|
||||
getUsers = (active: keyof User, dir: SortDirection, pageIndex: number) =>
|
||||
timer(0, 1000).pipe(
|
||||
take(1),
|
||||
map(() => this.sortByKey(this.users, active, sort[dir]))
|
||||
);
|
||||
|
||||
private sortByKey(
|
||||
arr: User[],
|
||||
key: keyof User,
|
||||
direction: SortDirectionIndex
|
||||
): User[] {
|
||||
return arr.sort((a, b) => {
|
||||
const valueA = a[key];
|
||||
const valueB = b[key];
|
||||
|
||||
if (valueA < valueB) {
|
||||
return -1 * direction;
|
||||
} else if (valueA > valueB) {
|
||||
return 1 * direction;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user