Open
Conversation
f0bd7ed to
d9782b0
Compare
|
will try this package after you merge this. 📦 |
|
Hey @ajwhite. This looks pretty good to me. If you want to get rid of evaluate why not just return the evaluate function with the api attached? renderIf.switch(x)();
renderif.switch(x)
.case(1)(<span>1</span>)
.case(2)(<span>2</span>)
.default(<span>default</span>)();also i noticed that you are sorting conditions by 'type' before evaluating them, presumably because you'd like to make sure conditions are executed in the proper order. I think it might be better to enforce the behavior you want instead of fixing invalid usages of the library and causing potentially unexpected results. I think the easiest way to do this would be to follow a builder type approach and create a funnel. Maybe something along these lines. const multi = () => {
const cases = [];
function createEvaluator() {
return function evaluate() {
for (let index = 0; index < cases.length; index++) {
if (cases[index].condition) {
if (typeof cases[index].elemOrThunk === 'function') {
return cases[index].elemOrThunk();
}
return cases[index].elemOrThunk;
}
}
return null;
};
};
function ifCondition (condition) {
return (elemOrThunk) => {
cases.push({ condition, elemOrThunk });
return ifConditionApi;
};
};
function elseCondition (elemOrThunk) {
cases.push({ condition: true, elemOrThunk });
return createEvaluator();
};
const ifConditionApi = Object.assign(
createEvaluator(),
{
else: elseCondition,
elseIf: ifCondition
}
);
return { if: ifCondition };
};
const renderIf = () => {};
renderIf.if = (condition) => multi().if(condition);
renderIf
.if(false)('if condition')
.elseIf(false)('else')
.elseIf(false)('another else')
.else('hello')();
renderIf
.if(false)('if condition')
.else('hi')();
renderIf
.if(false)('if condition')
.else('hi')
.elseIf(true)('ERROR!!11!!one!')(); |
|
Very handy, I like it! Any plans to merge? |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Seeking Feedback
I'd love to hear from some folks that currently use this, or are interested in using this, in what they think of the approaches behind these two new APIs.
Here we are -- support for multiple cases (#7):
If/ElseIf/Else
Instead of:
Note: this does not remove
renderIf- it's still quite handy and can be used for single conditionsSwitch
I'd love if we didn't have to call
evaluate()- but I can't imagine it's possible to know when we're at the end of the execution chain.Tests are still needed for this PR