Skip to content

Commit

Permalink
refactor: Refactor compare-ver common custom command (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcer authored Feb 2, 2025
1 parent ae091c1 commit 82f8792
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions nu/common.nu
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ export def has-ref [

# Compare two version number, return `1` if first one is higher than second one,
# Return `0` if they are equal, otherwise return `-1`
export def compare-ver [
from: string,
to: string,
] {
let dest = ($to | str downcase | str trim -c 'v' | str trim)
let source = ($from | str downcase | str trim -c 'v' | str trim)
# Ignore '-beta' or '-rc' suffix
let v1 = ($source | split row '.' | each {|it| ($it | parse -r '(?P<v>\d+)' | get v | get 0 )})
let v2 = ($dest | split row '.' | each {|it| ($it | parse -r '(?P<v>\d+)' | get v | get 0 )})
for $v in ($v1 | enumerate) {
let c1 = ($v1 | get -i $v.index | default 0 | into int)
let c2 = ($v2 | get -i $v.index | default 0 | into int)
if $c1 > $c2 {
return 1
} else if ($c1 < $c2) {
return (-1)
}
export def compare-ver [v1: string, v2: string] {
# Parse the version number: remove pre-release and build information,
# only take the main version part, and convert it to a list of numbers
def parse-ver [v: string] {
$v | str downcase | str trim -c v | str trim
| split row - | first | split row . | each { into int }
}
let a = parse-ver $v1
let b = parse-ver $v2
# Compare the major, minor, and patch parts; fill in the missing parts with 0
# If you want to compare more parts use the following code:
# for i in 0..([2 ($a | length) ($b | length)] | math max)
for i in 0..2 {
let x = $a | get -i $i | default 0
let y = $b | get -i $i | default 0
if $x > $y { return 1 }
if $x < $y { return (-1) }
}
return 0
0
}

# Compare two version number, return true if first one is lower then second one
Expand Down

0 comments on commit 82f8792

Please sign in to comment.