Skip to content

Instantly share code, notes, and snippets.

@incredimike
Created June 23, 2019 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incredimike/9d0902616d127d9bf50cc976374cba8e to your computer and use it in GitHub Desktop.
Save incredimike/9d0902616d127d9bf50cc976374cba8e to your computer and use it in GitHub Desktop.
catj
#!/usr/bin/jq -jf
# Original Author: jolmg on HN (https://news.ycombinator.com/item?id=20246727)
#
#
#
# Author notes:
# 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.
# https://news.ycombinator.com/item?id=20246727
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)
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment