Skip to content

Commit

Permalink
Feature: sort --reverse (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskilding authored Mar 7, 2019
1 parent 3583753 commit 115c89e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
36 changes: 23 additions & 13 deletions semver
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ sub usage() {
say STDERR " $program increment-minor <version>";
say STDERR " $program increment-patch <version>";
say STDERR " $program init";
say STDERR " $program sort -";
say STDERR " $program sort [-r | --reverse] -";
say STDERR " $program validate <string>";
say STDERR " $program (-h | --help)";
say STDERR " $program [-h | --help]";
say STDERR "";
say STDERR "Options:";
say STDERR " -h --help Show this help screen.";
Expand Down Expand Up @@ -299,7 +299,9 @@ sub semver::init {
say "0.0.0";
}

sub semver::sort {
sub semver::sort(&@) {
my ($sort_fn) = @_;

my @lines = <STDIN>;

chomp(@lines);
Expand All @@ -309,21 +311,25 @@ sub semver::sort {
exit 1;
}

@lines = sort {
my $result = semver::compare($a, $b);

if ($result eq $eq) {
return $a cmp $b;
} else {
return $result;
}
} @lines;
@lines = sort $sort_fn @lines;

foreach my $line (@lines) {
say $line;
}
}

sub semver::sort_pair {
my ($a, $b) = @_;

my $result = semver::compare($a, $b);

if ($result eq $eq) {
return $a cmp $b;
} else {
return $result;
}
}

sub semver::validate {
my ($str) = @_;

Expand Down Expand Up @@ -368,7 +374,11 @@ sub main {
} elsif ($subcommand eq "increment-patch") {
semver::increment_patch($args[1]);
} elsif ($subcommand eq "sort") {
semver::sort();
if (grep { $_ eq "-r" || $_ eq "--reverse" } @args) {
semver::sort { semver::sort_pair($b, $a) };
} else {
semver::sort { semver::sort_pair($a, $b) };
}
} elsif ($subcommand eq "validate") {
semver::validate($args[1]);
} elsif ($subcommand eq "compare") {
Expand Down
23 changes: 13 additions & 10 deletions semver.1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ increment-patch
init
.Nm
sort
.Op Fl r | Fl -reverse
.Fl
.Nm
validate
Expand Down Expand Up @@ -171,9 +172,9 @@ can get the MINOR component of a
.Ar version
string.
.Pp
If
The operation will fail if
.Ar version
is invalid, nothing will be printed, and the utility will exit with an error code.
is invalid.
.Ss get-patch
.Nm
can get the PATCH component of a
Expand Down Expand Up @@ -219,9 +220,7 @@ is invalid.
.El
.Ss grep
.Nm
can parse text from STDIN to extract zero or more Semantic Version strings, in the style of
.Sq grep
\&. It will tolerate quite noisy input.
can parse text from STDIN to extract zero or more Semantic Version strings. It will tolerate quite noisy input.
.Pp
A candidate version
.Sq string
Expand Down Expand Up @@ -289,11 +288,15 @@ can print the minimum Semantic Version
to STDOUT. You can use this as a base-case initializer, for example in a script which fails to find any Semantic Versions in its input.
.Ss sort
.Nm
can order a list of line-delimited Semantic Version strings from STDIN by precedence, in the style of
.Sq sort
\&.
can sort a list of line-delimited Semantic Version strings from STDIN in precedence order (low-to-high).
.Pp
The sort subcommand understands the following options:
.Bl -tag -width indent
.It Fl r, Fl -reverse
Sort in reverse order (high-to-low).
.El
.Pp
If the input is invalid (i.e. it contains anything besides Semantic Versions and line delimiter characters), nothing will be printed, and the utility will exit with an error code.
The operation will fail if the input is invalid (i.e. it contains anything besides Semantic Versions and line delimiter characters).
.Pp
Note: Some aspects of Semantic Version ordering are undefined in the specification. The
.Nm
Expand All @@ -319,7 +322,7 @@ The
.Nm
utility understands the following command-line options:
.Bl -tag -width indent
.It Fl h, Fl help
.It Fl h, Fl -help
Display the usage screen.
.El
.Sh EXAMPLES
Expand Down
20 changes: 20 additions & 0 deletions test/sort.bats
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,24 @@ EOF
)

[[ "$(semver sort <<< "$input")" = "$expected" ]]
}

@test "sort: should reverse ordering with the -r (--reverse) flag" {
input=$(cat <<EOF
1.0.0
2.0.0
1.0.0-SNAPSHOT
1.0.0+2008
EOF
)

expected=$(cat <<EOF
2.0.0
1.0.0+2008
1.0.0
1.0.0-SNAPSHOT
EOF
)

[[ "$(semver sort -r <<< "$input")" = "$expected" ]] && [[ "$(semver sort --reverse <<< "$input")" = "$expected" ]]
}

0 comments on commit 115c89e

Please sign in to comment.