Skip to content

refactor: remove collection to constructor #19

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

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The documentation is available on the [Website](https://adityadarma.github.io/ad

## License

Adonis Datatables is open-sourced software licensed under the [MIT license](LICENSE.md).
This package is open-sourced software licensed under the [MIT license](LICENSE.md).

[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adityadarma/adonis-datatables/release.yml?style=for-the-badge
[gh-workflow-url]: https://github.com/adityadarma/adonis-datatables/actions/workflows/release.yml 'Github action'
Expand Down
87 changes: 41 additions & 46 deletions src/engines/object_datatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,16 @@ import Helper from '../utils/helper.js'

export default class ObjectDataTable extends DataTableAbstract {
protected $offset: number = 0
protected collection: Collection<any> = collect()

constructor(object: Record<string, any>[]) {
constructor(protected items: Record<string, any>[]) {
super()
this.collection = new Collection(object)
this.$columns = this.collection.keys()
this.$columns = collect(this.items).keys()
}

static canCreate(source: any): boolean {
return typeof source === 'object' || source instanceof Collection
}

static create<T>(this: new (source: any) => T, source: any): T {
if (!(source instanceof Collection)) {
source = new Collection(source)
}

return super.create<T>(source)
}

protected resolveCallback() {
return this
}
Expand All @@ -34,7 +24,7 @@ export default class ObjectDataTable extends DataTableAbstract {
const orderable = this.request.orderableColumns()

if (orderable.length) {
this.collection = this.collection
this.items = collect(this.items)
.map((data) => Helper.dot(data))
.sort((a: Record<string, any>, b: Record<string, any>) => {
for (const value of Object.values(orderable)) {
Expand Down Expand Up @@ -83,6 +73,7 @@ export default class ObjectDataTable extends DataTableAbstract {

return data
})
.all()
}
}

Expand All @@ -92,7 +83,7 @@ export default class ObjectDataTable extends DataTableAbstract {
const index = this.$dataObject ? indexColumn : 0
let start = this.request.start()

this.collection.transform((data) => {
collect(this.items).transform((data) => {
data[index] = ++start

return data
Expand All @@ -101,7 +92,7 @@ export default class ObjectDataTable extends DataTableAbstract {
}

async count(): Promise<number> {
return this.collection.count()
return collect(this.items).count()
}

async results(): Promise<Record<string, any> | void> {
Expand All @@ -122,22 +113,22 @@ export default class ObjectDataTable extends DataTableAbstract {
}
)

this.collection = collect(output)
this.items = collect(output).all()
this.ordering()
await this.filterRecords()
this.paginate()

this.revertIndexColumn()
}

return this.render(this.collection.all())
return this.render(collect(this.items).all())
} catch (error) {
return this.errorResponse(error)
}
}

dataResults(): any {
return this.collection.all()
return collect(this.items).all()
}

setOffset(offset: number): this {
Expand All @@ -162,52 +153,56 @@ export default class ObjectDataTable extends DataTableAbstract {
const regex = this.request.isRegex(i)
const keyword = this.request.columnKeyword(i)

this.collection = this.collection.filter((row: Record<string, any>) => {
const value = lodash.get(row, column)
this.items = collect(this.items)
.filter((row: Record<string, any>) => {
const value = lodash.get(row, column)

if (self.config.isCaseInsensitive()) {
if (regex) {
return new RegExp(keyword, 'i').test(value)
}
if (self.config.isCaseInsensitive()) {
if (regex) {
return new RegExp(keyword, 'i').test(value)
}

return Helper.contains(value.toLowerCase(), keyword.toLowerCase())
}
return Helper.contains(value.toLowerCase(), keyword.toLowerCase())
}

if (regex) {
return new RegExp(keyword).test(value)
}
if (regex) {
return new RegExp(keyword).test(value)
}

return Helper.contains(value, keyword)
})
return Helper.contains(value, keyword)
})
.all()
}
}

paging(): void {
const offset = this.request.start() - this.$offset
const length = this.request.length() > 0 ? this.request.length() : 10

this.collection = this.collection.slice(offset, length)
this.items = collect(this.items).slice(offset, length).all()
}

globalSearch(keyword: string): void {
keyword = this.config.isCaseInsensitive() ? keyword.toLowerCase() : keyword

this.collection = this.collection.filter((row: any) => {
for (const index of Object.values(this.request.searchableColumnIndex())) {
const column = this.getColumnName(index) as string
let value = lodash.get(row, column)
if (typeof value !== 'string') {
continue
} else {
value = this.config.isCaseInsensitive() ? value.toLowerCase() : value
}
this.items = collect(this.items)
.filter((row: any) => {
for (const index of Object.values(this.request.searchableColumnIndex())) {
const column = this.getColumnName(index) as string
let value = lodash.get(row, column)
if (typeof value !== 'string') {
continue
} else {
value = this.config.isCaseInsensitive() ? value.toLowerCase() : value
}

if (Helper.contains(value, keyword)) {
return true
if (Helper.contains(value, keyword)) {
return true
}
}
}

return false
})
return false
})
.all()
}
}
Loading