mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { Comparator } from "./comparator.type";
|
|
|
|
export type SorterOptions<T> = {
|
|
defaultKey: string;
|
|
comparators: Record<string, Comparator<T>>;
|
|
tiebreak: Comparator<T>;
|
|
}
|
|
|
|
export class Sorter<T> {
|
|
public readonly defaultKey: string;
|
|
|
|
private readonly comparators: Record<string, Comparator<T>>;
|
|
private readonly tiebreak: Comparator<T>;
|
|
|
|
constructor(options: SorterOptions<T>) {
|
|
this.defaultKey = options.defaultKey;
|
|
this.comparators = options.comparators;
|
|
this.tiebreak = options.tiebreak;
|
|
}
|
|
|
|
public getComparatorKeys(): string[] {
|
|
return Object.keys(this.comparators);
|
|
}
|
|
|
|
public getDefaultComparator(): Comparator<T> {
|
|
return this.comparators[this.defaultKey];
|
|
}
|
|
|
|
public getComparator(key: string): Comparator<T> {
|
|
const comparator = this.comparators[key] || this.comparators[this.defaultKey];
|
|
return this.tiebreak === comparator
|
|
? comparator
|
|
: (object1, object2) => comparator(object1, object2) || this.tiebreak(object1, object2);
|
|
}
|
|
|
|
}
|