From 8f7d0ad18b161075115625f20f497e88db44be44 Mon Sep 17 00:00:00 2001 From: Rodrigo Cordeiro Date: Fri, 5 Sep 2025 12:57:43 -0300 Subject: [PATCH] feat: autofullfill alias with simple slug generator --- Private/Get-Slug.ps1 | 55 ++++++++++++++++++++++++++++++++++ Public/Functions/New-Task.ps1 | 4 ++- Public/Functions/Set-Task.ps1 | 6 ++-- pshabitica.psd1 | Bin 9838 -> 9838 bytes 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 Private/Get-Slug.ps1 diff --git a/Private/Get-Slug.ps1 b/Private/Get-Slug.ps1 new file mode 100644 index 0000000..22ae742 --- /dev/null +++ b/Private/Get-Slug.ps1 @@ -0,0 +1,55 @@ +function Get-Slug { + <# + .SYNOPSIS + Generates a simple, readable slug (alias) from input text. + + .DESCRIPTION + Converts a string into a URL/alias-safe slug: + - Lowercases everything + - Replaces spaces/underscores with dashes + - Removes invalid characters + - Collapses multiple dashes + - Ensures max length (default: 16 chars) cutting at the nearest dash if possible + + .PARAMETER InputString + The text you want to convert into a slug. + + .PARAMETER MaxLen + Maximum allowed length of the slug (default: 16). + + .EXAMPLE + New-Slug "Hello World! This is an Alias" + # hello-world + + .EXAMPLE + New-Slug "Daily Check In" -MaxLen 12 + # daily-check + #> + param( + [Parameter(Mandatory = $true)] + [string]$InputString, + + [int]$MaxLen = 16 + ) + + # Normalize + $slug = $InputString.ToLower().Trim() + $slug = $slug -replace '[\s_]+', '-' # spaces/underscores → dash + $slug = $slug -replace '[^a-z0-9-]', '' # keep only alphanumeric/dash + $slug = $slug -replace '-{2,}', '-' # collapse multiple dashes + $slug = $slug.Trim('-') # remove leading/trailing dashes + + if ($slug.Length -gt $MaxLen) { + # Try to cut at the last dash before max length + $cut = $slug.Substring(0, $MaxLen) + $lastDash = $cut.LastIndexOf('-') + if ($lastDash -gt 0) { + $slug = $cut.Substring(0, $lastDash) + } + else { + $slug = $cut.TrimEnd('-') + } + } + + return $slug +} diff --git a/Public/Functions/New-Task.ps1 b/Public/Functions/New-Task.ps1 index 8e58afd..6aa9fff 100644 --- a/Public/Functions/New-Task.ps1 +++ b/Public/Functions/New-Task.ps1 @@ -175,7 +175,9 @@ function New-Task { "attribute" = $Attribute } - if ($Alias) { $body.alias = $Alias } + if ($Alias) { $body.alias = $Alias } else { + $body.alias = (Get-Slug $Text) + } if ($Note) { $body.notes = $Note } if ($Tags) { $tagIds = @() diff --git a/Public/Functions/Set-Task.ps1 b/Public/Functions/Set-Task.ps1 index fa90ae0..9ef60e4 100644 --- a/Public/Functions/Set-Task.ps1 +++ b/Public/Functions/Set-Task.ps1 @@ -1,10 +1,10 @@ -function Set-Task { +function Set-Task { <# .SYNOPSIS Updates an existing Habitica task. .DESCRIPTION - Calls the Habitica API to update a task by ID. + Calls the Habitica API to update a task by ID. Parameters are grouped into sets depending on the task type (`todo`, `daily`, or `habit`). .PARAMETER TaskId @@ -100,7 +100,7 @@ function Set-Task { ) begin { - + } process { diff --git a/pshabitica.psd1 b/pshabitica.psd1 index d1c91d3e984310775aff1a0dc6477d8d9a87c44e..91db435fdeec02f941187e504de944785a072212 100644 GIT binary patch delta 18 acmaFo^Uh~O8xy19 delta 18 acmaFo^Uh~O8xy0!