Skip to content

Instantly share code, notes, and snippets.

@kaibadash
Last active November 6, 2018 01:03
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 kaibadash/8a44be31e75ee43a2acc44866832329f to your computer and use it in GitHub Desktop.
Save kaibadash/8a44be31e75ee43a2acc44866832329f to your computer and use it in GitHub Desktop.
Convert `export class hoge_moge` to `export class HogeMoge`
# How to use:
# find ./src -type f -name "*ts" | ruby camelize.rb
def capitalize(str)
str.split("_").map(&:capitalize).join
end
while (file = gets) do
output = ""
file.chomp!
p "start: #{file}"
File.open(file, mode = "rt") do |f|
f.each_line do |line|
if line.include? "export class"
line.gsub(/export class (\w+)/, "\1")
output += "export class #{capitalize($1)} {\n"
next
end
line.gsub(/import { (\w+) } from/, "\1")
unless $1.nil?
cls = capitalize($1)
output += "import { #{cls} } from \"./#{cls}\";\n"
next
end
line.gsub(/(.+): (\w+)(.+)/, "\1\2\3")
if $2 != nil && !$2.include?("number") && !$2.include?("true") && !$2.include?("false") && !$2.include?("string") && !$2.include?("bool")
cls = capitalize($2)
output += "#{$1}: #{cls}#{$3}\n"
next
end
output += line
end
end
File.open(file, mode = "w") do |f|
f.puts output
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment