diff --git a/index.js b/index.js index e28ae60..c1a0eb9 100644 --- a/index.js +++ b/index.js @@ -48,6 +48,10 @@ function get(obj, prop, value) { obj = obj[head]; } + if (typeof obj === 'undefined') { + return value; + } + return obj; } diff --git a/test/dot-prop-immutable.spec.js b/test/dot-prop-immutable.spec.js index 9e1c358..eed52e2 100644 --- a/test/dot-prop-immutable.spec.js +++ b/test/dot-prop-immutable.spec.js @@ -301,6 +301,9 @@ 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', () => { @@ -308,6 +311,9 @@ describe('dot-prop-immutable.spec.js', function() { 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', () => { @@ -315,6 +321,9 @@ describe('dot-prop-immutable.spec.js', function() { 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', () => { @@ -322,6 +331,9 @@ describe('dot-prop-immutable.spec.js', function() { 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', () => { @@ -329,6 +341,9 @@ describe('dot-prop-immutable.spec.js', function() { 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', () => { @@ -347,6 +362,9 @@ 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', () => { @@ -354,6 +372,9 @@ describe('dot-prop-immutable.spec.js', function() { 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', () => { @@ -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'); + }); }); }); @@ -385,6 +409,9 @@ 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', () => { @@ -392,6 +419,9 @@ describe('dot-prop-immutable.spec.js', function() { 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'); + }); }); });