Skip to content

Instantly share code, notes, and snippets.

@davit-khaburdzania
Last active January 6, 2022 12:18
Show Gist options
  • Save davit-khaburdzania/16582c3a637e9de48fa2ab76e611e4e6 to your computer and use it in GitHub Desktop.
Save davit-khaburdzania/16582c3a637e9de48fa2ab76e611e4e6 to your computer and use it in GitHub Desktop.
Rails titleize method port to Phoenix
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
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