Created
October 18, 2015 05:00
-
-
Save JakeSidSmith/f67cec8f9c4d03bdba34 to your computer and use it in GitHub Desktop.
Alernative syntax for nested ternaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Ternary = function (condition, result) { | |
var resolved; | |
if (condition) { | |
resolved = result; | |
} | |
var ifFn = function (condition, result) { | |
if (resolved) { | |
return new Ternary(true, resolved); | |
} else if (condition) { | |
return new Ternary(true, result); | |
} else { | |
return new Ternary(); | |
} | |
}; | |
var elseFn = function (result) { | |
if (resolved) { | |
return new Ternary(true, resolved); | |
} else { | |
return new Ternary(true, result); | |
} | |
}; | |
var resolveFn = function () { | |
return resolved; | |
}; | |
return { | |
if: ifFn, | |
elseIf: ifFn, | |
else: elseFn, | |
resolve: resolveFn | |
}; | |
}; | |
/* | |
// Example | |
var example = new Ternary() | |
.if(false, 'Fails') | |
.elseIf(true, 'Passes') | |
.else('Does not matter'); | |
// Logs 'Passes' | |
console.log(example.resolve()); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment