Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ Useful for:

Like `$dum` but logs arguments instead.

### $dume(object, method, callback)

Executes `callback` when the method is invoked.

### $dumr(object, method)

Removes debug or log wrappers added by `$dum` or `$duml`.
Expand Down
48 changes: 33 additions & 15 deletions du.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var exports = {
$duvr: $duvr,
$dum: $dum,
$duml: $duml,
$dume: $dume,
$dumr: $dumr,
$dug: $dug,
$dugl: $dugl,
Expand Down Expand Up @@ -161,15 +162,17 @@ function $duvr(object, event) {
var wrappedMethods = [];

/**
* Wraps a method with a debugger or logger statement.
* Wraps a method with a custom callback.
*
* @param {object} object
* @param {string} methodName
* @param {boolean} isLog
* @param {boolean} log
* @param {boolean} debug
* @param {function} callback
* @private
*/

function wrapMethod(object, methodName, isLog) {
function wrapMethod(object, methodName, log, debug, callback) {
assert(
typeof object === 'object' &&
object && typeof object[methodName] === 'function',
Expand All @@ -186,17 +189,20 @@ function wrapMethod(object, methodName, isLog) {
var slice = [].slice;
var replacement;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're streamlining the functions into one, let's just do function replacement


if (isLog) {
replacement = function() {
console.log(arguments);
return method.apply(this, slice.call(arguments));
};
} else {
replacement = function() {
replacement = function() {
if (typeof callback === 'function') {
callback.apply(this, arguments);
}
if (log) {
var copyArgs = slice.call(arguments);
copyArgs.unshift(methodName);
console.log.apply(console, copyArgs);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this adds the method name to the log message? that's a good idea but why not make it more explicit message, e.g. 'Method x called'?

}
if (debug) {
debugger;
return method.apply(this, slice.call(arguments));
};
}
}
return method.apply(this, slice.call(arguments));
};

object[methodName] = replacement;
}
Expand All @@ -210,7 +216,7 @@ function wrapMethod(object, methodName, isLog) {
*/

function $dum(object, method) {
wrapMethod(object, method);
wrapMethod(object, method, false, true);
}

/**
Expand All @@ -222,9 +228,21 @@ function $dum(object, method) {
*/

function $duml(object, method) {
wrapMethod(object, method, true);
wrapMethod(object, method, true, false);
}

/**
* Wraps a method with a custom callback.
*
* @param {object} object
* @param {string} methodName
* @param {callback} callback
* @public
*/

function $dume(object, method, callback) {
wrapMethod(object, method, false, false, callback);
}
/**
* Removes method wrapping.
*
Expand Down
14 changes: 13 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('$duv, $duvl, $duvr', function() {

});

describe('$dum, $duml, $dumr', function() {
describe('$dum, $duml, $dume, $dumr', function() {

beforeEach(function() {
this.obj = {
Expand All @@ -97,6 +97,18 @@ describe('$dum, $duml, $dumr', function() {
assert.equal(this.obj.foo(1, 2, 3), 6);
});

it('should call custom callback', function() {
var called = false;

du.$dume(this.obj, 'foo', function() {
called = true;
});

this.obj.foo(1, 2, 3);

assert.ok(called);
});

it('should removed wrapped method', function() {
du.$dum(this.obj, 'foo');
du.$dumr(this.obj, 'foo');
Expand Down