Skip to content

Commit 8a9df42

Browse files
🌟 [Major]: Introduces the Object module (#2)
## Description This pull request introduces the `Object` PowerShell module, primarily focusing on the addition of a new function and corresponding tests. ### Function Additions * Added a new function `Remove-Member` to remove specified members (properties) from objects passed through the pipeline. This function includes detailed documentation and examples. ### Testing Enhancements * Added tests for the `Remove-Member` function in `tests/Object.Tests.ps1`, including scenarios for removing single and multiple members from an object. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [x] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent a76783e commit 8a9df42

File tree

4 files changed

+101
-36
lines changed

4 files changed

+101
-36
lines changed

src/functions/public/Get-PSModuleTest.ps1

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function Remove-Member {
2+
<#
3+
.SYNOPSIS
4+
Removes specified members (properties) from an object.
5+
6+
.DESCRIPTION
7+
The Remove-Member function removes specified members (properties) from objects passed through the pipeline.
8+
If the specified properties exist, they are removed; otherwise, a verbose message is displayed.
9+
When the -PassThru switch is used, the modified object is returned.
10+
11+
.EXAMPLE
12+
$obj = [PSCustomObject]@{Name="John"; Age=30; City="New York"}
13+
$obj | Remove-Member -Name "Age" -PassThru
14+
15+
Output:
16+
```powershell
17+
Name City
18+
---- ----
19+
John New York
20+
```
21+
22+
Removes the "Age" property from the PSCustomObject and returns the modified object.
23+
24+
.OUTPUTS
25+
PSCustomObject
26+
27+
.NOTES
28+
The modified object without the specified properties, if -PassThru is used.
29+
30+
.LINK
31+
https://psmodule.io/Object/Functions/Remove-Member
32+
#>
33+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
34+
'PSUseShouldProcessForStateChangingFunctions', '',
35+
Justification = 'Sets text in memory'
36+
)]
37+
[CmdletBinding()]
38+
param(
39+
# Accept input from the pipeline.
40+
[Parameter(Mandatory, ValueFromPipeline)]
41+
[System.Object] $InputObject,
42+
43+
# Specify one or more member names to remove.
44+
[Parameter(Mandatory)]
45+
[string[]] $Name,
46+
47+
# When specified, output the modified object.
48+
[Parameter()]
49+
[switch] $PassThru
50+
)
51+
52+
process {
53+
foreach ($obj in $InputObject) {
54+
foreach ($member in $Name) {
55+
if ($obj.PSObject.Properties[$member]) {
56+
$null = $obj.PSObject.Properties.Remove($member)
57+
} else {
58+
Write-Verbose "Property '$member' not found on object of type $($obj.GetType().FullName)."
59+
}
60+
}
61+
if ($PassThru) {
62+
# Only output the object if -PassThru is used.
63+
$obj
64+
}
65+
}
66+
}
67+
}

tests/Object.Tests.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
2+
'PSReviewUnusedParameter', '',
3+
Justification = 'Required for Pester tests'
4+
)]
5+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
6+
'PSUseDeclaredVarsMoreThanAssignments', '',
7+
Justification = 'Required for Pester tests'
8+
)]
9+
[CmdletBinding()]
10+
param()
11+
12+
Describe 'Object' {
13+
Context 'Function: Remove-Member' {
14+
It 'Removes a member from an object' {
15+
$object = [pscustomobject]@{ Name = 'Test'; Value = 'Value' }
16+
$object | Remove-Member -Name Value
17+
$object.PSObject.Properties | Should -Not -Contain 'Value'
18+
}
19+
20+
It 'Removes multiple members from an object' {
21+
$object = [pscustomobject]@{ Name = 'Test'; Value = 'Value'; Other = 'Other' }
22+
$object | Remove-Member -Name Value, Other
23+
$object.PSObject.Properties | Should -Not -Contain 'Value'
24+
$object.PSObject.Properties | Should -Not -Contain 'Other'
25+
}
26+
27+
It 'Removes properties of classes' {
28+
$object = [System.Net.WebClient]::new()
29+
$object | Add-Member -MemberType NoteProperty -Name Something -Value 'http://example.com'
30+
$object | Remove-Member -Name BaseAddress
31+
$object.PSObject.Properties | Should -Not -Contain 'BaseAddress'
32+
}
33+
}
34+
}

tests/PSModuleTest.Tests.ps1

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)