Skip to content

Commit

Permalink
[#2286]: Underscore could use a _.propertyResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Krimm committed May 5, 2016
1 parent 53086b0 commit 10bc63e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test/utility.js
Expand Up @@ -95,6 +95,18 @@
assert.equal(undefPropertyOf('curly'), void 0, 'should return undefined when obj is undefined');
});

QUnit.test('method', function(assert) {
var stooge = {name: function() { return 'moe'; }, sum: function(a, b, c) { return a + b + c; }};
assert.equal(_.method('name')(stooge), 'moe', 'should return the results of calling the method with the given name');
assert.equal(_.method('sum')(stooge, 1, 2, 3), 6, 'should pass rest of arguments to named method for evaluation');
assert.equal(_.method(function() { return this.name(); })(stooge), 'moe', 'should apply a function literal passed.');
assert.equal(_.method(function(a, b, c) { return this.sum(a, b, c); })(stooge, 1, 2, 3), 6, 'should pass arguments when applying a function literal.');
assert.equal(_.method('macerena')(stooge), void 0, 'should return undefined for non-existant method name on defined object');
assert.equal(_.method('name')(null), void 0, 'should return undefined for null object');
assert.equal(_.method()(stooge), void 0, 'should return undefined for undefined method name on existing object');
assert.equal(_.method()(void 0), void 0, 'should return undefined for undefined method name on undefined object');
});

QUnit.test('random', function(assert) {
var array = _.range(1000);
var min = Math.pow(2, 31);
Expand Down
18 changes: 18 additions & 0 deletions underscore.js
Expand Up @@ -1370,6 +1370,24 @@

_.property = property;

// Generates a function for a given object that returns the passed function's return value.
// Accepts a string or function literal for value.
// If value is a string, function assumes (and verifies) this string is a method name on the
// referenced object.
_.method = function(value) {
var isFunction = this.isFunction,
rest = this.rest,
func;

return function(obj) {
if (obj == null) { return; }
func = isFunction(value) ? value : obj[value];
if (func) {
return func.apply(obj, rest(arguments));
}
};
};

// Generates a function for a given object that returns a given property.
_.propertyOf = function(obj) {
return obj == null ? function(){} : function(key) {
Expand Down

0 comments on commit 10bc63e

Please sign in to comment.