all files / src/app/components/user/ ReposList.js

100% Statements 35/35
100% Branches 11/11
100% Functions 9/9
100% Lines 32/32
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                                                                      10×           15×   14× 11×                      
class ReposListController {
  /* @ngInject */
  constructor() {
    this.active = '';
    this.reverse = false;
    this.repos = [];
    this.sortList = [{
      value: 'language',
      label: 'Lang'
    }, {
      value: 'updated_at',
      label: 'Update'
    }, {
      value: 'forks_count',
      label: 'Forks'
    }, {
      value: 'open_issues_count',
      label: 'Issues'
    }, {
      value: 'stargazers_count',
      label: 'Stars'
    }];
  }
 
  $onInit() {
    this.sortBy('stargazers_count');
  }
 
  sortBy(prop) {
    if (prop === this.active) {
      this.repos = this.repos.reverse();
      this.reverse = !this.reverse;
    } else {
      this.active = prop;
      this.reverse = false;
      const repos = this.repos;
 
      switch (prop) {
        case 'language':
          prop = 'compareLanguage';
          repos.forEach(r => {
            if (r.language) {
              r[prop] = r.language.slice(0, 1);
            } else {
              r[prop] = 'zzz';
            }
          });
          this.repos = repos.sort((a, b) => this._doSort(b, a, prop));
          break;
 
        case 'updated_at':
          prop = 'compareUpdate';
          repos.forEach(r => {
            r[prop] = new Date(r.updated_at);
          });
          this.repos = repos.sort((a, b) => this._doSort(a, b, prop));
          break;
 
        default:
          this.repos = repos.sort((a, b) => this._doSort(a, b, prop));
          break;
      }
    }
  }
 
  _doSort(a, b, prop) {
    if (a[prop] < b[prop]) {
      return 1;
    }
    if (a[prop] > b[prop]) {
      return -1;
    }
    return 0;
  }
}
 
export const ReposList = {
  template: require('./ReposList.html'),
  controller: ReposListController,
  bindings: {
    repos: '<'
  }
};