Skip to content

Instantly share code, notes, and snippets.

@symmetriq
Created November 13, 2017 01:48
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 symmetriq/10aa4cfa2b2bf714070e721737b2e5ef to your computer and use it in GitHub Desktop.
Save symmetriq/10aa4cfa2b2bf714070e721737b2e5ef to your computer and use it in GitHub Desktop.
BBEdit: Toggle CamelCase -
#!/usr/bin/env ruby
#-------------------------------------------------------------------------------
# Toggle Camelcase -
#-------------------------------------------------------------------------------
# Jason Sims <jason@symmetriq.com>
#-------------------------------------------------------------------------------
#
# Toggles between camelcase and hyphens
# e.g. doTheThing <-> do-the-thing
#
#-------------------------------------------------------------------------------
#
# Installation in BBEdit:
#
# 1. Place this file in your "Text Filters" directory:
# ~/Library/Application Support/BBEdit/Text Filters/
# or
# ~/Dropbox/Application Support/BBEdit/Text Filters/
#
# 2. Assign a keyboard shortcut to "Toggle CamelCase -" in:
# BBEdit → Preferences → Menus & Shortcuts → Text → Apply Text Filter
#
#-------------------------------------------------------------------------------
# More scripts & snippets here:
# https://gist.github.com/symmetriq
#-------------------------------------------------------------------------------
input = ARGF.set_encoding('UTF-8').read
if input.include? '-'
# has hyphens; convert to camelCase
print input.gsub(/_[A-Za-z]/) {|char| char[1].upcase }
else
# treat as camelCase (nothing will happen if the string contains no capital letters)
print input.gsub(/([A-Z])/, '-\1').downcase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment