Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String.Coalesce #60

Closed
kspearrin opened this issue Nov 23, 2022 · 5 comments
Closed

Add String.Coalesce #60

kspearrin opened this issue Nov 23, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@kspearrin
Copy link
Contributor

kspearrin commented Nov 23, 2022

It would be nice to have a helper that coalesces to find the first non-null/empty/whitespace value. I created this helper to serve my needs (see below). I would submit a PR for this, but I am unsure how to add infinite argument helpers, given your existing helper examples are all finite.

Is there interest in such a helper for this library?

context.RegisterHelper("String.Coalesce", (context, arguments) =>
{
    foreach (var arg in arguments)
    {
        if (arg != null)
        {
            if (arg is string s)
            {
                if (!string.IsNullOrWhiteSpace(s))
                {
                    return arg;
                }
            }
            else
            {
                return arg;
            }
        }
    }
    return null;
});
Result: {{String.Coalesce "", " ", "", "a", "b", "", "c"}}
Result: a
@StefH
Copy link
Collaborator

StefH commented Nov 24, 2022

Technically this should be possible. I'll take a look.

@StefH
Copy link
Collaborator

StefH commented Nov 24, 2022

#61

@StefH
Copy link
Collaborator

StefH commented Nov 24, 2022

@kspearrin
You can try preview 2.3.8-ci-16687

@StefH
Copy link
Collaborator

StefH commented Nov 25, 2022

@kspearrin
if this fix is ok, then i can create a new nuget which includes your pr + this one

@kspearrin
Copy link
Contributor Author

@StefH Tested and looks good. Thanks.

@StefH StefH added the enhancement New feature or request label Nov 25, 2022
@StefH StefH closed this as completed Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants