Skip to content

Commit

Permalink
Allow _.property to correctly fetch values
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Oct 8, 2016
1 parent 623587a commit 291a100
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,11 +916,15 @@
assert.strictEqual(_.property('name')(null), void 0, 'should return undefined for null values');
assert.strictEqual(_.property('name')(void 0), void 0, 'should return undefined for undefined values');
assert.strictEqual(_.property(null)('foo'), void 0, 'should return undefined for null object');
assert.strictEqual(_.property('x')({x: null}), null, 'can fetch null values');
assert.strictEqual(_.property('length')(null), void 0, 'does not crash on property access of non-objects');

// Deep property access
assert.strictEqual(_.property('a')({a: 1}), 1, 'can get a direct property');
assert.strictEqual(_.property(['a', 'b'])({a: {b: 2}}), 2, 'can get a nested property');
assert.strictEqual(_.property(['a'])({a: false}), false, 'can fetch falsey values');
assert.strictEqual(_.property(['x', 'y'])({x: {y: null}}), null, 'can fetch null values deeply');
assert.strictEqual(_.property(['x', 'y'])({x: null}), void 0, 'does not crash on property access of nested non-objects');
});

QUnit.test('propertyOf', function(assert) {
Expand Down
5 changes: 3 additions & 2 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@
if (!isArray(path)) path = [path];
var length = path.length;
return function(object) {
for (var i = 0; object != null && i < length; i++) {
for (var i = 0; i < length; i++) {
if (object == null) return void 0;
object = object[path[i]];
}
return object == null ? void 0 : object;
return object;
};
};

Expand Down

0 comments on commit 291a100

Please sign in to comment.