diff --git a/LEEME.md b/LEEME.md index 3d9ea8a..15bd716 100644 --- a/LEEME.md +++ b/LEEME.md @@ -66,9 +66,17 @@ En caso de recibir como parámetro `coalesce.throwError(message)` y que los parámetros anteriores no están definidos o son distintos de null lanza una excepción con ese mensaje. +En caso de recibir como parámetro `coalesce.throwErrorIfUndefined(message)` +y que los parámetros anteriores no están definidos +lanza una excepción con ese mensaje. + diff --git a/best-globals.js b/best-globals.js index 71b7815..650d70b 100644 --- a/best-globals.js +++ b/best-globals.js @@ -28,13 +28,25 @@ var bestGlobals = {}; bestGlobals.coalesce=function coalesce(){ var i=0; - while(i=arguments.length){ + if(hasNull){ + return null; + } + }else{ + return arguments[i]; } - return arguments[i]; }; bestGlobals.coalesce.throwError=function throwError(message){ @@ -45,6 +57,16 @@ bestGlobals.coalesce.throwError=function throwError(message){ } }; +bestGlobals.coalesce.throwErrorIfUndefined=function throwErrorIfUndefined(message){ + if(this === bestGlobals.coalesce){ + return new bestGlobals.coalesce.throwErrorIfUndefined(message); + }else{ + this.message=message; + } +} + +bestGlobals.coalesce.throwErrorIfUndefined.prototype=Object.create(bestGlobals.coalesce.throwError.prototype); + function retreiveOptions(functionCallee, functionAruguments, mandatoryPositionCountingFromOne){ if(functionAruguments.length", "license": "MIT", "repository": "codenautas/best-globals", diff --git a/test/test.js b/test/test.js index 5cc0363..cf7dd6b 100644 --- a/test/test.js +++ b/test/test.js @@ -10,18 +10,34 @@ describe('best-globals', function(){ it('return the first value if is not null',function(){ expect(bestGlobals.coalesce(7,8)).to.be(7); expect(bestGlobals.coalesce(7,8,bestGlobals.coalesce.throwError)).to.be(7); + expect(bestGlobals.coalesce(7,8,bestGlobals.coalesce.throwErrorIfUndefined)).to.be(7); }); it('return the first not null value',function(){ expect(bestGlobals.coalesce(null,null,null,null,null,null,null,null,null,null,null,null,null,17,8)).to.be(17); expect(bestGlobals.coalesce(null,null,null,null,null,null,null,null,null,null,null,null,null,17,8,bestGlobals.coalesce.throwError)).to.be(17); + expect(bestGlobals.coalesce(null,null,null,null,null,null,null,null,null,null,null,null,null,17,8,bestGlobals.coalesce.throwErrorIfUndefined)).to.be(17); }); - it('return the last value if all are nulls',function(){ - expect(typeof bestGlobals.coalesce(null,{}.inex)).to.be("undefined"); + it('coalesce(null,undefined)===null',function(){ + expect(bestGlobals.coalesce(null,{}.inex)+"").to.be("null"); + }); + it('coalesce(undefined,undefined)===undefined',function(){ + expect(typeof bestGlobals.coalesce(undefined,{}.inex)).to.be("undefined"); }); it('throw error if all are nulls',function(){ expect(function(){ - typeof bestGlobals.coalesce(null,{}.inex,bestGlobals.coalesce.throwError("this message")); + typeof bestGlobals.coalesce(null,null,bestGlobals.coalesce.throwError("this message")); }).to.throwError(/this message/); + expect( + bestGlobals.coalesce(null,null,bestGlobals.coalesce.throwErrorIfUndefined("m2"))+"" + ).to.eql("null"); + }); + it('throw error if all are undefined',function(){ + expect(function(x){ + bestGlobals.coalesce({}.inex,undefined,x,bestGlobals.coalesce.throwError("msgE")) + }).to.throwError(/msgE/); + expect(function(x){ + typeof bestGlobals.coalesce(x,undefined,{}.inex,bestGlobals.coalesce.throwErrorIfUndefined("msgU")); + }).to.throwError(/msgU/); }); var valids=[ { element:[] },