Files
afrobiz/lib/services/util.service.ts
2024-02-17 10:49:23 +11:00

33 lines
861 B
TypeScript

export namespace UtilService {
export function addMonths(date: Date, months: number): Date {
const d = date.getDate();
date.setMonth(date.getMonth() + +months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
export function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message;
return String(error);
}
export function stringifySafely(obj: any) {
let cache: any[] = [];
let str = JSON.stringify(obj, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = []; // reset the cache
return str;
}
}