UNPKG

cv-dialog-sdk

Version:

Catavolt Dialog Javascript API

58 lines (57 loc) 1.74 kB
/** * ***************************************************** */ export class ObjUtil { static addAllProps(sourceObj, targetObj) { if (null == sourceObj || 'object' !== typeof sourceObj) { return targetObj; } if (null == targetObj || 'object' !== typeof targetObj) { return targetObj; } for (const attr in sourceObj) { if (sourceObj.hasOwnProperty(attr)) { targetObj[attr] = sourceObj[attr]; } } return targetObj; } static cloneOwnProps(sourceObj) { if (null == sourceObj || 'object' !== typeof sourceObj) { return sourceObj; } const copy = sourceObj.constructor(); for (const attr in sourceObj) { if (sourceObj.hasOwnProperty(attr)) { copy[attr] = ObjUtil.cloneOwnProps(sourceObj[attr]); } } return copy; } static copyNonNullFieldsOnly(obj, newObj, filterFn) { for (const prop in obj) { if (!filterFn || filterFn(prop)) { const type = typeof obj[prop]; if (type !== 'function') { const val = obj[prop]; if (val) { newObj[prop] = val; } } } } return newObj; } static formatRecAttr(o, prettyPrint) { // @TODO - add a filter here to build a cache and detect (and skip) circular references if (prettyPrint) { return JSON.stringify(o, null, 2); } else { return JSON.stringify(o); } } static newInstance(type) { return new type(); } }