<commit_before>package Pakket::Versioning;
# ABSTRACT: A versioning class

use Moose;
use MooseX::StrictConstructor;
use Carp            qw< croak >;
use Log::Any        qw< $log >;
use Module::Runtime qw< require_module >;

has 'type' => (
    'is'       => 'ro',
    'isa'      => 'PakketVersioning',
    'coerce'   => 1,
    'required' => 1,
    'handles'  => [ 'compare' ],
);

sub parse_req_string {
    my ( $self, $req_string ) = @_;

    # A filter string is a comma-separated list of conditions
    # A condition is of the form "OP VER"
    # OP is >=, <=, !=, ==, >, <
    # VER is a version string valid for the version module
    # Whitespace is ignored
    my @conditions = split /,/xms, $req_string;
    my @filters;
    foreach my $condition (@conditions) {
        if ( $condition !~ /^\s*(>=|<=|==|!=|>|<)\s*(\S*)\s*$/xms ) {
            $condition = ">= $condition";
        }

        my @filter = $condition =~ /^\s*(>=|<=|==|!=|>|<)\s*(\S*)\s*$/xms;
        push @filters, \@filter;
    }

    return \@filters;
}

my %op_map = (
    '>=' => sub { $_[0] >= 0 },
    '<=' => sub { $_[0] <= 0 },
    '==' => sub { $_[0] == 0 },
    '!=' => sub { $_[0] != 0 },
    '>'  => sub { $_[0] >  0 },
    '<'  => sub { $_[0] <  0 },
);

sub filter_version {
    my ( $self, $req_string, $versions ) = @_;

    foreach my $filter ( @{ $self->parse_req_string($req_string) } ) {
        my ( $op, $req_version ) = @{$filter};

        @{$versions}
            = grep +( $op_map{$op}->( $self->compare( $_, $req_version ) ) ),
            @{$versions};
    }

    return;
}

sub latest {
    my ( $self, $category, $name, $req_string, @versions ) = @_;

    # Filter all @versions based on $req_string
    $self->filter_version( $req_string, \@versions );

    @versions
        or croak( $log->criticalf('No versions provided for %s/%s', $category, $name) );

    # latest_version
    my $latest;
    foreach my $version (@versions) {
        if ( !defined $latest ) {
            $latest = $version;
            next;
        }

        if ( $self->compare( $latest, $version ) < 0 ) {
            $latest = $version;
        }
    }

    return $latest;
}

sub is_satisfying {
    my ( $self, $req_string, @versions ) = @_;

    # Filter all @versions based on $req_string
    $self->filter_version( $req_string, \@versions );

    return !!( @versions > 0 );
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;
<commit_msg>Refactor regex<commit_after>package Pakket::Versioning;
# ABSTRACT: A versioning class

use Moose;
use MooseX::StrictConstructor;
use Carp            qw< croak >;
use Log::Any        qw< $log >;
use Module::Runtime qw< require_module >;

has 'type' => (
    'is'       => 'ro',
    'isa'      => 'PakketVersioning',
    'coerce'   => 1,
    'required' => 1,
    'handles'  => [ 'compare' ],
);

sub parse_req_string {
    my ( $self, $req_string ) = @_;

    # A filter string is a comma-separated list of conditions
    # A condition is of the form "OP VER"
    # OP is >=, <=, !=, ==, >, <
    # VER is a version string valid for the version module
    # Whitespace is ignored
    my @conditions = split /,/xms, $req_string;
    my $cond_regex = qr/^ \s* (>=|<=|==|!=|>|<) \s* (\S*) \s* $/xms;
    my @filters;
    foreach my $condition (@conditions) {
        if ( $condition !~ $cond_regex ) {
            $condition = ">= $condition";
        }

        my @filter = $condition =~ $cond_regex;
        push @filters, \@filter;
    }

    return \@filters;
}

my %op_map = (
    '>=' => sub { $_[0] >= 0 },
    '<=' => sub { $_[0] <= 0 },
    '==' => sub { $_[0] == 0 },
    '!=' => sub { $_[0] != 0 },
    '>'  => sub { $_[0] >  0 },
    '<'  => sub { $_[0] <  0 },
);

sub filter_version {
    my ( $self, $req_string, $versions ) = @_;

    foreach my $filter ( @{ $self->parse_req_string($req_string) } ) {
        my ( $op, $req_version ) = @{$filter};

        @{$versions}
            = grep +( $op_map{$op}->( $self->compare( $_, $req_version ) ) ),
            @{$versions};
    }

    return;
}

sub latest {
    my ( $self, $category, $name, $req_string, @versions ) = @_;

    # Filter all @versions based on $req_string
    $self->filter_version( $req_string, \@versions );

    @versions
        or croak( $log->criticalf('No versions provided for %s/%s', $category, $name) );

    # latest_version
    my $latest;
    foreach my $version (@versions) {
        if ( !defined $latest ) {
            $latest = $version;
            next;
        }

        if ( $self->compare( $latest, $version ) < 0 ) {
            $latest = $version;
        }
    }

    return $latest;
}

sub is_satisfying {
    my ( $self, $req_string, @versions ) = @_;

    # Filter all @versions based on $req_string
    $self->filter_version( $req_string, \@versions );

    return !!( @versions > 0 );
}

no Moose;
__PACKAGE__->meta->make_immutable;

1;

{'fraction_non_alphanumeric': 0.1928746928746929, 'fraction_numerical': 0.006961506961506962, 'mean_word_length': 2.265374331550802, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5866723', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1728, 'n_words': 425}