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

JavaScript notation? #87

Open
StefH opened this issue Jul 20, 2021 · 2 comments
Open

JavaScript notation? #87

StefH opened this issue Jul 20, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@StefH
Copy link

StefH commented Jul 20, 2021

Describe the solution you'd like
Would it be possible to add a "JavaScript" formatter to write a valid JavaScript notation?

Expression<Func<double, int, double[], FormattableString>> ex = (value, index, values) => $"{(value < 1000 ? value : (value / 1000.0) + " K")}";

should be converted to:

`$(value < 1000 ? value : (value / 1000) + ' K'`
@StefH StefH added the enhancement New feature or request label Jul 20, 2021
@zspitz
Copy link
Owner

zspitz commented Jul 20, 2021

It's certainly possible. Could you better clarify what's your use case?

Also, specifically WRT your example, my initial thinking would be as follows:

Expression<Func<double, int, double[], FormattableString>> ex = (value, index, values) => $"{(value < 1000 ? value : (value / 1000.0) + " K")}";
Console.WriteLine(ex.ToString("Javascript");
/*
    (value, index, values) => `$(value < 1000 ? value : (value / 1000) + ' K'`;
*/

and it would render the same whether the expression's return type is FormattableString or string, because in either case Javascript template literal syntax corresponds most closely to string interpolation.

But you could get the result you describe by using the LambdaExpression.Body property:

Expression<Func<double, int, double[], FormattableString>> ex = (value, index, values) => $"{(value < 1000 ? value : (value / 1000.0) + " K")}";
Console.WriteLine(ex.Body.ToString("Javascript");
/*
    `$(value < 1000 ? value : (value / 1000) + ' K'`
*/

Would this work for you?

@zspitz
Copy link
Owner

zspitz commented Jul 23, 2021

@StefH Any further thoughts on this?

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