ps/Modules/Alkami.PowerShell.Common/Public/Compare-MetadataPart.ps1

56 lines
1.6 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Compare-MetadataPart {
<#
.SYNOPSIS
Compare two version metadata parts for equivalence using the same pattern as Compare-SemVer.
This function principally intended to be called from Compare-SemVer, prefer that over this.
.PARAMETER Version1Part
[string] A fragment of a version for comparing/evaluating just the fragment of the metadata
.PARAMETER Version2Part
[string] A fragment of a version for comparing/evaluating just the fragment of the metadata
#>
[CmdletBinding()]
[OutputType([int])]
param(
[string]$Version1Part,
[string]$Version2Part
)
if ((-not $Version1Part) -and (-not $Version2Part)) {
return 0
}
# For release part, 1.0.0 is newer/greater then 1.0.0-alpha. So return 1 here.
if ((-not $Version1Part) -and $Version2Part) {
return 1
}
if (($Version1Part) -and (-not $Version2Part)) {
return -1
}
$version1Num = 0
$version2Num = 0
$v1IsNumeric = [System.Int32]::TryParse($Version1Part, [ref] $version1Num);
$v2IsNumeric = [System.Int32]::TryParse($Version2Part, [ref] $version2Num);
$result = 0
# if both are numeric compare them as numbers
if ($v1IsNumeric -and $v2IsNumeric) {
$result = $version1Num.CompareTo($version2Num);
} elseif ($v1IsNumeric -or $v2IsNumeric) {
# numeric numbers come before alpha chars
if ($v1IsNumeric) {
return -1
} else {
return 1
}
} else {
$result = [string]::Compare($Version1Part, $Version2Part)
}
return $result
}