Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.

snake_case method better handles numbers. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/extlib/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def unescape_regexp
def snake_case
return downcase if match(/\A[A-Z]+\z/)
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z])([A-Z])/, '\1_\2').
gsub(/([a-z0-9])([A-Z])/, '\1_\2').
downcase
end

Expand Down
14 changes: 14 additions & 0 deletions spec/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
it "leaves CamelCase as is" do
"TestController".camel_case.should == "TestController"
end

it "handle when section ends in a number" do
"ec2_instance".camel_case.should == "Ec2Instance"
"ec2".camel_case.should == "Ec2"
"s3_bucket_name".camel_case.should == "S3BucketName"
end
end


Expand All @@ -79,6 +85,8 @@
"CNNNews".snake_case.should == "cnn_news"
"HeadlineCNNNews".snake_case.should == "headline_cnn_news"
"NameACRONYM".snake_case.should == "name_acronym"
"EC2".snake_case.should == "ec2"
"EC2Instance".snake_case.should == "ec2_instance"
end

it "does NOT change one word lowercase" do
Expand All @@ -88,6 +96,12 @@
it "leaves snake_case as is" do
"merb_core".snake_case.should == "merb_core"
end

it "detects a capital after a number" do
"Ec2Instance".snake_case.should == "ec2_instance"
"Route53".snake_case.should == "route53"
"S3BucketName".snake_case.should == "s3_bucket_name"
end
end


Expand Down