make sorting method static, remove sorting service injector

This commit is contained in:
Dan Percic 2021-08-06 14:41:45 +03:00
parent be233975d6
commit 7bd24c4352
2 changed files with 10 additions and 14 deletions

View File

@ -4,10 +4,8 @@ import { SortingOrder } from './models/sorting-order.type';
import { KeysOf } from '../utils/types/utility-types'; import { KeysOf } from '../utils/types/utility-types';
@Pipe({ name: 'sortBy' }) @Pipe({ name: 'sortBy' })
export class SortByPipe<T extends object> implements PipeTransform { export class SortByPipe implements PipeTransform {
constructor(private readonly _sortingService: SortingService<T>) {} transform<T extends object>(values: T[], order: SortingOrder, column: KeysOf<T>): T[] {
return SortingService.sort(values, order, column);
transform(value: T[], order: SortingOrder, column: KeysOf<T>): T[] {
return this._sortingService.sort(value, order, column);
} }
} }

View File

@ -5,9 +5,7 @@ import { SortingOption } from './models/sorting-option.model';
import { SortingOrder, SortingOrders } from './models/sorting-order.type'; import { SortingOrder, SortingOrders } from './models/sorting-order.type';
import { KeysOf } from '../utils/types/utility-types'; import { KeysOf } from '../utils/types/utility-types';
@Injectable({ @Injectable()
providedIn: 'root'
})
export class SortingService<T extends object> { export class SortingService<T extends object> {
private readonly _sortingOption$ = new BehaviorSubject<SortingOption<T> | undefined>(undefined); private readonly _sortingOption$ = new BehaviorSubject<SortingOption<T> | undefined>(undefined);
readonly sortingOption$ = this._sortingOption$.asObservable(); readonly sortingOption$ = this._sortingOption$.asObservable();
@ -16,11 +14,7 @@ export class SortingService<T extends object> {
return this._sortingOption$.getValue(); return this._sortingOption$.getValue();
} }
setSortingOption(value: SortingOption<T>): void { static sort<T extends object>(values: T[], order?: SortingOrder, column?: KeysOf<T>): T[] {
this._sortingOption$.next(value);
}
sort(values: T[], order?: SortingOrder, column?: KeysOf<T>): T[] {
if (!values || values.length <= 1 || !order) return values; if (!values || values.length <= 1 || !order) return values;
if (!column) { if (!column) {
@ -32,8 +26,12 @@ export class SortingService<T extends object> {
return orderBy(values, [column], [order]); return orderBy(values, [column], [order]);
} }
setSortingOption(value: SortingOption<T>): void {
this._sortingOption$.next(value);
}
defaultSort(values: T[]): T[] { defaultSort(values: T[]): T[] {
return this.sort(values, this.sortingOption?.order, this.sortingOption?.column); return SortingService.sort(values, this.sortingOption?.order, this.sortingOption?.column);
} }
toggleSort(column: KeysOf<T>): void { toggleSort(column: KeysOf<T>): void {