All files / app/search/presenter/pages/github-search/components/molecules/results-table results-table-datasource.ts

82.14% Statements 23/28
63.15% Branches 12/19
90.9% Functions 10/11
82.14% Lines 23/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                              13x 13x       2x       2x   2x   3x           3x       3x       2x       2x   2x           5x 2x 2x         3x 1x     2x 4x   2x 2x   2x       2x         2x      
import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { map } from 'rxjs/operators';
import { Observable, merge, Subscription } from 'rxjs';
import { GithubUser } from 'src/app/search/domain/entities/github-user';
import { GithubSearchController } from '../../../github-search.controller';
import { GithubSearchResult } from 'src/app/search/domain/entities/github-search-result';
 
export class ResultsTableDataSource extends DataSource<GithubUser> {
  paginator: MatPaginator | undefined;
  sort: MatSort | undefined;
 
  paginatorSubscription: Subscription | undefined;
 
  constructor(private controller: GithubSearchController) {
    super();
  }
 
  connect(): Observable<GithubUser[]> {
    Iif (!this.sort) {
      throw Error('Please set the sort on the data source before connecting.');
    }
 
    this.subscribeForPaginatorChanges();
 
    return merge(this.controller.dataState$, this.sort.sortChange).pipe(
      map(() => {
        return this.getSortedData(this.controller.dataState$.value.data);
      })
    );
  }
 
  disconnect(): void {
    Iif (!this.paginator) {
      return;
    }
 
    this.unsubscribeForPaginatorChanges();
  }
 
  subscribeForPaginatorChanges() {
    Iif (!this.paginator) {
      return;
    }
 
    this.unsubscribeForPaginatorChanges();
 
    this.paginatorSubscription = this.paginator.page.subscribe((page) => {
      this.controller.setPage(page.pageIndex);
    });
  }
 
  unsubscribeForPaginatorChanges() {
    if (this.paginatorSubscription) {
      this.paginatorSubscription.unsubscribe();
      this.paginatorSubscription = undefined;
    }
  }
 
  private getSortedData(result: GithubSearchResult): GithubUser[] {
    if (!this.sort || !this.sort.active || this.sort.direction === '') {
      return result.items;
    }
 
    return result.items
      .map((item) => item)
      .sort((a: any, b: any) => {
        const activeProperty = this.sort?.active;
        const isAsc = this.sort?.direction === 'asc';
 
        Iif (!activeProperty) {
          return 0;
        }
 
        return this.compare(a[activeProperty], b[activeProperty], isAsc);
      });
  }
 
  private compare(a: string, b: string, isAsc: boolean): number {
    return new Intl.Collator().compare(a, b) * (isAsc ? 1 : -1);
  }
}