<commit_before>import * as moment from 'moment/moment';
import * as _ from 'lodash';
import { translate } from './../../shared/utils';

export class BaseResourceModel {
  className = 'ResourceModel';
  pkFieldName: string;
  pkIsNumber: boolean;
  dateAsStringFormat = 'DD.MM.YYYY';
  dateInputFormat = 'YYYY-MM-DD';
  [key: string]: any;

  get pk(): string | number {
    const key = this.pkFieldName;
    return this[key];
  }
  static meta(): any {
    const meta: any = BaseResourceModel;
    return meta;
  }

  constructor(obj?: any) {
    const funcNameRegex = /function (.{1,})\(/;
    const results = (funcNameRegex).exec((this).constructor.toString());
    this.className = (results && results.length > 1) ? results[1] : '';

    if (this.pkFieldName === undefined) {
      this.pkFieldName = 'id';
    }
    if (this.pkIsNumber === undefined) {
      this.pkIsNumber = true;
    }
    if (obj && (_.isNumber(obj) || _.isString(obj))) {
      const newObj: any = {};
      newObj[this.pkFieldName] = obj;
      obj = newObj;
    }
    this.parse(obj ? obj : {});
  }
  parse(obj: any) {
    const current: any = this.constructor;
    this.parseByFields(obj, current.meta());
  }
  parseByFields(obj: any, meta: any) {
    const fields: string[] = meta.fields ? meta.fields : [];
    const dateFields: string[] = meta.dateFields ? meta.dateFields : [];
    for (let i = 0; i < fields.length; i++) {
      this[fields[i]] = null;
    }
    if (fields.length > 0) {
      for (const key in obj) {
        if (fields.indexOf(key) !== -1) {
          try {
            this[key] = obj[key];
          } catch (err) {

          }
        }
      }
    }
    if (dateFields.length > 0) {
      for (const key in obj) {
        if (dateFields.indexOf(key) !== -1) {
          try {
            this[key] = moment(obj[key]).toDate();
          } catch (err) {
            this[key] = null;
          }
          if (this[key] === 'Invalid date') {
            this[key] = null;
          }
        }
      }
    }
    if (this.pkIsNumber) {
      this[this.pkFieldName] = +obj[this.pkFieldName];
    }
  }
  format() {
    const current: any = this.constructor;
    const result = this.formatByFields(current.meta());
    return result;
  }
  formatByFields(meta: any): any {
    const obj: any = {};
    const fields: string[] = meta.fields ? meta.fields : [];
    const dateFields: string[] = meta.dateFields ? meta.dateFields : [];
    if (fields.length > 0) {
      for (const key in this) {
        if (fields.indexOf(key) !== -1) {
          try {
            obj[key] = this[key];
          } catch (err) {
          }
        }
      }
    }
    if (dateFields.length > 0) {
      const tzoffset = (new Date()).getTimezoneOffset() * 60000;
      for (const key in this) {
        if (dateFields.indexOf(key) !== -1) {
          if (obj[key]) {
            try {
              obj[key] = moment(+moment(this[key]).toDate() - tzoffset).toISOString().slice(0, -1);
            } catch (err) {
              this[key] = null;
            }
            if (obj[key] === 'Invalid date') {
              obj[key] = null;
            }
          } else {
            obj[key] = null;
          }
        }
      }
    }
    return obj;
  }
  dateAsString(fieldName: string) {
    let text: string;
    text = '';
    if (this[fieldName] !== undefined) {
      try {
        text = moment(this[fieldName]).format(this.dateAsStringFormat);
      } catch (err) {
        text = '';
      }
      if (text === 'Invalid date') {<commit_msg>fix(base-resource.model): Change detect used timezone fore  date fields<commit_after>import * as moment from 'moment/moment';
import * as _ from 'lodash';
import { translate } from './../../shared/utils';

export class BaseResourceModel {
  className = 'ResourceModel';

  pkFieldName: string;
  pkIsNumber: boolean;
  dateAsStringFormat = 'DD.MM.YYYY';
  dateInputFormat = 'YYYY-MM-DD';
  [key: string]: any;

  get pk(): string | number {
    const key = this.pkFieldName;
    return this[key];
  }
  static meta(): any {
    const meta: any = BaseResourceModel;
    return meta;
  }

  constructor(obj?: any) {
    const funcNameRegex = /function (.{1,})\(/;
    const results = (funcNameRegex).exec((this).constructor.toString());
    this.className = (results && results.length > 1) ? results[1] : '';

    if (this.pkFieldName === undefined) {
      this.pkFieldName = 'id';
    }
    if (this.pkIsNumber === undefined) {
      this.pkIsNumber = true;
    }
    if (obj && (_.isNumber(obj) || _.isString(obj))) {
      const newObj: any = {};
      newObj[this.pkFieldName] = obj;
      obj = newObj;
    }
    this.parse(obj ? obj : {});
  }
  parse(obj: any) {
    const current: any = this.constructor;
    this.parseByFields(obj, current.meta());
  }
  parseByFields(obj: any, meta: any) {
    const fields: string[] = meta.fields ? meta.fields : [];
    const dateFields: string[] = meta.dateFields ? meta.dateFields : [];
    for (let i = 0; i < fields.length; i++) {
      this[fields[i]] = null;
    }
    if (fields.length > 0) {
      for (const key in obj) {
        if (fields.indexOf(key) !== -1) {
          try {
            this[key] = obj[key];
          } catch (err) {

          }
        }
      }
    }
    if (dateFields.length > 0) {
      for (const key in obj) {
        if (dateFields.indexOf(key) !== -1) {
          try {
            this[key] = moment(obj[key]).toDate();
          } catch (err) {
            this[key] = null;
          }
          if (this[key] === 'Invalid date') {
            this[key] = null;
          }
        }
      }
    }
    if (this.pkIsNumber) {
      this[this.pkFieldName] = +obj[this.pkFieldName];
    }
  }
  format() {
    const current: any = this.constructor;
    return this.formatByFields(current.meta());
  }
  formatByFields(meta: any): any {
    const obj: any = {};
    const fields: string[] = meta.fields ? meta.fields : [];
    const dateFields: string[] = meta.dateFields ? meta.dateFields : [];
    if (fields.length > 0) {
      for (const key in this) {
        if (fields.indexOf(key) !== -1) {
          try {
            obj[key] = this[key];
          } catch (err) {
          }
        }
      }
    }
    if (dateFields.length > 0) {
      for (const key in this) {
        if (dateFields.indexOf(key) !== -1) {
          if (obj[key]) {
            let tzoffset = 0;
            try {
              tzoffset = (moment(this[key]).toDate()).getTimezoneOffset() * 60000;
            } catch (err) {
              tzoffset = (new Date()).getTimezoneOffset() * 60000;
            }
            try {
              obj[key] = moment(+moment(this[key]).toDate() - tzoffset).toISOString().slice(0, -1);
            } catch (err) {
              this[key] = null;
            }
            if (obj[key] === 'Invalid date') {
              obj[key] = null;
            }
          } else {
            obj[key] = null;
          }
        }
      }
    }
    return obj;
  }
  dateAsString(fieldName: string) {
    let text: string;
    text = '';
    if (this[fieldName] !== undefined) {
      try {
        text = moment(this[fieldName]).format(this.dateAsStringFormat);
      } catch (err) {
        text = '';
      }
      if (text === 'Invalid date') {
{'fraction_non_alphanumeric': 0.15927502402855967, 'fraction_numerical': 0.006041466428669504, 'mean_word_length': 1.8893296310987704, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '663977', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2323, 'n_words': 528}