Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added -CaseInsensitive switch parameter to Select-Object & Get-Unique cmdlets #19683

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
ArmaanMcleod marked this conversation as resolved.
Show resolved Hide resolved
{
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