Skip to content

Commit

Permalink
fix: quantities starting with a dot .33L (#9284)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet authored Nov 13, 2023
1 parent 8638175 commit 5523a15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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
11 changes: 10 additions & 1 deletion tests/unit/units.t
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ 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);
is(normalize_quantity(".33 l"), 330);
is(normalize_serving_size(".33L"), 330);
is(normalize_serving_size(".33 l"), 330);
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 +163,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();

0 comments on commit 5523a15

Please sign in to comment.