This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Lita::Util.underscore for robust namespace generation.
- Loading branch information
1 parent
62b3857
commit 66a6735
Showing
5 changed files
with
47 additions
and
1 deletion.
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
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,20 @@ | ||
module Lita | ||
module Util | ||
ACRONYM_REGEX = /(?=a)b/ | ||
|
||
class << self | ||
def underscore(camel_cased_word) | ||
word = camel_cased_word.to_s.dup | ||
word.gsub!('::', '/') | ||
word.gsub!(/(?:([A-Za-z\d])|^)(#{ACRONYM_REGEX})(?=\b|[^a-z])/) do | ||
"#{$1}#{$1 && '_'}#{$2.downcase}" | ||
end | ||
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2') | ||
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') | ||
word.tr!("-", "_") | ||
word.downcase! | ||
word | ||
end | ||
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 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,9 @@ | ||
require "spec_helper" | ||
|
||
describe Lita::Util do | ||
describe ".underscore" do | ||
it "converts camel cased strings into snake case" do | ||
expect(described_class.underscore("FooBarBaz")).to eq("foo_bar_baz") | ||
end | ||
end | ||
end |