Skip to content

Commit

Permalink
Properly Count Emoji Characters(#2)
Browse files Browse the repository at this point in the history
* Emoji get properly count when evaluating the number of characters

* prepare for new release

* Updated README

* emoji-fix updated original author info

* emoji-fix test cases for emoji
  • Loading branch information
DrEtrange authored Mar 14, 2023
1 parent 8329aa3 commit a73a2f0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The package can be installed by adding `sms_part_counter` to your list of depend
```elixir
def deps do
[
{:sms_part_counter, "~> 0.1.3"}
{:sms_part_counter, "~> 0.1.7"}
]
end
```
Expand Down
7 changes: 5 additions & 2 deletions lib/sms_part_counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ defmodule SmsPartCounter do
"""
@spec count(binary) :: integer()
def count(str) when is_binary(str) do
String.codepoints(str)
|> Enum.count()
codepoints = String.to_charlist(str)

codepoints
|> Enum.count(fn cp -> cp > 0xFFFF end)
|> then(&(&1 + Enum.count(codepoints)))
end

@doc """
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule SmsPartCounter.MixProject do
def project do
[
app: :sms_part_counter,
version: "0.1.6",
version: "0.1.7",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -35,6 +35,7 @@ defmodule SmsPartCounter.MixProject do

defp package() do
[
maintainers: ["m4hi2", "Rum&Code"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/m4hi2/sms-counter"}
]
Expand Down
8 changes: 8 additions & 0 deletions test/sms_part_counter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ defmodule SmsPartCounterTest do
assert SmsPartCounter.count(" ") == 1
assert SmsPartCounter.count("আমি তুমি") == 8
end

test "can count emoji characters" do
assert SmsPartCounter.count("👨‍👨‍👧‍👧") == 11
assert SmsPartCounter.count("😃") == 2
assert SmsPartCounter.count("😃🥃💻") == 6
assert SmsPartCounter.count("👨‍👨‍👧‍👧😃") == 13
assert SmsPartCounter.count("👨‍👨‍👧‍👧😃👨‍👨‍👧‍👧") == 24
end
end

describe "GSM 7bit encoding SMS part counter" do
Expand Down

0 comments on commit a73a2f0

Please sign in to comment.