Skip to content

Commit

Permalink
coalesce elije null por sobre undefined. Agrego throwErrorIfUndefined
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioplatzer committed Jun 13, 2016
1 parent 10c09e9 commit 3671d00
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
10 changes: 9 additions & 1 deletion LEEME.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!--lang:en--]
Returns the first not null nor undefined parameter
Returns the first not null nor undefined parameter.
Use `coalesce.throwError(message)` for throw an Exception if all parameters are null or undefined.
Use `coalesce.throwErrorIfUndefined(message)` for throw an Exception if all parameters are undefined.
[!--lang:*-->

Expand Down
28 changes: 25 additions & 3 deletions best-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ var bestGlobals = {};

bestGlobals.coalesce=function coalesce(){
var i=0;
while(i<arguments.length-1 && arguments[i]==null){
var hasNull=false;
while(i<arguments.length && arguments[i]==null){
hasNull=hasNull || arguments[i]===null;
i++;
}
if(arguments[i] instanceof bestGlobals.coalesce.throwError){
throw new Error(arguments[i].message);
if(arguments[i] instanceof bestGlobals.coalesce.throwErrorIfUndefined && hasNull){
return null;
}else{
throw new Error(arguments[i].message);
}
}
if(i>=arguments.length){
if(hasNull){
return null;
}
}else{
return arguments[i];
}
return arguments[i];
};

bestGlobals.coalesce.throwError=function throwError(message){
Expand All @@ -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<mandatoryPositionCountingFromOne){
return {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "best-globals",
"description": "common global function and constants - i.e. coalesce",
"version": "0.4.5",
"version": "0.4.6",
"author": "Codenautas <codenautas@googlegroups.com>",
"license": "MIT",
"repository": "codenautas/best-globals",
Expand Down
22 changes: 19 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:[] },
Expand Down

0 comments on commit 3671d00

Please sign in to comment.