forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetUnique.cs
More file actions
120 lines (105 loc) · 3.65 KB
/
Copy pathGetUnique.cs
File metadata and controls
120 lines (105 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Internal;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// </summary>
[Cmdlet(VerbsCommon.Get, "Unique", DefaultParameterSetName = "AsString",
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097028", RemotingCapability = RemotingCapability.None)]
public sealed class GetUniqueCommand : PSCmdlet
{
#region Parameters
/// <summary>
/// </summary>
/// <value></value>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { get; set; } = AutomationNull.Value;
/// <summary>
/// This parameter specifies that objects should be converted to
/// strings and the strings should be compared.
/// </summary>
/// <value></value>
[Parameter(ParameterSetName = "AsString")]
public SwitchParameter AsString
{
get { return _asString; }
set { _asString = value; }
}
private bool _asString;
/// <summary>
/// This parameter specifies that just the types of the objects
/// should be compared.
/// </summary>
/// <value></value>
[Parameter(ParameterSetName = "UniqueByType")]
public SwitchParameter OnType
{
get { return _onType; }
set { _onType = value; }
}
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
/// <summary>
/// </summary>
protected override void ProcessRecord()
{
bool isUnique = true;
if (_lastObject == null)
{
// always write first object, but return nothing
// on "MSH> get-unique"
if (AutomationNull.Value == InputObject)
return;
}
else if (OnType)
{
isUnique = (InputObject.InternalTypeNames[0] != _lastObject.InternalTypeNames[0]);
}
else if (AsString)
{
string inputString = InputObject.ToString();
_lastObjectAsString ??= _lastObject.ToString();
if (string.Equals(
inputString,
_lastObjectAsString,
CaseInsensitive.IsPresent ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture))
{
isUnique = false;
}
else
{
_lastObjectAsString = inputString;
}
}
else // compare as objects
{
_comparer ??= new ObjectCommandComparer(
ascending: true,
CultureInfo.CurrentCulture,
caseSensitive: !CaseInsensitive.IsPresent);
isUnique = (_comparer.Compare(InputObject, _lastObject) != 0);
}
if (isUnique)
{
WriteObject(InputObject);
_lastObject = InputObject;
}
}
#endregion Overrides
#region Internal
private PSObject _lastObject = null;
private string _lastObjectAsString = null;
private ObjectCommandComparer _comparer = null;
#endregion Internal
}
}