forked from savonrb/nori
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop monkey-patching String with #snakecase (savonrb#102)
- Loading branch information
Showing
8 changed files
with
55 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
require "nori/core_ext/string" | ||
require "nori/string_utils" | ||
require "nori/core_ext/hash" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Nori | ||
module StringUtils | ||
# Converts a string to snake case. | ||
# | ||
# @param inputstring [String] The string to be converted to snake case. | ||
# @return [String] A copy of the input string converted to snake case. | ||
def self.snakecase(inputstring) | ||
str = inputstring.dup | ||
str.gsub!(/::/, '/') | ||
str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') | ||
str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') | ||
str.tr!(".", "_") | ||
str.tr!("-", "_") | ||
str.downcase! | ||
str | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "spec_helper" | ||
|
||
describe Nori::StringUtils do | ||
|
||
describe ".snakecase" do | ||
it "lowercases one word CamelCase" do | ||
expect(Nori::StringUtils.snakecase("Merb")).to eq("merb") | ||
end | ||
|
||
it "makes one underscore snakecase two word CamelCase" do | ||
expect(Nori::StringUtils.snakecase("MerbCore")).to eq("merb_core") | ||
end | ||
|
||
it "handles CamelCase with more than 2 words" do | ||
expect(Nori::StringUtils.snakecase("SoYouWantContributeToMerbCore")).to eq("so_you_want_contribute_to_merb_core") | ||
end | ||
|
||
it "handles CamelCase with more than 2 capital letter in a row" do | ||
expect(Nori::StringUtils.snakecase("CNN")).to eq("cnn") | ||
expect(Nori::StringUtils.snakecase("CNNNews")).to eq("cnn_news") | ||
expect(Nori::StringUtils.snakecase("HeadlineCNNNews")).to eq("headline_cnn_news") | ||
end | ||
|
||
it "does NOT change one word lowercase" do | ||
expect(Nori::StringUtils.snakecase("merb")).to eq("merb") | ||
end | ||
|
||
it "leaves snake_case as is" do | ||
expect(Nori::StringUtils.snakecase("merb_core")).to eq("merb_core") | ||
end | ||
end | ||
|
||
end |