import {ErrorSystem, ErrorCommon} from '../app/model/common/Error'; class DataHelper { static handleDataModelInput(dataInput): void { Object.keys(dataInput).forEach(key => { if (dataInput[key] === undefined) delete dataInput[key]; else if (dataInput[key] === null || dataInput[key] === 'null') dataInput[key] = undefined; }); } static handleIdDataModel(data) { return data && !data._id ? data.toString() : data; } static handleFileDataModel(file) { if (file) { if (file.url) return file.url; return file.toString(); } return undefined; } static handlePromiseRequest(promise): Promise { return new Promise < any >((resolve, reject) => { promise.then(({data, error}) => { if (error) reject(error); else resolve(data); }).catch(error => { if (error.name === 'RequestError') reject(new ErrorCommon(10)); else reject(new ErrorSystem(error.message)); }); }); } static applyTemplate(template, ...params) { return template.replace(/{(\d+)}/g, (match, number) => { return params[number] || match; }); } static convertToCurrency(value: number, option): string { if (typeof value !== 'number') return ''; if (!option) option = {}; if (!option.format) option.format = 'en-US'; if (!option.currency) option.currency = 'USD'; return value.toLocaleString(option.format, {style: 'currency', currency: option.currency}); } static convertStringToBoolean(val: string): boolean { if (!val) return false; val = val.toString(); switch (val.toLowerCase().trim()) { case 'true': case 'yes': case '1': return true; case 'false': case 'no': case '0': return false; default: return false; } } static getContext(val:string) { if (!val) return 'N/A'; let arrStr = val.split('/'); if (arrStr.length > 3) return arrStr[2]; else if (arrStr.length > 2 && arrStr.length < 4) return arrStr[1]; else return 'N/A'; } static pagination(page?: number, limit?: number) { if (!page || isNaN(page)) page = 1; if (!limit || isNaN(limit)) limit = 10; return { limit: limit, skip: (page - 1) * limit }; } } Object.seal(DataHelper); export default DataHelper;