Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unicode regular expressions #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/slugger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Slugger do
text
|> handle_possessives
|> replace_special_chars
|> remove_unwanted_chars(separator, ~r/([^A-Za-z0-9가-힣])+/)
|> remove_unwanted_chars(separator, ~r/([^A-Za-z0-9가-힣])+/u)
end

@doc """
Expand All @@ -66,7 +66,7 @@ defmodule Slugger do
|> handle_possessives
|> replace_special_chars
|> String.downcase
|> remove_unwanted_chars(separator, ~r/([^a-z0-9가-힣])+/)
|> remove_unwanted_chars(separator, ~r/([^a-z0-9가-힣])+/u)
end

@spec remove_unwanted_chars(text :: String.t, separator :: char, pattern :: Regex.t) :: String.t
Expand Down
14 changes: 11 additions & 3 deletions test/slugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ defmodule SluggerTest do
end

test "removing space at ending and ending with korean chars" do
assert Slugger.slugify(" \n \t \n ㅈㅓㅇㅅㅜㅇㅕㄴ for 정수연 \n \t \n ") == "ㅈㅓㅇㅅㅜㅇㅕㄴ-for-정수연"
assert Slugger.slugify(" \n \t \n 정수연 for 정수연 \n \t \n ") == "정수연-for-정수연"
end

test "replace whitespace inside with seperator" do
assert Slugger.slugify(" A B C ") == "A-B-C"
assert Slugger.slugify(" A B C ", ?_) == "A_B_C"
end

test "replace multiple seperator inside and outside" do
assert Slugger.slugify("--a--b c - - - ") == "a-b-c"
end
Expand All @@ -44,8 +44,12 @@ defmodule SluggerTest do
assert Slugger.slugify("Sheep's Milk") == "Sheeps-Milk"
end

test "removing unwanted unicode characters" do
assert Slugger.slugify("abc 😀") == "abc"
end

#--- slugify_downcase()

test "string to lower" do
assert Slugger.slugify_downcase("ABC") == "abc"
end
Expand All @@ -71,6 +75,10 @@ defmodule SluggerTest do
assert Slugger.slugify_downcase("Sheep's Milk") == "sheeps-milk"
end

test "removing unwanted unicode characters lowercase" do
assert Slugger.slugify_downcase("abc 😀") == "abc"
end

#--- Naughty strings

test "naughty strings" do
Expand Down