Skip to content

Instantly share code, notes, and snippets.

@Koriit
Created February 8, 2019 13:47
Show Gist options
  • Save Koriit/f38c1c6ef89dde6fec0cea3af5d36708 to your computer and use it in GitHub Desktop.
Save Koriit/f38c1c6ef89dde6fec0cea3af5d36708 to your computer and use it in GitHub Desktop.
To camel case function for JQ
def to_camel_case: . | ascii_downcase | split("_") | .[0:1] as $first | .[1:] | map(.[1:] as $rest | .[0:1] | ascii_upcase | . + $rest) | $first + . | join("");
@reegnz
Copy link

reegnz commented Jan 29, 2021

A somewhat shorter solution:

def to_camel_case: ascii_downcase | split("_") | .[0:1] + (.[1:] | map( (.[0:1] | ascii_upcase ) + .[1:])) | join("");

A longer, but more readable solution:

def head:
  .[0:1];
  
def tail:
  .[1:];
  
def capitalize:
  ( head | ascii_upcase ) + tail;

def to_camel_case: ascii_downcase | split("_") | head + ( tail | map(capitalize))) | join("");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment