potatoqualitee / kbupdate

🛡 KB Viewer, Saver, Installer and Uninstaller
MIT License
338 stars 50 forks source link

Replaced multiple if statements with regex switch statements #220

Closed HCRitter closed 1 year ago

HCRitter commented 1 year ago

Refactored code to improve architecture determination:

Hey Chrissy, I hope you are fine with my improvements. To test them you can check this code:


$titles = @(
    "Intel IA32 Processor",
    "Intel IA64 Processor",
    "Intel 64-Bit Processor",
    "Intel 32-Bit Processor",
    "AMD64 Processor",
    "Intel x64 Processor",
    "Intel x86 Processor",
    "ARM64 Processor",
    "ARM-based Processor"
)

foreach ($title in $titles) {
    $item = [PSCustomObject]@{
        title = $title
        Architecture = $null
    }

    $item.Architecture = switch -regex ($item.title) {
        ".*ia32.*" { "IA32" }
        ".*ia64.*" { "IA64" }
        ".*(32-Bit|x86).*" { "x86" }
        ".*(x64|AMD64|64-Bit).*" { "x64" }
        ".*ARM64.*" { "ARM64" }
        ".*ARM-based.*" { "ARM32" }
        default { $null } # Handle the case if no matching architecture is found
    }

    Write-Host "Title: $($item.title) | Architecture: $($item.Architecture)"
}

If you like I could check for other improvements as well :)

HCRitter commented 1 year ago

I have seen there are multiple other instances of the if checks for the architecture. in other functions in that file like:

if ($title -match "ia32") {
                        $arch = "IA32"
                    }
                    if ($title -match "ia64") {
                        $arch = "IA64"
                    }
                    if ($title -match "64-Bit" -and $title -notmatch "32-Bit" -and -not $arch) {
                        $arch = "x64"
                    }
                    if ($title -notmatch "64-Bit" -and $title -match "32-Bit" -and -not $arch) {
                        $arch = "x86"
                    }
                    if ($title -match "x64" -or $title -match "AMD64") {
                        $arch = "x64"
                    }
                    if ($title -match "x86") {
                        $arch = "x86"
                    }
                    if ($title -match "ARM64") {
                        $arch = "ARM64"
                    }
                    if ($title -match "ARM-based") {
                        $arch = "ARM32"
                    }
#...

If you like I can change them as well.

potatoqualitee commented 1 year ago

awesome! thank you. i havent had time in forever to work on this project and need to set aside some maintenance. this is a great change but may take some time to get done

potatoqualitee commented 1 year ago

yes, please do change. i prefer pretty and dont know regex 🚀

HCRitter commented 1 year ago

@potatoqualitee now it should be nice and clean :D

HCRitter commented 1 year ago

@potatoqualitee anything else to change? Should be good to go now :)