Special string argument syntax

I’m trying to output the numbers 1 to 12 in double digit format (i.e. 00 to 12).

When using the regular expression syntax this works correctly.

{{ 1 | range(12) | assignTo: months }}
{{ months | select(′{{ '{0:00}' | fmt(it) }}\n′) }}

However I can’t get the special syntax to work. I’m trying to use:

{{ 1 | range(12) | assignTo: months }}
{{ months | select: { '{0:00}' | fmt(it) }\n }}

But the output is just {0:00} on each line.

What is the correct way to construct the expression using the special short syntax?

The issue is the {} braces in the fmt expression where it’s being rewritten as:

{{ 1 | range(12) | assignTo: months }}
{{ months | select(′{{ '{{0:00}}' | fmt(it) }}\n′) }}

I’ll look into making the rewriting smarter so it ignores braces in strings. Another way to rewrite it without using {0:00} is with:

{{ 1 | range(12) | assignTo: months }}
{{ months | select: { it | format("0#") }\n }}

or

{{ months | select: { it | padLeft(2,'0') }\n }}
1 Like

I ended up creating an API that only replaces strings outside of quotes in this commit however it breaks a number of use-cases that uses nested filters within whitespace syntax, e.g:

{{ [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] | assignTo: numbers }}
{{ numbers 
   | groupBy: mod(it,5)
   | let({ remainder: 'it.Key', numbers: 'it' })
   | select: Nums with remainder {remainder} div by 5:\n{ numbers | select('{it}\n') } }}

This needs to replace the {} in select('{it}\n') that using the new API doesn’t replace and we can’t use select('{{it}}\n') because {{ }} are global markers which identify the start and end of a filter, the only solution would be to use an external assigned variable or partial for all sub expressions which is cumbersome.

As it can’t support both use-cases I’m going to retain the existing behavior where all { } within a whitespace expression are replaced with {{ }} which means you wont be able to use '{0:00}' | fmt(it) inline and would need to either use an external variable, e.g:

{{ 1 | range(12) | assignTo: months }}
{{ '{0:00}' | assignTo: monthFormat }}
{{ months | select: { monthFormat  | fmt(it) }\n }}

Or one of the alternative solutions from my previous comment.

1 Like