Skip to content

Instantly share code, notes, and snippets.

@chriskuech
Created June 7, 2019 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chriskuech/f260928e780ae835726b237ed762af1a to your computer and use it in GitHub Desktop.
Save chriskuech/f260928e780ae835726b237ed762af1a to your computer and use it in GitHub Desktop.
# 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