From aeb6daf55e4ddec83db638b70391476fbcc13abd Mon Sep 17 00:00:00 2001 From: Cesar Navarro Araya Date: Wed, 19 Feb 2025 17:45:46 -0600 Subject: [PATCH] Fix issue with NumberCompactor.compactable? when the string is not a number representation --- lib/pdf417/number_compactor.ex | 2 +- test/number_compactor_test.exs | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/pdf417/number_compactor.ex b/lib/pdf417/number_compactor.ex index 31bf864..048b3da 100644 --- a/lib/pdf417/number_compactor.ex +++ b/lib/pdf417/number_compactor.ex @@ -9,7 +9,7 @@ defmodule PDF417.NumberCompactor do end def compactable?(part) do - String.length(part) > 13 && Integer.parse(part) != :error + String.length(part) > 13 && String.match?(part, ~r/^\d+$/) end def codeword, do: 902 diff --git a/test/number_compactor_test.exs b/test/number_compactor_test.exs index 753dce6..958d771 100644 --- a/test/number_compactor_test.exs +++ b/test/number_compactor_test.exs @@ -19,10 +19,18 @@ defmodule PDF417.NumberCompactorTest do assert NumberCompactor.compactable?("12345678912345") end - test "returns false for strings" do + test "returns false for short strings" do refute NumberCompactor.compactable?("asdf") end + test "returns false for long strings" do + refute NumberCompactor.compactable?("stringgreaterthanthirteencharacters") + end + + test "returns false for strings that does not represent a number" do + refute NumberCompactor.compactable?("123abcdefghijkl") + end + test "returns false for short numbers" do refute NumberCompactor.compactable?("123") end