Created
June 23, 2019 18:26
-
-
Save incredimike/9d0902616d127d9bf50cc976374cba8e to your computer and use it in GitHub Desktop.
catj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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