stringutil

package
v0.37.30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package stringutil provides utility functions for working with strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractDomainFromURL added in v0.37.0

func ExtractDomainFromURL(urlStr string) string

ExtractDomainFromURL extracts the domain name from a URL string. Handles various URL formats including full URLs with protocols, URLs with ports, and plain domain names.

This function uses net/url.Parse for proper URL parsing when a protocol is present, and falls back to string manipulation for other formats.

Examples:

ExtractDomainFromURL("https://mcp.tavily.com/mcp/")           // returns "mcp.tavily.com"
ExtractDomainFromURL("http://api.example.com:8080/path")      // returns "api.example.com"
ExtractDomainFromURL("mcp.example.com")                       // returns "mcp.example.com"
ExtractDomainFromURL("github.com:443")                        // returns "github.com"
ExtractDomainFromURL("http://sub.domain.com:8080/path")       // returns "sub.domain.com"
ExtractDomainFromURL("localhost:8080")                        // returns "localhost"

func FormatCampaignLabel added in v0.37.2

func FormatCampaignLabel(campaignID string) string

FormatCampaignLabel generates a campaign-specific label from a campaign ID. Campaign-specific labels follow the format "z_campaign_<id>" where <id> is the campaign identifier. The "z_" prefix ensures these labels sort last in label lists for better visibility.

This function sanitizes the campaign ID by replacing invalid label characters (spaces, special chars) with hyphens to ensure GitHub label compatibility.

Examples:

FormatCampaignLabel("security-q1-2025")                     // returns "z_campaign_security-q1-2025"
FormatCampaignLabel("Security Q1 2025")                     // returns "z_campaign_security-q1-2025"
FormatCampaignLabel("dependency_updates")                   // returns "z_campaign_dependency-updates"

func IsAgenticWorkflow added in v0.36.0

func IsAgenticWorkflow(path string) bool

IsAgenticWorkflow returns true if the file path is an agentic workflow file. Agentic workflows end with .md.

Examples:

IsAgenticWorkflow("test.md")                                // returns true
IsAgenticWorkflow("weekly-research.md")                     // returns true
IsAgenticWorkflow(".github/workflows/workflow.md")          // returns true
IsAgenticWorkflow("test.lock.yml")                          // returns false

func IsLockFile added in v0.36.0

func IsLockFile(path string) bool

IsLockFile returns true if the file path is a compiled lock file. Lock files end with .lock.yml and can be compiled from agentic workflows or campaign orchestrators.

Examples:

IsLockFile("test.lock.yml")                                 // returns true
IsLockFile("test.campaign.lock.yml")                        // returns true
IsLockFile(".github/workflows/workflow.lock.yml")           // returns true
IsLockFile("test.md")                                       // returns false
IsLockFile("test.campaign.md")                              // returns false

func IsPositiveInteger added in v0.37.4

func IsPositiveInteger(s string) bool

IsPositiveInteger checks if a string is a positive integer. Returns true for strings like "1", "123", "999" but false for:

  • Zero ("0")
  • Negative numbers ("-5")
  • Numbers with leading zeros ("007")
  • Floating point numbers ("3.14")
  • Non-numeric strings ("abc")
  • Empty strings ("")

func LockFileToMarkdown added in v0.36.0

func LockFileToMarkdown(lockPath string) string

LockFileToMarkdown converts a compiled lock file path back to its markdown source path. This is used when navigating from compiled workflows back to source files.

The function removes the .lock.yml extension and adds .md extension. If the input already has a .md extension, it returns the path unchanged.

Examples:

LockFileToMarkdown("weekly-research.lock.yml")              // returns "weekly-research.md"
LockFileToMarkdown(".github/workflows/test.lock.yml")       // returns ".github/workflows/test.md"
LockFileToMarkdown("workflow.md")                           // returns "workflow.md" (unchanged)
LockFileToMarkdown("my.workflow.lock.yml")                  // returns "my.workflow.md"

func MarkdownToLockFile added in v0.36.0

func MarkdownToLockFile(mdPath string) string

MarkdownToLockFile converts a workflow markdown file path to its compiled lock file path. This is the standard transformation for agentic workflow files.

The function removes the .md extension and adds .lock.yml extension. If the input already has a .lock.yml extension, it returns the path unchanged.

Examples:

MarkdownToLockFile("weekly-research.md")                    // returns "weekly-research.lock.yml"
MarkdownToLockFile(".github/workflows/test.md")             // returns ".github/workflows/test.lock.yml"
MarkdownToLockFile("workflow.lock.yml")                     // returns "workflow.lock.yml" (unchanged)
MarkdownToLockFile("my.workflow.md")                        // returns "my.workflow.lock.yml"

func NormalizePath added in v0.35.1

func NormalizePath(path string) string

NormalizePath normalizes a file path by resolving . and .. components. It splits the path on "/" and processes each component: - Empty parts and "." are skipped - ".." moves up one directory (if possible) - Other parts are added to the result

This is useful for resolving relative paths in bundler operations and other file path manipulations where . and .. components need to be resolved.

Examples:

NormalizePath("a/b/../c")        // returns "a/c"
NormalizePath("./a/./b")         // returns "a/b"
NormalizePath("a/b/../../c")     // returns "c"
NormalizePath("../a/b")          // returns "a/b" (leading .. is ignored)
NormalizePath("a//b")            // returns "a/b" (empty parts removed)

func NormalizeSafeOutputIdentifier added in v0.35.1

func NormalizeSafeOutputIdentifier(identifier string) string

NormalizeSafeOutputIdentifier converts dashes to underscores for safe output identifiers. This standardizes identifier format from the user-facing dash-separated format to the internal underscore-separated format used in safe outputs configuration.

Both dash-separated and underscore-separated formats are valid inputs. This function simply standardizes to the internal representation.

This function performs normalization only - it assumes the input is already a valid identifier and does NOT perform character validation or sanitization.

Examples:

NormalizeSafeOutputIdentifier("create-issue")      // returns "create_issue"
NormalizeSafeOutputIdentifier("create_issue")      // returns "create_issue" (unchanged)
NormalizeSafeOutputIdentifier("add-comment")       // returns "add_comment"
NormalizeSafeOutputIdentifier("update-pr")         // returns "update_pr"

func NormalizeWhitespace

func NormalizeWhitespace(content string) string

NormalizeWhitespace normalizes trailing whitespace and newlines to reduce spurious conflicts. It trims trailing whitespace from each line and ensures exactly one trailing newline.

func NormalizeWorkflowName added in v0.35.1

func NormalizeWorkflowName(name string) string

NormalizeWorkflowName removes .md and .lock.yml extensions from workflow names. This is used to standardize workflow identifiers regardless of the file format.

The function checks for extensions in order of specificity: 1. Removes .lock.yml extension (the compiled workflow format) 2. Removes .md extension (the markdown source format) 3. Returns the name unchanged if no recognized extension is found

This function performs normalization only - it assumes the input is already a valid identifier and does NOT perform character validation or sanitization.

Examples:

NormalizeWorkflowName("weekly-research")           // returns "weekly-research"
NormalizeWorkflowName("weekly-research.md")        // returns "weekly-research"
NormalizeWorkflowName("weekly-research.lock.yml")  // returns "weekly-research"
NormalizeWorkflowName("my.workflow.md")            // returns "my.workflow"

func ParseVersionValue

func ParseVersionValue(version any) string

ParseVersionValue converts version values of various types to strings. Supports string, int, int64, uint64, and float64 types. Returns empty string for unsupported types.

func SanitizeErrorMessage

func SanitizeErrorMessage(message string) string

SanitizeErrorMessage removes potential secret key names from error messages to prevent information disclosure via logs. This prevents exposing details about an organization's security infrastructure by redacting secret key names that might appear in error messages.

func SanitizeParameterName added in v0.35.1

func SanitizeParameterName(name string) string

SanitizeParameterName converts a parameter name to a safe JavaScript identifier by replacing non-alphanumeric characters with underscores.

This function ensures that parameter names from workflows can be used safely in JavaScript code by: 1. Replacing any non-alphanumeric characters (except $ and _) with underscores 2. Prepending an underscore if the name starts with a number

Valid characters: a-z, A-Z, 0-9 (not at start), _, $

Examples:

SanitizeParameterName("my-param")        // returns "my_param"
SanitizeParameterName("my.param")        // returns "my_param"
SanitizeParameterName("123param")        // returns "_123param"
SanitizeParameterName("valid_name")      // returns "valid_name"
SanitizeParameterName("$special")        // returns "$special"

func SanitizePythonVariableName added in v0.35.1

func SanitizePythonVariableName(name string) string

SanitizePythonVariableName converts a parameter name to a valid Python identifier by replacing non-alphanumeric characters with underscores.

This function ensures that parameter names from workflows can be used safely in Python code by: 1. Replacing any non-alphanumeric characters (except _) with underscores 2. Prepending an underscore if the name starts with a number

Valid characters: a-z, A-Z, 0-9 (not at start), _ Note: Python does not allow $ in identifiers (unlike JavaScript)

Examples:

SanitizePythonVariableName("my-param")        // returns "my_param"
SanitizePythonVariableName("my.param")        // returns "my_param"
SanitizePythonVariableName("123param")        // returns "_123param"
SanitizePythonVariableName("valid_name")      // returns "valid_name"

func SanitizeToolID added in v0.35.1

func SanitizeToolID(toolID string) string

SanitizeToolID removes common MCP prefixes and suffixes from tool IDs. This cleans up tool identifiers by removing redundant MCP-related naming patterns.

The function: 1. Removes "mcp-" prefix 2. Removes "-mcp" suffix 3. Returns the original ID if the result would be empty

Examples:

SanitizeToolID("notion-mcp")        // returns "notion"
SanitizeToolID("mcp-notion")        // returns "notion"
SanitizeToolID("some-mcp-server")   // returns "some-server"
SanitizeToolID("github")            // returns "github" (unchanged)
SanitizeToolID("mcp")               // returns "mcp" (prevents empty result)

func StripANSIEscapeCodes added in v0.37.3

func StripANSIEscapeCodes(s string) string

StripANSIEscapeCodes removes ANSI escape sequences from a string. This prevents terminal color codes and other control sequences from being accidentally included in generated files (e.g., YAML workflows).

Common ANSI escape sequences that are removed:

  • Color codes: \x1b[31m (red), \x1b[0m (reset)
  • Text formatting: \x1b[1m (bold), \x1b[4m (underline)
  • Cursor control: \x1b[2J (clear screen)

Example:

input := "Hello \x1b[31mWorld\x1b[0m"  // "Hello [red]World[reset]"
output := StripANSIEscapeCodes(input)  // "Hello World"

This function is particularly important for:

  • Workflow descriptions copied from terminal output
  • Comments in generated YAML files
  • Any text that should be plain ASCII

func Truncate

func Truncate(s string, maxLen int) string

Truncate truncates a string to a maximum length, adding "..." if truncated. If maxLen is 3 or less, the string is truncated without "...".

This is a general-purpose utility for truncating any string to a configurable length. For domain-specific workflow command identifiers with newline handling, see workflow.ShortenCommand instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL