UNPKG

object-wrapper

Version:

Wraps all of an object's functions in a wrapper of your choosing.

33 lines 1.24 kB
export default function wrapObject(obj, wrapper, objName = 'root', modifyObj = false) { if (typeof (obj) !== 'object' && typeof (obj) !== 'function') { throw new Error(`Erroneous argument of type ${typeof (obj)}. object-wrapper only wraps objects or functions.`); } let wrappedObj = modifyObj ? obj : (typeof (obj) === 'function' ? function () { return wrapper({ namespace: obj, namespaceName: objName, originalFcn: obj, originalFcnName: objName }, arguments, this instanceof wrappedObj, wrappedObj); } : {}); // Avoid items on the prototype. Object.keys(obj).forEach(function (name) { if (typeof (obj[name]) === 'function') { var funcInfo = { namespace: obj, namespaceName: objName, originalFcn: obj[name], originalFcnName: name }; wrappedObj[name] = function () { return wrapper(funcInfo, arguments, this instanceof wrappedObj[name], wrappedObj[name]); }; } else { wrappedObj[name] = obj[name]; } }); return wrappedObj; } ; //# sourceMappingURL=index.js.map