Skip to content

Commit 0cd1ad2

Browse files
authoredNov 8, 2019
Merge pull request #256 from martin-dimitrov13/master
Fixed object/get throwing exception for null or undefined input objects
2 parents 75c3b86 + 98f98ce commit 0cd1ad2

File tree

7 files changed

+24
-9
lines changed

7 files changed

+24
-9
lines changed
 

‎CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
mout changelog
22
==============
3+
v1.1.1 (2019/04/18)
4+
--------------------
5+
- fixed `object/get` throwing an exception for null or undefined input objects
36

47
v1.1.0 (2017/10/04)
58
--------------------
@@ -204,4 +207,3 @@ v0.1.0 (2013/01/09)
204207
-------------------
205208

206209
- Rename project from "amd-utils" to "mout"
207-

‎bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@
4040
"type": "git",
4141
"url": "git://github.com/mout/mout.git"
4242
},
43-
"version": "1.1.0"
43+
"version": "1.1.1"
4444
}

‎doc/object.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ functions(obj); // ['foo']
363363
## get(obj, propName):*
364364

365365
Returns nested property value. Will return `undefined` if property doesn't
366-
exist.
366+
exist or if the object is null or undefined.
367367

368368
See: [`set()`](#set), [`namespace()`](#namespace), [`has()`](#has)
369369

@@ -378,6 +378,7 @@ var lorem = {
378378

379379
get(lorem, 'ipsum.dolor.sit'); // "amet"
380380
get(lorem, 'foo.bar'); // undefined
381+
get(undefined, 'foo.bar'); // undefined
381382
```
382383

383384

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mout",
33
"description": "Modular Utilities",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"homepage": "http://moutjs.com/",
66
"author": "Miller Medeiros <contact@millermedeiros.com> (http://blog.millermedeiros.com)",
77
"contributors": [

‎src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**@license
2-
* mout v1.1.0 | http://moutjs.com | MIT license
2+
* mout v1.1.1 | http://moutjs.com | MIT license
33
*/
44
define(function(require){
55

66
//automatically generated, do not edit!
77
//run `node build` instead
88
return {
9-
'VERSION' : '1.1.0',
9+
'VERSION' : '1.1.1',
1010
'array' : require('./array'),
1111
'collection' : require('./collection'),
1212
'date' : require('./date'),

‎src/object/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ define(['../lang/isPrimitive'], function (isPrimitive) {
44
* get "nested" object property
55
*/
66
function get(obj, prop){
7+
if (!obj) return;
78
var parts = prop.split('.'),
89
last = parts.pop();
9-
1010
while (prop = parts.shift()) {
1111
obj = obj[prop];
1212
if (obj == null) return;

‎tests/spec/object/spec-get.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ define(
2323
lorem : function(){}
2424
}
2525
};
26-
26+
2727
foo.bar.lorem.ipsum = 'dolor'
2828

2929
expect( get(foo, 'bar.lorem.ipsum') ).toBe( 'dolor' );
@@ -58,8 +58,20 @@ define(
5858
expect( get(foo, 'foo.bar.baz') ).toBe(undef);
5959
});
6060

61+
it('should return undefined for undefined input objects', function() {
62+
var foo = undefined;
63+
64+
var undef;
65+
expect( get(foo, 'bar.baz') ).toBe(undef);
66+
});
67+
68+
it('should return undefined for null input objects', function() {
69+
var foo = null;
70+
71+
var undef;
72+
expect( get(foo, 'bar.baz') ).toBe(undef);
73+
});
6174
});
6275

6376
}
6477
);
65-

0 commit comments

Comments
 (0)
Please sign in to comment.