Permalink
Browse files

add undefined as default

  • Loading branch information...
1 parent df215a2 commit 72d7c80528fcc86e5a13274c74cbf92219862470 @tj tj committed Sep 19, 2012
Showing with 8 additions and 1 deletion.
  1. +1 −0 index.js
  2. +7 −1 test/index.js
View
@@ -23,6 +23,7 @@ function stringToFunction(str) {
var props = str.split('.');
return function(obj){
for (var i = 0; i < props.length; ++i) {
+ if (null == obj) return;
obj = obj[props[i]];
}
return obj;
View
@@ -1,7 +1,7 @@
var map = require('..');
-var tobi = { name: { first: 'tobi' }, age: 2 };
+var tobi = { name: { first: 'tobi' }, age: 2, role: { name: 'admin' } };
var loki = { name: { first: 'loki' }, age: 1 };
var jane = { name: { first: 'jane' }, age: 8 };
@@ -29,4 +29,10 @@ describe('map(arr, fn)', function(){
var arr = map(users, 'name.first');
arr.should.eql(['tobi', 'loki', 'jane']);
})
+
+ it('should return undefined when the nested props do not exist', function(){
+ var users = [tobi, loki, jane];
+ var arr = map(users, 'role.name');
+ arr.should.eql(['admin', undefined, undefined]);
+ })
})

0 comments on commit 72d7c80

Please sign in to comment.