Skip to content

Instantly share code, notes, and snippets.

@JakeSidSmith
Created October 18, 2015 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakeSidSmith/f67cec8f9c4d03bdba34 to your computer and use it in GitHub Desktop.
Save JakeSidSmith/f67cec8f9c4d03bdba34 to your computer and use it in GitHub Desktop.
Alernative syntax for nested ternaries
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