2 ms·
It occurs to me that a better way to blacklist would be something like: jq -r ' tostream | select(length > 1) | ( .[0] | map( if to
by jolmg 7y ago
It occurs to me that a better way to blacklist would be something like:
jq -r '
tostream
| select(length > 1)
| (
.[0] | map(
if tostring | (
test("^[A-Za-z$_][0-9A-Za-z$_]*$")
and (
. as $property
| ["if", "else"] | all(. != $property)
)
)
then "." + .
else "[" + @json + "]"
end
) | join("")
) + " = " + (.[1] | @json)
'
You whitelist against what the syntax allows for identifiers and then you blacklist reserved keywords. Writing it this way makes it easier to verify for correctness when comparing with the ECMAScript Specs. This is still a non-exhaustive blacklist and the whitelist regex lacks allowed unicode characters.