
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
As of late my affection for the conciseness-niceness of ternary operators is sky high. However, nesting ternaries is a one-way ticket to the ternary infirmary. A place where you second guess yourself, proceed to curse and raise your blood pressure resulting in your eventual but early death. In a nutshell, nested ternaries kill. The goal of ifd is to provide an alternative to the ternary infirmary by purveying two synergetic abstractions in one tasty punch. One abstraction is to deal with default values, and the other is for nested ternaries both with the goal of extending your life.
Sidenote: Admittedly, ifd is the definition of feature creep, but I got sick of working on mind-numbing documentation, and after toying with an idea or two the challenge was too tempting to turn down.
The mode of action for ifd is entirely dependent on its arguments. In other words, ifd has two distinct responsibilities under the same action, thus flipping the bird to the single responsibility principle. You may find this to be a questionable or even outright foolish design decision, and on many levels, I would agree. I could have separated ifd into two separate methods or functions, but it came at a cost I was unwilling to pay.
This documentation is relatively brief, so it's in your best interest to look through the tests as well. Each test is separated from one another. The .js file is the test and the .md file is the description of the test. For the most part, each test focuses on a single aspect and the .md file should give you a good general gist of what should happen along with auxiliary notes if I saw fit.
Info: Default values at times can be a pain in the butt. For the majority of cases, the pipe operator does the trick, but sometimes the pipe does not make the cut. When I can't pipe, more times than not, I find myself writing logic that for some reason or another turns into a janky mess that tends to creep into my ternaries. To solve this issue, ifd first and foremost acts as a truth-teller of identity.
Action: Returns first "truth" or returns the last argument if no default option Object property is defined.
Syntax
ifd(<condition>, <condition>, ..., <options>)
The Falsey Problem
// if 0 is not a "falsey" value, then we gotz a problem
const foo = 0 || 5;
The Falsey Solution
const foo = ifd(0, 5, {
// 0 is not false
zero: true
});
// foo === 0
A Volume Bump
// creates custom identity thunk
const isAnArrayOfTwo = ifd({
identity: function (val) {
return Array.isArray(val) && val.length === 2;
}
});
const foo = isAnArrayOfTwo([1, 2, 3], 'te', [4, 5], [6, 7]);
//foo === [4, 5]
Thoughts: While the "default" abstraction is splendid all by itself, it really spreads its wings when it teams up with its main man the "ternary" abstraction.
Test Location: /__tests__/default-abstraction
Info: As I mentioned earlier, nesting ternaries will land you in the ternary infirmary resulting in premature death, and from what I hear death isn't all that its cracked up to be. To postpone deaths looming inevitability, ifd creates an abstraction over array arguments which emulates that of the arguments in a ternary condition in more or less words.
Action: If ifd has at least two arguments whose arguments are Array values excluding the last optional option argument Object; ifd evaluates the arguments using the "ternary" abstraction mode of action. In this mode ifd evaluates the <condition> arguments via the set "default" abstraction until a truthy <condition> is met in which case ifd then returns the <return> argument for the <condition>. If no truthy evaluation for the <condition> arguments is met, then the last <return> argument is returned if no default option Object property is defined.
Syntax
ifd([<condition>, <return>], [<condition>, <return>], ..., <options>)
The Nested Ternary Problem
const one = false;
const two = false;
const three = true;
// while this gives us the desired outcome the
// logic is hard to reason about
const foo = one ? 1 : two ? 2 : three ? 3 : 4;
// foo === 3
The Nested Ternary Solution
const one = false;
const two = false;
const three = true;
const foo = ifd([one, 1], [two, 2], [three, 3], { default: 4 });
// foo === 3
"Ternary" Abstraction + "Default" Abstraction
const one = false;
const two = 'Kool';
const three = false;
const foo = ifd(
[[ifd, false, null, one], 1],
[[ifd, two, null], 2],
[three, 3], {
default: false
}
);
// foo === 2
// Additionaly, you can use a placeholder
// to return the passing condition
const returnCondition = ifd(
[[ifd, false, null, one], 1],
//default placeholder = %p
[[ifd, two, null], '%p'],
[three, 3], {
default: false
}
);
// returnCondition === 'Kool'
Function Evaluation
const one = false;
const two = false;
const three = true;
const foo = ifd([one, 1], [two, 2], [three, 3], { default: 4 });
// foo === 3
A Volume Bump
// creates custom identity thunk
const isAnArrayOfTwo = ifd({
// if isAnArryOfTwo does not met identity
// then we want to return false as the default value
default: false,
identity: function (val) {
return Array.isArray(val) && val.length === 2;
}
});
// lazy evaluation of arguments
const foo = ifd(
[[isAnArrayOfTwo, false, true, 0, [2]], 'Not Me!'],
[[isAnArrayOfTwo, [2, 3, 4], false, 'not-an-array'], 'Or Me Either!'],
// [1, 2] === truthy -> <return>
[[isAnArrayOfTwo, undefined, [1, 2], null, [2]], 'Pick Me, And Return This String'],
[[isAnArrayOfTwo, null, []], 'will not be evaluated']
);
Thoughts: ifd goes up to eleven if you need that extra push over the cliff. Right now if you're scratching your head in all likelihood, ifd is probably not the package for you because the reason why you would potentially want to use ifd should click right away.
Test Location: /__tests__/default-abstraction
all: <Boolean | true>
emptyString: <Boolean | false>
false: <Boolean | false>
NaN: <Boolean | false>
null: <Boolean | false>
undefined: <Boolean | false>
zero: <Boolean | false>
identity: <Function, [Function] | false>
everyIdentity: <Boolean | false>
self: <All | Null>arrayArgs: <Boolean | true>
condition: <Boolean | true>
create: <Boolean | true>
ifd instance with set optionsdefault: <All | null>
invoke: <Boolean | true>
ifdplaceholder: <String | '%p'>
Best, te
FAQs
Life Extending Ternary Operator Abstractions
We found that ifd demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.