Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# modified from https://github.com/chriskuech/functional
function recursiveEquality($a, $b) {
# recursive step for arrays
if ($a -is [array] -and $b -is [array]) {
if ($a.Count -ne $b.Count) {
return $false
}
$inequalIndexes = 0..($a.Count - 1) | ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalIndexes.Count -eq 0
}
# recursive step for hashtables
if ($a -is [hashtable] -and $b -is [hashtable]) {
$inequalKeys = $a.Keys + $b.Keys `
| Sort-Object -Unique `
| ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalKeys.Count -eq 0
}
# recursive step for objects
# use a helper function to circumvent this PowerShell bug:
# https://github.com/PowerShell/PowerShell/issues/9557
if ((isPsCustomObject $a) -and (isPsCustomObject $b)) {
$inequalKeys = $a.psobject.Properties + $b.psobject.Properties `
| % Name `
| Sort-Object -Unique `
| ? { -not (recursiveEquality $a.$_ $b.$_) }
return $inequalKeys.Count -eq 0
}
# base case
return $a.GetType() -eq $b.GetType() -and $a -eq $b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment