Skip to content

Commit

Permalink
new NinStringComparer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Sep 22, 2023
1 parent ebfd039 commit a7230eb
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
109 changes: 109 additions & 0 deletions public_autoloader/New-NinStringComparer.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

if ( $script:publicToExport) {
$script:publicToExport.function += @(
'New-NinStringComparer'
)
$script:publicToExport.alias += @(
'Nin.StringComparer'
)
}

function New-NinStringComparer {
<#
.SYNOPSIS
quickly call a string comparison, or return the comparer type as a instance
.NOTES
future: add support for CultureInfo options
.EXAMPLE
Dotils.New-StringComparer 'a' 'A' OrdinalIgnoreCase, InvariantCulture |Ft -AutoSize
CompareName CompareFunc Left Right Result
----------- ----------- ---- ----- ------
OrdinalIgnoreCase System.OrdinalIgnoreCaseComparer a A 0
InvariantCulture System.CultureAwareComparer a A -1
.EXAMPLE
Dotils.New-StringComparer -left 'a' -Right 'A' -StringComparerKind InvariantCultureIgnoreCase, InvariantCulture
.EXAMPLE
# Passthru returns the comparers
$q = Dotils.New-StringComparer -All -PassThru
$q.ForEach({ $_.Compare('a', 'A') }) | Join.Csv
# prints:
-1, 0, -1, 0, 32, 0
$q.ForEach({ $_ | Format-ShortTypeName }) | sort -Unique | Join.csv
# prints:
[CultureAwareComparer], [OrdinalCaseSensitiveComparer], [OrdinalIgnoreCaseComparer]
.EXAMPLE
Dotils.New-StringComparer -left 'a' -Right 'A' -All
CompareName CompareFunc Left Right Result
----------- ----------- ---- ----- ------
InvariantCulture System.CultureAwareComparer a A -1
InvariantCultureIgnoreCase System.CultureAwareComparer a A 0
CurrentCulture System.CultureAwareComparer a A -1
CurrentCultureIgnoreCase System.CultureAwareComparer a A 0
Ordinal System.OrdinalCaseSensitiveComparer a A 32
OrdinalIgnoreCase System.OrdinalIgnoreCaseComparer a A 0
#>
[Alias('Nin.StringComparer')]
param(
[AllowEmptyString()]
[Parameter(Position=0)][string]$LeftInput,

[AllowEmptyString()]
[Parameter(Position=1)][string]$RightInput,


[Alias('CompareAs')]
[Parameter(Position = 2)]
[ArgumentCompletions(
# to rebuild, run: [StringComparer] | fime -MemberType Property | % Name | sort -Unique | join-string -sep ",`n" -SingleQuote
'InvariantCulture', 'InvariantCultureIgnoreCase', 'CurrentCulture', 'CurrentCultureIgnoreCase', 'Ordinal', 'OrdinalIgnoreCase', 'All'
)]
# [StringComparer]
[string[]]
$StringComparerKind = @(
[StringComparer]::CurrentCultureIgnoreCase ),

[switch]$All,

# return comparer instead
[switch]$PassThru
)
[string[]]$allNames =
[StringComparer] | fime -MemberType Property | % Name

if($All -or $StringComparerKind -eq 'All') {
$StringComparerKind = $allNames
}

foreach($curCompareKind in $StringComparerKind) {

$comparer = [StringComparer]::FromComparison( $curCompareKind )
if(-not $Comparer) {
'Error creating [StringComparer]::FromComparison( "{0}" )' -f @( $StringComparerKind ?? '' )
| Write-error
continue
}
if($PassThru) {
$comparer
continue
}
$result = $comparer.Compare( $LeftInput, $RightInput )
$info = $meta = [ordered]@{
PSTypeName = '{0}.Result' -f $MyInvocation.MyCommand.Name
CompareName = $curCompareKind
CompareFunc = $comparer
Left = $LeftInput
Right = $RightInput
Result = $result
}
[pscustomobject]$meta
}
}

41 changes: 41 additions & 0 deletions public_autoloader/New-NinStringComparer.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
BeforeAll {
# walrus sugar to cancel verbose/warning/info streams yet -passthru print module version
( $load = Import-Module Ninmonkey.Console -Force -PassThru ) *>$Null
$load | ft -auto | Out-Host
}

Describe 'New-NinStringComparer' {
It '<Kind> of <Left> and <Right>' -foreach @(
@{
Left = 'a'
Right = 'A'
Kind = 'OrdinalIgnoreCase'
ExpectResult = 0
}
@{
Left = 'a'
Right = 'A'
Kind = 'OrdinalIgnoreCase', 'CurrentCulture'
ExpectResult = 0, -1
}
# New-NinStringComparer -LeftInput 'a' -RightInput 'A' -StringComparerKind OrdinalIgnoreCase | % Result | Should -be 0

) {
New-NinStringComparer -Left $Left -Right $Right -StringComparerKind $Kind
| % Result | Should -be $ExpectResult

}
it 'Manual Implements Test' {
$cmp = New-NinstringComparer -PassThru -StringComparerKind InvariantCulture
$cmp.GetType().ImplementedInterfaces.name -contains 'IEqualityComparer'
| Should -be $True

}
# $out.GetType().ImplementedInterfaces | Join-String -sep ', ' {
# $_.Namespace, $_.Name -join '.' -replace 'System\.', ''
# }
# It 'hardcoded' {
# New-NinStringComparer -LeftInput 'a' -RightInput 'A' -StringComparerKind OrdinalIgnoreCase
# $false | Should -Be $True
# }
}

0 comments on commit a7230eb

Please sign in to comment.