<commit_before>import {
  Component,
  Directive,
  Input,
  EventEmitter,
  SimpleChange,
  OnChanges,
  Optional,
  DoCheck,
  NgModule,
  ModuleWithProviders,
  ViewEncapsulation,
} from '@angular/core';
import { CommonModule } from '@angular/common';

export interface SortEvent {
  sortField: string | string[];
  sortOrder: string
}

export interface PageEvent {
  activePage: number;
  pageLength: number;
  dataLength: number;
}

export interface DataEvent {
  length: number;
}

@Directive({
  selector: 'table[md2-data]',
  exportAs: 'Md2DataTable'
})
export class Md2DataTable implements OnChanges, DoCheck {

  public data: any[];
  public onDataChange = new EventEmitter<DataEvent>();
  public onSortChange = new EventEmitter<SortEvent>();
  public onPageChange = new EventEmitter<PageEvent>();

  private sortField: string | string[] = '';
  private sortOrder = 'asc';
  private isDataChanged = false;

  @Input('md2-data') inputData: any[] = [];
  @Input('md2-page-length') pageLength = 1000;
  @Input('md2-active-page') activePage = 1;

  public getSort(): SortEvent {
    return { sortField: this.sortField, sortOrder: this.sortOrder };
  }

  public setSort(sortField: string | string[], sortOrder: string): void {
    if (this.sortField !== sortField || this.sortOrder !== sortOrder) {
      this.sortField = sortField;
      this.sortOrder = sortOrder;
      this.isDataChanged = true;
      this.onSortChange.emit({ sortField: sortField, sortOrder: sortOrder });
    }
  }

  public getPage(): PageEvent {
    return { activePage: this.activePage, pageLength: this.pageLength, dataLength: this.inputData.length };
  }

  public setPage(activePage: number, pageLength: number): void {
    if (this.pageLength !== pageLength || this.activePage !== activePage) {
      this.activePage = this.activePage !== activePage ? activePage : this.calculateNewActivePage(this.pageLength, pageLength);
      this.pageLength = pageLength;
      this.isDataChanged = true;
      this.onPageChange.emit({
        activePage: this.activePage,
        pageLength: this.pageLength,
        dataLength: this.inputData.length
      });
    }
  }

  private calculateNewActivePage(previousPageLength: number, currentPageLength: number): number {
    let firstRowOnPage = (this.activePage - 1) * previousPageLength + 1;
    let newActivePage = Math.ceil(firstRowOnPage / currentPageLength);
    return newActivePage;
  }

  private recalculatePage() {
    let _lastPage = Math.ceil(this.inputData.length / this.pageLength);
    this.activePage = _lastPage < this.activePage ? _lastPage : this.activePage;
    this.activePage = this.activePage || 1;
  }

  public ngOnChanges(changes: { [key: string]: SimpleChange }): any {
    if (changes['inputData']) {
      this.inputData = changes['inputData'].currentValue || [];
      this.recalculatePage();
      this.onPageChange.emit({
        activePage: this.activePage,
        pageLength: this.pageLength,
        dataLength: this.inputData.length
      });
      this.isDataChanged = true;
    }
  }

  public ngDoCheck(): any {
    if (this.isDataChanged) {
      this.fillData();
      this.isDataChanged = false;
    }
  }

  private fillData(): void {
    this.activePage = this.activePage;
    this.pageLength = this.pageLength;

    let offset = (this.activePage - 1) * this.pageLength;
    let data = this.inputData;
    let sortField = this.sortField;
    if (sortField) {
      data = data.sort((a, b) => {
        let x = isNaN(a[sortField + '']) ? a[sortField + ''].toLowerCase() : a[sortField + ''];
        let y = isNaN(b[sortField + '']) ? b[sortField + ''].toLowerCase() : b[sortField + ''];
        return (x > y) ? 1 : (y > x) ? -1 : 0;
      });
    }
    if (this.sortOrder === 'desc') { data.reverse(); }
    this.data = data.slice(offset, offset + this.pageLength);
  }

  private caseInsensitiveIteratee(sortField: string) {
    return (row: any): any => {
      let value = row[sortField];
      if (value && typeof value === 'string' || value instanceof String) {
        return value.toLowerCase();
      }
      return value;
    };
  }<commit_msg>fix(datatable) update arrya dynamic issue.<commit_after>import {
  Component,
  Directive,
  Input,
  EventEmitter,
  SimpleChange,
  OnChanges,
  Optional,
  DoCheck,
  NgModule,
  ModuleWithProviders,
  ViewEncapsulation,
} from '@angular/core';
import { CommonModule } from '@angular/common';

export interface SortEvent {
  sortField: string | string[];
  sortOrder: string
}

export interface PageEvent {
  activePage: number;
  pageLength: number;
  dataLength: number;
}

export interface DataEvent {
  length: number;
}

@Directive({
  selector: 'table[md2-data]',
  exportAs: 'Md2DataTable'
})
export class Md2DataTable implements OnChanges, DoCheck {

  private dataLength = 0;

  public data: any[];
  public onDataChange = new EventEmitter<DataEvent>();
  public onSortChange = new EventEmitter<SortEvent>();
  public onPageChange = new EventEmitter<PageEvent>();

  private sortField: string | string[] = '';
  private sortOrder = 'asc';
  private isDataChanged = false;

  @Input('md2-data') inputData: any[] = [];
  @Input('md2-page-length') pageLength = 1000;
  @Input('md2-active-page') activePage = 1;

  public getSort(): SortEvent {
    return { sortField: this.sortField, sortOrder: this.sortOrder };
  }

  public setSort(sortField: string | string[], sortOrder: string): void {
    if (this.sortField !== sortField || this.sortOrder !== sortOrder) {
      this.sortField = sortField;
      this.sortOrder = sortOrder;
      this.isDataChanged = true;
      this.onSortChange.emit({ sortField: sortField, sortOrder: sortOrder });
    }
  }

  public getPage(): PageEvent {
    return { activePage: this.activePage, pageLength: this.pageLength, dataLength: this.inputData.length };
  }

  public setPage(activePage: number, pageLength: number): void {
    if (this.pageLength !== pageLength || this.activePage !== activePage) {
      this.activePage = this.activePage !== activePage ? activePage : this.calculateNewActivePage(this.pageLength, pageLength);
      this.pageLength = pageLength;
      this.isDataChanged = true;
      this.onPageChange.emit({
        activePage: this.activePage,
        pageLength: this.pageLength,
        dataLength: this.inputData.length
      });
    }
  }

  private calculateNewActivePage(previousPageLength: number, currentPageLength: number): number {
    let firstRowOnPage = (this.activePage - 1) * previousPageLength + 1;
    let newActivePage = Math.ceil(firstRowOnPage / currentPageLength);
    return newActivePage;
  }

  private recalculatePage() {
    let _lastPage = Math.ceil(this.inputData.length / this.pageLength);
    this.activePage = _lastPage < this.activePage ? _lastPage : this.activePage;
    this.activePage = this.activePage || 1;
  }

  public ngOnChanges(changes: { [key: string]: SimpleChange }): any {
    if (changes['inputData']) {
      this.inputData = changes['inputData'].currentValue || [];
      this.recalculatePage();
      this.onPageChange.emit({
        activePage: this.activePage,
        pageLength: this.pageLength,
        dataLength: this.inputData.length
      });
      this.isDataChanged = true;
    }
  }

  public ngDoCheck(): any {
    if (this.dataLength !== this.inputData.length) {
      this.dataLength = this.inputData.length;
      this.fillData();
      this.recalculatePage();
      this.onPageChange.emit({
        activePage: this.activePage,
        pageLength: this.pageLength,
        dataLength: this.inputData.length
      });
      this.isDataChanged = true;
    }
    if (this.isDataChanged) {
      this.fillData();
      this.isDataChanged = false;
    }
  }

  private fillData(): void {
    this.activePage = this.activePage;
    this.pageLength = this.pageLength;

    let offset = (this.activePage - 1) * this.pageLength;
    let data = this.inputData;
    let sortField = this.sortField;
    if (sortField) {
      data = data.sort((a: any, b: any) => {
        let x = isNaN(a[sortField + '']) ? a[sortField + ''].toLowerCase() : a[sortField + ''];
        let y = isNaN(b[sortField + '']) ? b[sortField + ''].toLowerCase() : b[sortField + ''];
        return (x > y) ? 1 : (y > x) ? -1 : 0;
      });
    }
    if (this.sortOrder === 'desc') { data.reverse(); }
    this.data = data.slice(offset, offset + this.pageLength);
  }

  private caseInsensitiveIteratee(sortField: string) {
    return (row: any): any => {
      let value = row[sortField];
      if (value && typeof value === 'string' || value instanceof String) {
        return value.toLowerCase();
      }
      return value;
    };
  }
{'fraction_non_alphanumeric': 0.13223235939484929, 'fraction_numerical': 0.004273010740270239, 'mean_word_length': 3.2913776015857286, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3201109', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2671, 'n_words': 599}