Last active
January 6, 2022 12:18
-
-
Save davit-khaburdzania/16582c3a637e9de48fa2ab76e611e4e6 to your computer and use it in GitHub Desktop.
Rails titleize method port to Phoenix
This file contains hidden or 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
defmodule MyApp.StringHelperTest do | |
use MyApp.DataCase | |
alias MyApp.StringHelpers | |
describe "titleize/1 with valid string input" do | |
test "returns titleized version of string" do | |
assert "Man From The Boondocks" == StringHelpers.titleize("man from the boondocks") | |
assert "The Man Without A Past" == StringHelpers.titleize("TheManWithoutAPast") | |
assert "Raiders Of The Lost Ark" == StringHelpers.titleize("raiders_of_the_lost_ark") | |
assert "String Ending With" == StringHelpers.titleize("string_ending_with_id") | |
end | |
end | |
end |
This file contains hidden or 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
defmodule MyApp.StringHelpers do | |
@moduledoc """ | |
Helper methods for working with strings | |
""" | |
def titleize(str) do | |
str | |
|> Phoenix.Naming.underscore() | |
|> Phoenix.Naming.humanize() | |
|> String.replace(~r/\b(?<!\w['â`])[a-z]/, &String.capitalize/1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment