import { Dictionary } from './dictionary'; export class Base { constructor(data: any = null) { this.load(data); } load(data: any = null): void { if (!data) { return; } Object.keys(this).forEach(key => { if (!data.hasOwnProperty(key)) { return; } const dataPropertyValue: any = data[key]; const thisPropertyValue = (this as any)[key]; if (this.isNullOrUndefined(dataPropertyValue)) { (this as any)[key] = null; return; } if (!this.isObject(thisPropertyValue)) { if (typeof thisPropertyValue === 'boolean') { const cloneObj = Boolean(dataPropertyValue); (this as any)[key] = cloneObj; } else if (typeof thisPropertyValue === 'number') { const cloneObj = Number(dataPropertyValue); (this as any)[key] = cloneObj; } else if (typeof thisPropertyValue === 'string') { const cloneObj = String(dataPropertyValue); (this as any)[key] = cloneObj; } } else if (thisPropertyValue instanceof Date) { if (typeof dataPropertyValue === 'number') { let aDate: Date | null = null; aDate = new Date(); aDate.setTime(dataPropertyValue); (this as any)[key] = aDate; } else if (dataPropertyValue instanceof Date) { (this as any)[key] = dataPropertyValue; } else if (typeof dataPropertyValue.toDate === 'function') { try { (this as any)[key] = dataPropertyValue.toDate(); } catch (error) { (this as any)[key] = null; } } else { (this as any)[key] = null; } } else if (thisPropertyValue instanceof Dictionary) { try { const aT = thisPropertyValue.type; (this as any)[key] = new Dictionary(aT); const objKeys = Object.keys(dataPropertyValue); objKeys.forEach(objKey => { if (aT && typeof aT.load === 'function') { const ci = new aT.constructor(); ci.load(dataPropertyValue[objKey]); (this as any)[key].Add(objKey, ci); } else { (this as any)[key].Add(objKey, dataPropertyValue[objKey]); } }); } catch (error) { } } else if (Array.isArray(thisPropertyValue)) { (this as any)[key] = new Array(); dataPropertyValue.forEach((element: any) => { (this as any)[key].push(element); }); } else if (this.isObject(thisPropertyValue)) { const cloneObj = new thisPropertyValue.constructor(dataPropertyValue); (this as any)[key] = cloneObj; } }); } map(mapInformation?: any, sourceObj?: any): void { if (sourceObj) { Object.keys(sourceObj).forEach(property => { if (mapInformation.hasOwnProperty(property)) { const sourceObjProperty = mapInformation[property]; if (this.isString(sourceObjProperty) && this.hasOwnProperty(sourceObjProperty)) { let newValue = sourceObj[property]; if ((this as any)[sourceObjProperty] instanceof Date && typeof newValue === 'number') { let aDate: Date | null = null; if (newValue) { aDate = new Date(); aDate.setTime(newValue); } (this as any)[sourceObjProperty] = aDate; } else { const oValue = (this as any)[sourceObjProperty]; if (typeof oValue === 'number') { newValue = Number(newValue); } else if (typeof oValue === 'string') { newValue = String(newValue); } else if (typeof oValue === 'boolean') { newValue = Boolean(newValue); } (this as any)[sourceObjProperty] = newValue; } } else if (this.isObject(sourceObjProperty)) { const key = Object.keys(sourceObjProperty).shift(); if (key) { const childObjProperty = sourceObjProperty[key]; const childObj = (this as any)[key]; if (this.isObject(childObjProperty)) { if (childObj instanceof Dictionary) { const dictionaryKey = Object.keys(childObjProperty).shift(); if (dictionaryKey) { const dictProperty = childObjProperty[dictionaryKey]; const type = childObj.type; let ci: any = null; if (childObj.ContainsKey(dictionaryKey)) { ci = childObj.Item(dictionaryKey); } if (type && typeof type.load === 'function') { if (!ci) { ci = new type.constructor(); } ci.map({ [property]: dictProperty }, sourceObj); } else { ci = sourceObj[property]; } childObj.Add(dictionaryKey, ci); } } else if (childObj && typeof childObj.load === 'function') { const aKey = Object.keys(childObjProperty).shift(); if (childObj.hasOwnProperty(aKey)) { childObj.map({ [property]: childObjProperty }, sourceObj); } } } else { const newValue = sourceObj[property]; if (childObj[childObjProperty] instanceof Date && typeof newValue === 'number') { let aDate: Date | null = null; if (newValue) { aDate = new Date(); aDate.setTime(newValue); } childObj[childObjProperty] = aDate; } else { if (newValue) { childObj[childObjProperty] = newValue; } } } } } } }); } } toAny(): any { const gObject: any = {}; for (const p in this) { if (this.hasOwnProperty(p)) { const propertyName: string = p; const propertyValue: any = (this as any)[p]; if (propertyName === 'rules' || propertyName === 'dynamicRuleObjects') { continue; } if (this.isNullOrUndefined((this as any)[p])) { gObject[propertyName] = null; continue; } if (Array.isArray((this as any)[p])) { const len = propertyValue.length; const anArray = []; for (let i = 0; i < len; i++) { if (this.isFunction((this as any)[p][i].toAny)) { const anArrayValue: {} = propertyValue[i].toAny() as Base; anArray.push(anArrayValue); } else { anArray.push(propertyValue[i]); } } gObject[propertyName] = anArray; } else if (this.isFunction(propertyValue.toAny)) { const ab: any = propertyValue; const cObj = ab.toAny(); gObject[propertyName] = Object.assign({}, cObj); } else if (this.isFunction(propertyValue.constructor) && propertyValue.constructor.name === 'Dictionary') { const aDict = (this as any)[propertyName]; const keys = aDict.Keys(); const dictObj: any = {}; keys.forEach((key: string) => { const anObject = aDict.Item(key); if (this.isFunction(anObject.toAny)) { const cObj = anObject.toAny(); dictObj[key] = Object.assign(cObj); } else { dictObj[key] = Object.assign({}, anObject); } }); gObject[propertyName] = dictObj; } else if (this.isObject(propertyValue) && !this.isDate(propertyValue)) { if (typeof propertyValue.toAny === 'function') { gObject[propertyName] = propertyValue.toAny(); } else { gObject[propertyName] = Object.assign({}, propertyValue); } } else if (this.isDate(propertyValue)) { gObject[propertyName] = propertyValue; } else { if (this.isObject(propertyValue)) { gObject[propertyName] = Object.assign({}, propertyValue); } else { gObject[propertyName] = propertyValue; } } } } return gObject; } isArray(object: any): object is any[] { return Array.isArray(object); } isBoolean(object: any): object is boolean { return typeof object === 'boolean'; } isNull(object: any): object is null { return object === null; } isNullOrUndefined(arg) { return arg == null; } isNumber(object: any): object is number { return typeof object === 'number'; } isString(object: any): object is string { return typeof object === 'string'; } isSymbol(object: any): object is symbol { return typeof object === 'symbol'; } isUndefined(object: any): object is undefined { return object === void 0; } isRegExp(re) { return this.isObject(re) && this.objectToString(re) === '[object RegExp]'; } isObject(object: any): boolean { return typeof object === 'object' && object !== null; } isDate(object: any): object is Date { return this.isObject(object) && this.objectToString(object) === '[object Date]'; } isError(object: any): object is Error { return this.isObject(object) && (this.objectToString(object) === '[object Error]' || object instanceof Error); } objectToString(o: any): string { return Object.prototype.toString.call(o); } isFunction(arg: any): boolean { return typeof arg === 'function'; } }