-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataList.model.ts
46 lines (38 loc) · 1.3 KB
/
dataList.model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { PaginatedDataDto } from '@/dtos/paginatedData.dto'
import { Data } from '@/interfaces/data.interface'
class DataList {
private data: Data[]
constructor(data: Data[]) {
this.data = [...data]
}
filterBySearch(keyword: string): DataList {
if (keyword) {
this.data = this.data.filter(item => item.name.toLowerCase().includes(keyword.toLowerCase()))
}
return this
}
filterByStatus(status: string): DataList {
if (status) {
this.data = this.data.filter(item => item.status.toLowerCase() === status.toLowerCase())
}
return this
}
sortByKey(key: string, order: 'asc' | 'desc'): DataList {
if (key) {
this.data.sort((a, b) => ((order === 'asc' ? a[key] > b[key] : a[key] < b[key]) ? 1 : -1))
}
return this
}
paginate(page: number, limit: number): PaginatedDataDto {
const totalItems = this.data.length
const totalPages = Math.ceil(totalItems / limit)
const currentPage = Math.min(page, totalPages) // Ensure currentPage does not exceed totalPages
const startIndex = (currentPage - 1) * limit
const paginatedData = this.data.slice(startIndex, startIndex + limit)
return new PaginatedDataDto(paginatedData, totalItems, totalPages, currentPage)
}
getData(): Data[] {
return this.data
}
}
export default DataList