-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit. String with underscore
- Loading branch information
0 parents
commit 6cbd208
Showing
7 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Crutches | ||
======= | ||
|
||
The start of an Elixir ~~ripoff~~ port to the ActiveSupport Ruby gem. |
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,24 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
use Mix.Config | ||
|
||
# This configuration is loaded before any dependency and is restricted | ||
# to this project. If another project depends on this project, this | ||
# file won't be loaded nor affect the parent project. For this reason, | ||
# if you want to provide default values for your application for third- | ||
# party users, it should be done in your mix.exs file. | ||
|
||
# Sample configuration: | ||
# | ||
# config :logger, :console, | ||
# level: :info, | ||
# format: "$date $time [$level] $metadata$message\n", | ||
# metadata: [:user_id] | ||
|
||
# It is also possible to import configuration files, relative to this | ||
# directory. For example, you can emulate configuration per environment | ||
# by uncommenting the line below and defining dev.exs, test.exs and such. | ||
# Configuration from the imported file will override the ones defined | ||
# here (which is why it is important to import them last). | ||
# | ||
# import_config "#{Mix.env}.exs" |
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,2 @@ | ||
defmodule Crutches do | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule Crutches.String do | ||
def underscore(camel_case_string) do | ||
camel_case_string | ||
|> regex_replace(~r/\./, "/") | ||
|> regex_replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2") | ||
|> regex_replace(~r/([a-z\d])([A-Z])/, "\\1_\\2") | ||
|> regex_replace(~r/-/, "_") | ||
|> String.downcase | ||
end | ||
|
||
defp regex_replace(string, regex, replace) do | ||
Regex.replace(regex, string, replace) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Crutches.Mixfile do | ||
use Mix.Project | ||
|
||
def project do | ||
[app: :crutches, | ||
version: "0.0.1", | ||
elixir: "~> 1.0", | ||
build_embedded: Mix.env == :prod, | ||
start_permanent: Mix.env == :prod, | ||
deps: deps] | ||
end | ||
|
||
# Configuration for the OTP application | ||
# | ||
# Type `mix help compile.app` for more information | ||
def application do | ||
[applications: [:logger]] | ||
end | ||
|
||
# Dependencies can be Hex packages: | ||
# | ||
# {:mydep, "~> 0.3.0"} | ||
# | ||
# Or git/path repositories: | ||
# | ||
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} | ||
# | ||
# Type `mix help deps` for more examples and options | ||
defp deps do | ||
[] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Crutches.StringTest do | ||
alias Crutches.String | ||
use ExUnit.Case | ||
|
||
def compare string_pairs do | ||
string_pairs |> Enum.each fn([camel_case, underscore]) -> | ||
assert underscore == String.underscore camel_case | ||
end | ||
end | ||
|
||
test :camel_to_underscore do | ||
[ | ||
["Product", "product"], | ||
["SpecialGuest", "special_guest"], | ||
["ApplicationController", "application_controller"], | ||
["Area51Controller", "area51_controller"] | ||
] | ||
|> compare | ||
end | ||
|
||
test :camel_to_underscore_without_reverse do | ||
[ | ||
["HTMLTidy", "html_tidy"], | ||
["HTMLTidyGenerator", "html_tidy_generator"], | ||
["FreeBSD", "free_bsd"], | ||
["HTML", "html"], | ||
["ForceXMLController", "force_xml_controller"] | ||
] | ||
|> compare | ||
end | ||
|
||
test :camel_with_module_to_underscore_with_slash do | ||
[ | ||
["Admin.Product", "admin/product"], | ||
["Users.Commission.Department", "users/commission/department"], | ||
["UsersSection.CommissionDepartment","users_section/commission_department"] | ||
] | ||
|> compare | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |