Skip to content

Commit

Permalink
add null checks and tests relative to the solved issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cluk3 committed Dec 30, 2017
1 parent 3844f7d commit a75e188
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ function set(obj, prop, value) {
* Get a value by a dot path.
* @param obj The object to evaluate.
* @param prop The path to value that should be returned.
* @param value The default value that should be returned when the target doesn't exist.
*/
function get(obj, prop, value) {
prop = typeof prop === 'number' ? propToArray(prop.toString()) : typeof prop === 'string' ? propToArray(prop) : prop;

for (var i = 0; i < prop.length; i++) {
if (typeof obj !== 'object') {
if (obj === null || typeof obj !== 'object') {
return value;
}
var head = prop[i];
Expand Down Expand Up @@ -69,7 +70,7 @@ function _delete(obj, prop) {
var deletePropImmutableRec = function(obj, prop, i) {
var clone, head = prop[i];

if (typeof obj !== 'object' ||
if (obj === null || typeof obj !== 'object' ||
!Array.isArray(obj) && obj[head] === undefined) {

return obj;
Expand Down
19 changes: 19 additions & 0 deletions test/dot-prop-immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ describe('dot-prop-immutable.spec.js', function() {
});
});

describe('when get intermediate deep prop is null', () => {

it('should return default value', () => {
expect(dotProp.get({b: {z: null}}, 'b.z.w')).to.equal(undefined);
expect(dotProp.get({b: {z: null}}, 'b.z.w', 'default')).to.equal('default');
});
});

describe('when get array[index]', () => {

it('should get index', () => {
Expand Down Expand Up @@ -509,6 +517,17 @@ describe('dot-prop-immutable.spec.js', function() {
it('invariant', objInvariant);
});

describe('when delete deep prop is null', () => {

before(function () {
result = dotProp.delete({b: {z: null}}, 'b.z.w');
});

it('should delete prop', () => {
expect(result).to.eql({b: {z: null}});
});
});

describe('when delete array[index]', () => {

before(function () {
Expand Down

0 comments on commit a75e188

Please sign in to comment.