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

fix: quantities starting with a dot .33L #9284

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions lib/ProductOpener/Numbers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,31 @@ BEGIN {
&remove_insignificant_digits
&convert_string_to_number
$number_regexp
); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}

use vars @EXPORT_OK;

=head1 VARIABLES
=head2 $number_regexp
Regular expression to match something that looks like a number:
32
32.5
0.5
.5
32,5
=cut

# dot followed by digits,
# or digits followed by a dot or a comma, optionnaly followed by 0 or more digits
$number_regexp = '\.\d+|\d+(?:(?:\,|\.)\d+)?';

=head1 FUNCTIONS
=head2 remove_insignificant_digits($)
Expand Down
6 changes: 3 additions & 3 deletions lib/ProductOpener/Units.pm
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ sub normalize_quantity ($quantity) {
# 10 unités, 170 g
# 4 bouteilles en verre de 20cl
if ($quantity
=~ /(?<number>\d+)(\s(\p{Letter}| )+)?(\s)?( de | of |x|\*)(\s)?(?<quantity>(\d+)(\.|,)?(\d+)?)(\s)?(?<unit>$units_regexp)\b/i
=~ /(?<number>\d+)(\s(\p{Letter}| )+)?(\s)?( de | of |x|\*)(\s)?(?<quantity>$number_regexp)(\s)?(?<unit>$units_regexp)\b/i
)
{
my $m = $+{number};
Expand All @@ -254,7 +254,7 @@ sub normalize_quantity ($quantity) {
$q = convert_string_to_number($q);
$q = unit_to_g($q * $m, $u);
}
elsif ($quantity =~ /(?<quantity>(\d+)(\.|,)?(\d+)?)(\s)?(?<unit>$units_regexp)\s*\b/i) {
elsif ($quantity =~ /(?<quantity>$number_regexp)(\s)?(?<unit>$units_regexp)\s*\b/i) {
$q = lc($+{quantity});
$u = $+{unit};
$q = convert_string_to_number($q);
Expand All @@ -276,7 +276,7 @@ sub normalize_serving_size ($serving) {

# Regex captures any <number>( )?<unit-identifier> group, but leaves allowances for a preceding
# token to allow for patterns like "One bag (32g)", "1 small bottle (180ml)" etc
if ($serving =~ /^(.*[ \(])?(?<quantity>(\d+)(\.|,)?(\d+)?)( )?(?<unit>$units_regexp)\b/i) {
if ($serving =~ /^(.*[ \(])?(?<quantity>$number_regexp)( )?(?<unit>$units_regexp)\b/i) {
my $q = $+{quantity};
my $u = $+{unit};
$q = convert_string_to_number($q);
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/units.t
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ is(normalize_quantity("2 kgr"), 2000);
is(normalize_quantity("2 kilogramme"), 2000);
is(normalize_quantity("2 kilogrammes"), 2000);

# . without a 0 before
is(normalize_quantity(".33L"), 330);
stephanegigandet marked this conversation as resolved.
Show resolved Hide resolved
is(normalize_serving_size(".33L"), 330);
stephanegigandet marked this conversation as resolved.
Show resolved Hide resolved
is(normalize_serving_size("5 bottles (.33L)"), 330);
is(normalize_serving_size("5 bottles .33L"), 330);
is(normalize_serving_size("5 bottles2.33L"), undef); # Broken string, missing word separator before number

my @serving_sizes = (
["100g", "100"],
["250 g", "250"],
Expand All @@ -154,7 +161,7 @@ my @serving_sizes = (
);

foreach my $test_ref (@serving_sizes) {
is(normalize_serving_size($test_ref->[0]), $test_ref->[1]);
is(normalize_serving_size($test_ref->[0]), $test_ref->[1]) or diag explain $test_ref;
}

done_testing();
Loading