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

Add optional multibyte characters support, closes #21 #22

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ The package can be installed by adding `owl` to your list of dependencies in `mi
```elixir
def deps do
[
{:owl, "~> 0.8"}
{:owl, "~> 0.8"},
# ucwidth is an optional dependency, uncomment it for multibyte characters support (emoji, etc)
# {:ucwidth, "~> 0.2"}
]
end
```
Expand Down
14 changes: 12 additions & 2 deletions lib/owl/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,20 @@ defmodule Owl.Data do

iex> Owl.Data.length(["222", Owl.Data.tag(["333", "444"], :green)])
9

# if ucwidth dependency is present, then it is used to calculate the length of the string
iex> Owl.Data.length("😂")
2
"""
@spec length(t()) :: non_neg_integer()
def length(data) when is_binary(data) do
String.length(data)
if Code.ensure_loaded?(Ucwidth) do
def length(data) when is_binary(data) do
Ucwidth.width(data)
end
else
def length(data) when is_binary(data) do
String.length(data)
end
end

def length(data) when is_list(data) do
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ defmodule Owl.MixProject do

defp deps do
[
{:ucwidth, "~> 0.2", optional: true},
{:ex_doc, "~> 0.24", only: :dev, runtime: false},
{:excoveralls, "~> 0.10", only: :test},
{:dialyxir, "~> 1.0", only: :dev, runtime: false}
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"ucwidth": {:hex, :ucwidth, "0.2.0", "1f0a440f541d895dff142275b96355f7e91e15bca525d4a0cc788ea51f0e3441", [:mix], [], "hexpm", "c1efd1798b8eeb11fb2bec3cafa3dd9c0c3647bee020543f0340b996177355bf"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}
Loading