Skip to content

Commit

Permalink
Added -CaseInsensitive switch parameter to Select-Object & `Get-U…
Browse files Browse the repository at this point in the history
…nique` cmdlets (PowerShell#19683)

* Added Select-Object CaseInsensitive parameter and tests

* Added Get-Unique CaseInsensitive parameter and tests

* Fix codefactor issue

* Add named arguements

* Remove comments
  • Loading branch information
ArmaanMcleod authored and VindSkyggen committed Dec 26, 2023
1 parent 94dba7b commit 0ba3a84
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public SwitchParameter OnType
}

private bool _onType = false;

/// <summary>
/// Gets or sets case insensitive switch for string comparison.
/// </summary>
[Parameter]
public SwitchParameter CaseInsensitive { get; set; }

#endregion Parameters

#region Overrides
Expand Down Expand Up @@ -77,7 +84,7 @@ protected override void ProcessRecord()
if (string.Equals(
inputString,
_lastObjectAsString,
StringComparison.CurrentCulture))
CaseInsensitive.IsPresent ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
{
isUnique = false;
}
Expand All @@ -89,9 +96,9 @@ protected override void ProcessRecord()
else // compare as objects
{
_comparer ??= new ObjectCommandComparer(
true, // ascending (doesn't matter)
ascending: true,
CultureInfo.CurrentCulture,
true); // case-sensitive
caseSensitive: !CaseInsensitive.IsPresent);

isUnique = (_comparer.Compare(InputObject, _lastObject) != 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public SwitchParameter Unique

private bool _unique;

/// <summary>
/// Gets or sets case insensitive switch for string comparison.
/// Used in combination with Unique switch parameter.
/// </summary>
[Parameter]
public SwitchParameter CaseInsensitive { get; set; }

/// <summary>
/// </summary>
/// <value></value>
Expand Down Expand Up @@ -632,7 +639,11 @@ private void FilteredWriteObject(PSObject obj, List<PSNoteProperty> addedNotePro
bool isObjUnique = true;
foreach (UniquePSObjectHelper uniqueObj in _uniques)
{
ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, true);
ObjectCommandComparer comparer = new(
ascending: true,
CultureInfo.CurrentCulture,
caseSensitive: !CaseInsensitive.IsPresent);

if ((comparer.Compare(obj.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) &&
(uniqueObj.NotePropertyCount == addedNoteProperties.Count))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Get-Unique DRT Unit Tests" -Tags "CI" {
It "Command get-unique works with AsString switch" {

BeforeAll {
$inputArray = "aa","aa","Aa","ba","BA","BA"
}

It "Command get-unique works with AsString switch" {
$results = $inputArray | Get-Unique -AsString

$results.Length | Should -Be 4
Expand All @@ -17,6 +21,18 @@ Describe "Get-Unique DRT Unit Tests" -Tags "CI" {
$results[2] | Should -BeOfType System.String
$results[3] | Should -BeOfType System.String
}

It "Command get-unique works with AsString and CaseInsensitive switches" {
$results = $inputArray | Get-Unique -AsString -CaseInsensitive

$results.Length | Should -Be 2

$results[0] | Should -BeExactly "aa"
$results[1] | Should -BeExactly "ba"

$results[0] | Should -BeOfType System.String
$results[1] | Should -BeOfType System.String
}
}

Describe "Get-Unique" -Tags "CI" {
Expand All @@ -26,6 +42,8 @@ Describe "Get-Unique" -Tags "CI" {
$expectedOutput1 = 1,2,3,4,5
$collection = "a", "b", "b", "d"
$expectedOutput2 = "a", "b", "d"
$collection2 = "a","A", "b", "B"
$expectedOutput3 = "a", "b"
}

It "Should be able to use the Get-Unique cmdlet without error with inputObject switch" {
Expand Down Expand Up @@ -61,4 +79,9 @@ Describe "Get-Unique" -Tags "CI" {
$actual = $collection | Get-Unique
$(Compare-Object $actual $expectedOutput2 -SyncWindow 0).Length | Should -Be 0
}

It "Should get the unique strings when CaseInsensitive switch is used" {
$actual = $collection2 | Get-Unique -CaseInsensitive
$(Compare-Object $actual $expectedOutput3 -SyncWindow 0).Length | Should -Be 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ Describe "Select-Object" -Tags "CI" {
$result | Should -Be $expected
}

It "Should work work correctly with Unique and CaseInsensitive parameter" {
$result = "abc", "Abc" | Select-Object -Unique -CaseInsensitive

$result.Count | Should -Be 1
$result | Should -Be "abc"
}

It "Should return correct object with Skip parameter" {
$result = $dirObject | Select-Object -Skip $TestLength

Expand Down

0 comments on commit 0ba3a84

Please sign in to comment.