Skip to content

Commit

Permalink
Remove leading zeroes in parsed inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Hixson committed Apr 18, 2024
1 parent 9849770 commit 2bde30c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
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
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 2bde30c

Please sign in to comment.