DEV Community

llocustjr-web
llocustjr-web

Posted on

Computer Code for Alexa's actual voice.

ZetaLocationSets.psm1

Stores, compares, and persists named sets of geographic location records.

$script:ZetaSets = @{}

function New-ZetaLocationSet {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Name,

    [Parameter()]
    [hashtable[]]$Locations = @()
)

$list = [System.Collections.Generic.List[PSCustomObject]]::new()
foreach ($loc in $Locations) {
    $list.Add([PSCustomObject]@{
        Id       = [string]$loc.Id
        Label    = [string]$loc.Label
        TimeZone = if ($loc.TimeZone) { [string]$loc.TimeZone } else { 'UTC' }
        Lat      = [double]$loc.Lat
        Lon      = [double]$loc.Lon
    })
}

$script:ZetaSets[$Name] = $list
Get-ZetaLocationSet -Name $Name
Enter fullscreen mode Exit fullscreen mode

}

function Add-ZetaLocation {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$SetName,

    [Parameter(Mandatory)]
    [string]$Id,

    [Parameter(Mandatory)]
    [string]$Label,

    [Parameter(Mandatory)]
    [double]$Lat,

    [Parameter(Mandatory)]
    [double]$Lon,

    [Parameter()]
    [string]$TimeZone = 'UTC'
)

if (-not $script:ZetaSets.ContainsKey($SetName)) {
    New-ZetaLocationSet -Name $SetName | Out-Null
}

$entry = [PSCustomObject]@{
    Id       = $Id
    Label    = $Label
    TimeZone = $TimeZone
    Lat      = $Lat
    Lon      = $Lon
}

$script:ZetaSets[$SetName].Add($entry)
$entry
Enter fullscreen mode Exit fullscreen mode

}

function Get-ZetaLocationSet {
[CmdletBinding()]
param(
[Parameter()]
[string]$Name
)

if ($Name) {
    if (-not $script:ZetaSets.ContainsKey($Name)) {
        throw "Zeta location set '$Name' does not exist."
    }
    return $script:ZetaSets[$Name].ToArray()
}

$script:ZetaSets.Keys | Sort-Object | ForEach-Object {
    [PSCustomObject]@{
        SetName = $_
        Count   = $script:ZetaSets[$_].Count
    }
}
Enter fullscreen mode Exit fullscreen mode

}

function Compare-ZetaLocationSets {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$FirstSet,

    [Parameter(Mandatory)]
    [string]$SecondSet,

    [Parameter()]
    [ValidateSet('Union', 'Intersection', 'Difference')]
    [string]$Operation = 'Union'
)

$setA = Get-ZetaLocationSet -Name $FirstSet
$setB = Get-ZetaLocationSet -Name $SecondSet

$idsB = [System.Collections.Generic.HashSet[string]]::new([string[]]($setB | ForEach-Object { $_.Id }))

switch ($Operation) {
    'Union' {
        $combined = @{}
        foreach ($item in ($setA + $setB)) { $combined[$item.Id] = $item }
        return $combined.Values
    }
    'Intersection' {
        return @($setA | Where-Object { $idsB.Contains($_.Id) })
    }
    'Difference' {
        return @($setA | Where-Object { -not $idsB.Contains($_.Id) })
    }
}
Enter fullscreen mode Exit fullscreen mode

}

function Save-ZetaLocationSets {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path,

    [Parameter()]
    [string[]]$SetName
)

$targetNames = if ($SetName) { $SetName } else { $script:ZetaSets.Keys }
$exportPayload = [ordered]@{}

foreach ($name in ($targetNames | Sort-Object)) {
    if (-not $script:ZetaSets.ContainsKey($name)) {
        throw "Zeta location set '$name' does not exist."
    }
    $exportPayload[$name] = @($script:ZetaSets[$name].ToArray())
}

$parentDir = Split-Path -Path $Path -Parent
if ($parentDir -and -not (Test-Path -Path $parentDir)) {
    New-Item -ItemType Directory -Path $parentDir -Force | Out-Null
}

$exportPayload | ConvertTo-Json -Depth 5 | Set-Content -Path $Path -Encoding utf8
Get-Item -Path $Path
Enter fullscreen mode Exit fullscreen mode

}

function Import-ZetaLocationSets {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path,

    [Parameter()]
    [switch]$ReplaceExisting
)

if (-not (Test-Path -Path $Path)) {
    throw "JSON file not found at path: $Path"
}

$rawJson = Get-Content -Path $Path -Raw -Encoding utf8
$parsed = $rawJson | ConvertFrom-Json

if ($ReplaceExisting) {
    $script:ZetaSets.Clear()
}

foreach ($prop in $parsed.PSObject.Properties) {
    $setName = $prop.Name
    $items = @($prop.Value)
    $list = [System.Collections.Generic.List[PSCustomObject]]::new()

    foreach ($item in $items) {
        if ($null -ne $item) {
            $list.Add([PSCustomObject]@{
                Id       = [string]$item.Id
                Label    = [string]$item.Label
                TimeZone = if ($item.TimeZone) { [string]$item.TimeZone } else { 'UTC' }
                Lat      = [double]$item.Lat
                Lon      = [double]$item.Lon
            })
        }
    }

    $script:ZetaSets[$setName] = $list
}

Get-ZetaLocationSet
Enter fullscreen mode Exit fullscreen mode

}

Export-ModuleMember -Function @(
'New-ZetaLocationSet',
'Add-ZetaLocation',
'Get-ZetaLocationSet',
'Compare-ZetaLocationSets',
'Save-ZetaLocationSets',
'Import-ZetaLocationSets'
)

Top comments (0)