Skip to content

Commit

Permalink
Merge pull request #4 from MBXSystems/feature/leading-zeroes
Browse files Browse the repository at this point in the history
Trim zeroes in parsed inputs
  • Loading branch information
rgtj2 authored Apr 22, 2024
2 parents 9849770 + cbc27c4 commit 82d8af9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/nested_lines.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ defmodule NestedLines do
defp parse_input(line) when is_binary(line) do
line
|> String.split(".", trim: true)
|> remove_leading_zeros()
|> convert_to_binary_list([])
end

Expand All @@ -53,6 +54,13 @@ defmodule NestedLines do
convert_to_binary_list(tail, [0 | list])
end

@spec remove_leading_zeros(list(String.t())) :: list(String.t())
defp remove_leading_zeros(line) do
line
|> Enum.map(&String.replace_leading(&1, "0", ""))
|> Enum.reject(&(&1 == ""))
end

@doc """
Output a string representation of the line numbers.
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule NestedLines.MixProject do
def project do
[
app: :nested_lines,
version: "0.1.2",
version: "0.1.3",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
description: description(),
Expand Down
5 changes: 5 additions & 0 deletions test/nested_lines_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ defmodule NestedLinesTest do
input = NestedLines.new!(["1", "1.1", "1.2"])
assert %NestedLines{lines: [[1], [0, 1], [0, 1]]} = input
end

test "~W(1 1.00 1.01 1.02 1.03) returns [[1], [1], [0, 1], [0, 1], [0, 1]]" do
input = NestedLines.new!(["1", "1.00", "1.01", "1.02", "1.03"])
assert %NestedLines{lines: [[1], [1], [0, 1], [0, 1], [0, 1]]} = input
end
end

describe "parsing numeric values" do
Expand Down

0 comments on commit 82d8af9

Please sign in to comment.