Skip to content

Commit

Permalink
Merge pull request #15707 from bekzod/can-invoke
Browse files Browse the repository at this point in the history
fix `canInvoke` for edge cases
  • Loading branch information
krisselden authored Oct 17, 2017
2 parents c482f7b + b6e016e commit 241adea
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ember-utils/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import applyStr from './apply-str';
@private
*/
export function canInvoke(obj, methodName) {
return !!(obj && typeof obj[methodName] === 'function');
return obj !== null && obj !== undefined && typeof obj[methodName] === 'function';
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/ember-utils/tests/can_invoke_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ QUnit.test('should return false if the object doesn\'t exist', function() {
equal(canInvoke(undefined, 'aMethodThatDoesNotExist'), false);
});

QUnit.test('should return true for falsy values that have methods', function() {
equal(canInvoke(false, 'valueOf'), true);
equal(canInvoke('', 'charAt'), true);
equal(canInvoke(0, 'toFixed'), true);
});

QUnit.test('should return true if the method exists on the object', function() {
equal(canInvoke(obj, 'aMethodThatExists'), true);
});
Expand Down

0 comments on commit 241adea

Please sign in to comment.