Skip to content

Commit 3055d79

Browse files
🚀 [Feature]: Add a Compare-PSCustomObject function (#3)
## Description This pull request introduces a major overhaul to the `PSCustomObject` module, focusing on adding new functionality for comparing PowerShell custom objects, removing outdated features, and updating documentation and examples accordingly. The most important changes are grouped below by theme. ### New Functionality * Added a new function `Compare-PSCustomObject` to compare two PowerShell custom objects and return a detailed overview of their differences. This includes an option to filter only the changed properties. * Introduced a new test suite in `tests/PSCustomObject.Tests.ps1` to validate the functionality of `Compare-PSCustomObject`. ### Documentation Updates * Updated `README.md` to reflect the new module focus on `PSCustomObject`, replacing placeholder content with meaningful descriptions, installation instructions, and an example usage of `Compare-PSCustomObject`. [[1]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L1-R3) [[2]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L15-L37) * Removed outdated sections in `README.md` such as "Acknowledgements" and placeholder documentation links. [[1]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L47-L51) [[2]](diffhunk://#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5L66-L69) ### Example Updates * Replaced the old `General.ps1` example with a new example demonstrating the usage of `Compare-PSCustomObject`. ### Removed Legacy Features * Removed the `Get-PSModuleTest` function, which was no longer relevant to the updated module purpose. * Deleted the corresponding test suite for `Get-PSModuleTest` in `tests/PSModuleTest.Tests.ps1`. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [x] 🚀 [Feature] - [ ] 🌟 [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 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7f02aee commit 3055d79

6 files changed

Lines changed: 191 additions & 72 deletions

File tree

README.md

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# {{ NAME }}
1+
# PSCustomObject
22

3-
{{ DESCRIPTION }}
3+
This module provides a set of functions for working with PowerShell custom objects (PSCustomObject).
44

55
## Prerequisites
66

@@ -12,29 +12,61 @@ This uses the following external resources:
1212
To install the module from the PowerShell Gallery, you can use the following command:
1313

1414
```powershell
15-
Install-PSResource -Name {{ NAME }}
16-
Import-Module -Name {{ NAME }}
15+
Install-PSResource -Name PSCustomObject
16+
Import-Module -Name PSCustomObject
1717
```
1818

1919
## Usage
2020

2121
Here is a list of example that are typical use cases for the module.
2222

23-
### Example 1: Greet an entity
23+
### Example 1: Compare two objects
2424

25-
Provide examples for typical commands that a user would like to do with the module.
25+
This example shows how to compare two objects and get the differences between them.
2626

2727
```powershell
28-
Greet-Entity -Name 'World'
29-
Hello, World!
28+
$object1 = [PSCustomObject]@{
29+
Name = 'Test'
30+
Value = 1
31+
}
32+
33+
$object2 = [PSCustomObject]@{
34+
Name = 'Test'
35+
Value = 2
36+
}
37+
38+
$object1 | Compare-PSCustomObject $object2
39+
40+
# Output:
41+
# Property Left Right Changed
42+
# -------- ---- ----- -------
43+
# Name Test Test False
44+
# Value 1 2 True
45+
3046
```
3147

32-
### Example 2
48+
### Example 2: Compare two objects showing only changed properties
3349

34-
Provide examples for typical commands that a user would like to do with the module.
50+
This example shows how to compare two objects and get the differences between them.
3551

3652
```powershell
37-
Import-Module -Name PSModuleTemplate
53+
$object1 = [PSCustomObject]@{
54+
Name = 'Test'
55+
Value = 1
56+
}
57+
58+
$object2 = [PSCustomObject]@{
59+
Name = 'Test'
60+
Value = 2
61+
}
62+
63+
$object1 | Compare-PSCustomObject $object2 -OnlyChanged
64+
65+
# Output:
66+
# Property Left Right Changed
67+
# -------- ---- ----- -------
68+
# Value 1 2 True
69+
3870
```
3971

4072
### Find more examples
@@ -44,11 +76,6 @@ To find more examples of how to use the module, please refer to the [examples](e
4476
Alternatively, you can use the Get-Command -Module 'This module' to find more commands that are available in the module.
4577
To find examples of each of the commands you can use Get-Help -Examples 'CommandName'.
4678

47-
## Documentation
48-
49-
Link to further documentation if available, or describe where in the repository users can find more detailed documentation about
50-
the module's functions and features.
51-
5279
## Contributing
5380

5481
Coder or not, you can contribute to the project! We welcome all contributions.
@@ -63,7 +90,3 @@ Please see the issues tab on this project and submit a new issue that matches yo
6390

6491
If you do code, we'd love to have your contributions. Please read the [Contribution guidelines](CONTRIBUTING.md) for more information.
6592
You can either help by picking up an existing issue or submit a new one if you have an idea for a new feature or improvement.
66-
67-
## Acknowledgements
68-
69-
Here is a list of people and projects that helped this project in some way.

examples/General.ps1

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
<#
2-
.SYNOPSIS
3-
This is a general example of how to use the module.
4-
#>
1+
$object1 = [PSCustomObject]@{
2+
Name = 'Test'
3+
Value = 1
4+
}
55

6-
# Import the module
7-
Import-Module -Name 'PSModule'
6+
$object2 = [PSCustomObject]@{
7+
Name = 'Test'
8+
Value = 2
9+
}
810

9-
# Define the path to the font file
10-
$FontFilePath = 'C:\Fonts\CodeNewRoman\CodeNewRomanNerdFontPropo-Regular.tff'
11+
$object1 | Compare-PSCustomObject $object2
1112

12-
# Install the font
13-
Install-Font -Path $FontFilePath -Verbose
14-
15-
# List installed fonts
16-
Get-Font -Name 'CodeNewRomanNerdFontPropo-Regular'
17-
18-
# Uninstall the font
19-
Get-Font -Name 'CodeNewRomanNerdFontPropo-Regular' | Uninstall-Font -Verbose
13+
# Output:
14+
# Property Left Right Changed
15+
# -------- ---- ----- -------
16+
# Name Test Test False
17+
# Value 1 2 True
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
filter Compare-PSCustomObject {
2+
<#
3+
.SYNOPSIS
4+
Compare two PSCustomObjects and return the comparison.
5+
6+
.DESCRIPTION
7+
This function compares two PSCustomObjects and returns an overview of the differences.
8+
It can also filter to show only the properties that have changed.
9+
10+
.EXAMPLE
11+
Compare-PSCustomObject -Left $object1 -Right $object2 -OnlyChanged
12+
13+
.EXAMPLE
14+
$object1 = [PSCustomObject]@{
15+
Name = 'Test'
16+
Value = 1
17+
}
18+
$object2 = [PSCustomObject]@{
19+
Name = 'Test'
20+
Value = 2
21+
}
22+
$object1 | Compare-PSCustomObject $object2
23+
24+
Output:
25+
```powershell
26+
Property Left Right Changed
27+
-------- ---- ----- -------
28+
Name Test Test False
29+
Value 1 2 True
30+
```
31+
32+
.LINK
33+
https://psmodule.io/PSCustomObject/Functions/Compare-PSCustomObject
34+
#>
35+
[Alias('Compare-PSObject')]
36+
[CmdletBinding()]
37+
param (
38+
# The left object to compare
39+
[Parameter(Mandatory, ValueFromPipeline)]
40+
[object] $Left,
41+
42+
# The right object to compare
43+
[Parameter(Mandatory, Position = 0)]
44+
[object] $Right,
45+
46+
# Only show properties that have changed
47+
[Parameter()]
48+
[switch] $OnlyChanged
49+
)
50+
51+
$diff = foreach ($propertyName in $Left.psobject.Properties.Name) {
52+
$leftValue = $Left.$propertyName
53+
$rightValue = $Right.$propertyName
54+
[PSCustomObject]@{
55+
Property = $propertyName
56+
Left = "$leftValue"
57+
Right = "$rightValue"
58+
Changed = "$leftValue" -ne "$rightValue"
59+
}
60+
}
61+
62+
if ($OnlyChanged) {
63+
return $diff | Where-Object { $_.Changed }
64+
}
65+
$diff
66+
}

src/functions/public/Get-PSModuleTest.ps1

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

tests/PSCustomObject.Tests.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#Requires -Modules @{ ModuleName = 'Pester'; RequiredVersion = '5.7.1' }
2+
3+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
4+
'PSUseDeclaredVarsMoreThanAssignments', '',
5+
Justification = 'Pester grouping syntax: known issue.'
6+
)]
7+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
8+
'PSAvoidUsingConvertToSecureStringWithPlainText', '',
9+
Justification = 'Used to create a secure string for testing.'
10+
)]
11+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
12+
'PSAvoidUsingWriteHost', '',
13+
Justification = 'Log outputs to GitHub Actions logs.'
14+
)]
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
16+
'PSAvoidLongLines', '',
17+
Justification = 'Long test descriptions and skip switches'
18+
)]
19+
[CmdletBinding()]
20+
param()
21+
22+
Describe 'PSCustomObject' {
23+
It 'Function: Compare-PSCustomObject' {
24+
$Left = [PSCustomObject]@{
25+
Name = 'Test'
26+
Value = 1
27+
}
28+
29+
$Right = [PSCustomObject]@{
30+
Name = 'Test'
31+
Value = 2
32+
}
33+
34+
LogGroup 'Diff' {
35+
$diff = $Left | Compare-PSCustomObject $Right
36+
Write-Host "$($diff | Format-Table | Out-String)"
37+
}
38+
39+
$diff | Should -HaveCount 2
40+
$diff.Property | Should -BeIn @('Name', 'Value')
41+
$diff.Left | Should -BeIn @('Test', 1)
42+
$diff.Right | Should -BeIn @('Test', 2)
43+
$diff.Changed | Should -Contain $true
44+
}
45+
46+
It 'Function: Compare-PSCustomObject -OnlyChanged' {
47+
$Left = [PSCustomObject]@{
48+
Name = 'Test'
49+
Value = 1
50+
}
51+
52+
$Right = [PSCustomObject]@{
53+
Name = 'Test'
54+
Value = 2
55+
}
56+
57+
LogGroup 'Diff' {
58+
$diff = $Left | Compare-PSCustomObject $Right -OnlyChanged
59+
Write-Host "$($diff | Format-Table | Out-String)"
60+
}
61+
62+
$diff | Should -HaveCount 1
63+
$diff.Property | Should -Be 'Value'
64+
$diff.Left | Should -Be 1
65+
$diff.Right | Should -Be 2
66+
$diff.Changed | Should -Be $true
67+
}
68+
}

tests/PSModuleTest.Tests.ps1

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

0 commit comments

Comments
 (0)