Skip to content

Commit

Permalink
Merge pull request #19 from selfcontained/fix-default-cases
Browse files Browse the repository at this point in the history
Fixes several cases around get() returning default value
  • Loading branch information
Dan Rasmussen authored Nov 24, 2017
2 parents 0c60b8c + c95db5a commit 3844f7d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function get(obj, prop, value) {
obj = obj[head];
}

if (typeof obj === 'undefined') {
return value;
}

return obj;
}

Expand Down
30 changes: 30 additions & 0 deletions test/dot-prop-immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,34 +301,49 @@ describe('dot-prop-immutable.spec.js', function() {
it('should get prop', () => {
expect(dotProp.get(obj, 'b')).to.eql({ x: 1, y: 2 });
});
it('should get prop default', () => {
expect(dotProp.get(obj, 'd', 'default')).to.eql('default');
});
});

describe('when get prop empty object', () => {

it('should get prop', () => {
expect(dotProp.get({}, 'b')).to.equal(undefined);
});
it('should get prop default', () => {
expect(dotProp.get({}, 'b', 'default')).to.equal('default');
});
});

describe('when get prop empty path', () => {

it('should get prop', () => {
expect(dotProp.get(obj, '')).to.equal(undefined);
});
it('should get prop default', () => {
expect(dotProp.get(obj, '', 'default')).to.equal('default');
});
});

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

it('should get prop', () => {
expect(dotProp.get(obj, 'b.x')).to.equal(1);
});
it('should get prop default', () => {
expect(dotProp.get(obj, 'b.z', 'default')).to.equal('default');
});
});

describe('when get dotted prop', () => {

it('should get prop', () => {
expect(dotProp.get(obj, 'b\\.x')).to.equal(10);
});
it('should get prop default', () => {
expect(dotProp.get(obj, 'b\\.y', 'default')).to.equal('default');
});
});

describe('when get deep prop not defined', () => {
Expand All @@ -347,13 +362,19 @@ describe('dot-prop-immutable.spec.js', function() {
it('should get index', () => {
expect(dotProp.get(obj, 'c.0')).to.equal(1);
});
it('should get index default', () => {
expect(dotProp.get(obj, 'c.2', 'default')).to.equal('default');
});
});

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

it('should return undefined', () => {
expect(dotProp.get(obj, 'c.1.z.w')).to.equal(undefined);
});
it('should return default', () => {
expect(dotProp.get(obj, 'c.1.z.w', 'default')).to.equal('default');
});
});

describe('when get array[index] out of index', () => {
Expand All @@ -375,6 +396,9 @@ describe('dot-prop-immutable.spec.js', function() {
it('should return undefined', () => {
expect(dotProp.get(obj, 'c.w')).to.equal(undefined);
});
it('should return default', () => {
expect(dotProp.get(obj, 'c.w', 'default')).to.equal('default');
});
});
});

Expand All @@ -385,13 +409,19 @@ describe('dot-prop-immutable.spec.js', function() {
it('should get index', () => {
expect(dotProp.get(arr, '0')).to.equal(1);
});
it('should return default when index not present', () => {
expect(dotProp.get(arr, '2', 'default')).to.equal('default');
});
});

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

it('should replace prop', () => {
expect(dotProp.get(arr, '1.a')).to.equal(false);
});
it('should return default when not present', () => {
expect(dotProp.get(arr, '1.b', 'default')).to.equal('default');
});
});

});
Expand Down

0 comments on commit 3844f7d

Please sign in to comment.