ExpressionTreeToString icon indicating copy to clipboard operation
ExpressionTreeToString copied to clipboard

JavaScript notation?

Open StefH opened this issue 2 years ago • 2 comments

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 avatar Jul 20 '21 08:07 StefH

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 avatar Jul 20 '21 10:07 zspitz

@StefH Any further thoughts on this?

zspitz avatar Jul 23 '21 10:07 zspitz