All files / lib/models BaseModel.ts

31.58% Statements 6/19
0% Branches 0/10
25% Functions 1/4
31.58% Lines 6/19
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  1x 1x 1x     4x 4x                                                               1x            
import * as dynogels from 'drandx-dynogels';
import {Utils} from '../config/utils';
import { capitalize, changeTimeFormat } from '../miscelanea'
 
export class BaseModel {
  public createdAt: number;
  public updatedAt: number;
 
  constructor() {
    this.createdAt = Utils.unixNowTimestamp();
    this.updatedAt = Utils.unixNowTimestamp();
  }

  public fillFromJSON(json: string): void {
    const jsonObj: Object = JSON.parse(json);
    for (var propName in jsonObj) {
        this[propName] = jsonObj[propName] === "" ? undefined : jsonObj[propName];
    }
    this.afterFillFromJSON();
  }

  public static transformData(jsonObj: {}, exclude?: String[]): {} {
    for (var propName in jsonObj) {
        try {
          if ( exclude.includes(propName)) {
            continue;
          }
          else if (propName == 'createdAt' || propName == 'updatedAt' ) {
            jsonObj[propName] =  changeTimeFormat(<number>jsonObj[propName]);
          }
          else if (typeof(jsonObj[propName]) === 'string') {
            jsonObj[propName] =  capitalize(<string>jsonObj[propName]);
          }
        } catch(e) {
        }
    }
    return jsonObj;
  }
 
 
  public afterFillFromJSON(){
    //
  };
 
}