UNPKG

typescript-object-helper

Version:

A simple dependency to aid in mapping and instantiating typescript classes

41 lines 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectHelperBaseObject = void 0; class ObjectHelperBaseObject { // Put anything you want to run/set after everything has been initialized here onInit() { } toJSON() { const proto = Object.getPrototypeOf(this); const jsonObj = {}; const nonPrivateProperties = Object.entries(this).filter(prop => !prop[0].startsWith('_')); const getters = Object.entries(Object.getOwnPropertyDescriptors(proto)) .filter(([, descriptor]) => typeof descriptor.get === 'function'); const allProperties = nonPrivateProperties.concat(getters); allProperties .forEach(([key, descriptor]) => { if (descriptor && !key.startsWith('_')) { try { const val = this[key]; if (typeof val === 'object' && val instanceof ObjectHelperBaseObject) { jsonObj[key] = val.toJSON(); } else if (val instanceof Map) { jsonObj[key] = Object.fromEntries(val.entries()); } else if (typeof val === 'object') { jsonObj[key] = JSON.stringify(val); } else { jsonObj[key] = val; } } catch (error) { console.error(`Error calling getter ${key}`, error); } } }); return JSON.stringify(jsonObj); } } exports.ObjectHelperBaseObject = ObjectHelperBaseObject; //# sourceMappingURL=object-helper-base-object.model.js.map