7 October 2023
Disproportionately Satisfying Programs
I find some little programs/scripts/snippets disproportionately satisfying to write/use. The best programs are ones that I never intend to share with anyone else.
The following program is one I wrote recently that falls into this category. All it does is convert lines of CSS into React inline styles, and it took only a few minutes to write.
sed ':a s/-\(\w\)\(.*:\)/\u\1\2/g;t a;s/\: *\(.[^;]*\);/: "\1",/'
Breakdown/Explanation:
# (For each line of the input)
# Set label a
:a
# For the content before the first colon,
# discard the first `-`, and capitalise the letter after it
s/-\(\w\)\(.*:\)/\u\1\2/;
# Jump back to label a if any replacements were made
t a;
# Double-Quote the CSS value, and replace the EOL semicolon with a comma
s/\: *\(.[^;]*\);/: "\1",/
I actually use it within vim, using the following shortcut:
xnoremap <leader>css :!sed ':a s/-\(\w\)\(.*:\)/\u\1\2/;t a;s/\: *\(.[^;]*\);/: "\1",/'<CR><CR>