rostrings

package module
v0.0.0-...-a6ee939 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

Strings Plugin

The string plugin provides operators for manipulating strings in reactive streams.

Installation

go get github.com/samber/ro/plugins/strings

Operators

CamelCase

Converts strings to camelCase format.

import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
)

observable := ro.Pipe1(
    ro.Just(
        "hello world",
        "user_name",
        "API_KEY",
    ),
    rostrings.CamelCase[string](),
)

// Output:
// Next: helloWorld
// Next: userName
// Next: apiKey
// Completed
Capitalize

Capitalizes the first letter of each string.

observable := ro.Pipe1(
    ro.Just(
        "hello",
        "world",
        "golang",
    ),
    rostrings.Capitalize[string](),
)

// Output:
// Next: Hello
// Next: World
// Next: Golang
// Completed
KebabCase

Converts strings to kebab-case format.

observable := ro.Pipe1(
    ro.Just(
        "hello world",
        "userName",
        "API_KEY",
    ),
    rostrings.KebabCase[string](),
)

// Output:
// Next: hello-world
// Next: user-name
// Next: api-key
// Completed
PascalCase

Converts strings to PascalCase format.

observable := ro.Pipe1(
    ro.Just(
        "hello world",
        "user_name",
        "api_key",
    ),
    rostrings.PascalCase[string](),
)

// Output:
// Next: HelloWorld
// Next: UserName
// Next: ApiKey
// Completed
SnakeCase

Converts strings to snake_case format.

observable := ro.Pipe1(
    ro.Just(
        "hello world",
        "userName",
        "API_KEY",
    ),
    rostrings.SnakeCase[string](),
)

// Output:
// Next: hello_world
// Next: user_name
// Next: api_key
// Completed
Ellipsis

Truncates strings with ellipsis to a specified length.

observable := ro.Pipe1(
    ro.Just(
        "This is a very long string that needs to be truncated",
        "Short",
        "Another long string for demonstration",
    ),
    rostrings.Ellipsis[string](20),
)

// Output:
// Next: This is a very lon...
// Next: Short
// Next: Another long string...
// Completed
Words

Splits strings into words.

observable := ro.Pipe1(
    ro.Just(
        "hello world",
        "user_name",
        "camelCase",
        "PascalCase",
    ),
    rostrings.Words[string](),
)

// Output:
// Next: [hello world]
// Next: [user name]
// Next: [camel case]
// Next: [pascal case]
// Completed
Random

Generates random strings of specified size using a charset.

observable := ro.Pipe1(
    ro.Just(
        "prefix",
        "suffix",
        "base",
    ),
    rostrings.Random[string](10, rostrings.AlphanumericCharset),
)

// Output: (random strings will vary)
// Next: prefixa1b2c3d4e5
// Next: suffixf6g7h8i9j0
// Next: basek1l2m3n4o5p
// Completed

Available Charsets

The Random operator provides several predefined charsets:

  • LowerCaseLettersCharset: a-z
  • UpperCaseLettersCharset: A-Z
  • LettersCharset: a-z + A-Z
  • NumbersCharset: 0-9
  • AlphanumericCharset: a-z + A-Z + 0-9
  • SpecialCharset: !@#$%^&*()_+-=[]{}|;':",./<>?
  • AllCharset: All characters

Real-world Example

Here's a practical example that processes user input and normalizes it:

import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
)

// Process user input and normalize to different formats
pipeline := ro.Pipe4(
    // Simulate user input
    ro.Just(
        "user first name",
        "API_ENDPOINT_URL",
        "databaseTableName",
    ),
    // Convert to snake_case for database
    rostrings.SnakeCase[string](),
    // Also generate camelCase version
    rostrings.CamelCase[string](),
    // And PascalCase for display
    rostrings.PascalCase[string](),
)

subscription := pipeline.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: user_first_name
// Next: api_endpoint_url
// Next: database_table_name
// Completed

Comparison with Byte Plugin

The string plugin provides similar functionality to the byte plugin but works with string types instead of []byte. Choose the appropriate plugin based on your data type:

  • Use string plugin when working with string types
  • Use byte plugin when working with []byte types

Performance Considerations

  • String operations are generally faster than byte operations for text processing
  • The string plugin uses Go's standard strings package for operations
  • Consider using the byte plugin for binary data or when you need byte-level control

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	LowerCaseLettersCharset = []rune("abcdefghijklmnopqrstuvwxyz")
	UpperCaseLettersCharset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	LettersCharset          = append(LowerCaseLettersCharset, UpperCaseLettersCharset...)
	NumbersCharset          = []rune("0123456789")
	AlphanumericCharset     = append(LettersCharset, NumbersCharset...)
	SpecialCharset          = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?")
	AllCharset              = append(AlphanumericCharset, SpecialCharset...)
)

Functions

func CamelCase

func CamelCase[T ~string]() func(destination ro.Observable[T]) ro.Observable[T]

CamelCase converts the string to camel case. Play: https://go.dev/play/p/MMmhpwApG1y

Example
// Convert strings to camelCase format
observable := ro.Pipe1(
	ro.Just(
		"hello world",
		"user_name",
		"API_KEY",
		"camel case",
	),
	CamelCase[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: helloWorld
Next: userName
Next: apiKey
Next: camelCase
Completed

func Capitalize

func Capitalize[T ~string]() func(destination ro.Observable[T]) ro.Observable[T]

Capitalize capitalizes the first letter of the string. Play: https://go.dev/play/p/7hK8m9jL3nS

Example
// Capitalize the first letter of each string
observable := ro.Pipe1(
	ro.Just(
		"hello",
		"world",
		"golang",
	),
	Capitalize[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: Hello
Next: World
Next: Golang
Completed

func Ellipsis

func Ellipsis[T ~string](length int) func(destination ro.Observable[T]) ro.Observable[T]

Ellipsis truncates the string to the specified length and appends "..." if the string is longer than the specified length. Play: https://go.dev/play/p/Yqx1pUep0uX

Example
// Truncate strings with ellipsis
observable := ro.Pipe1(
	ro.Just(
		"This is a very long string that needs to be truncated",
		"Short",
		"Another long string for demonstration",
	),
	Ellipsis[string](20),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: This is a very lo...
Next: Short
Next: Another long stri...
Completed

func KebabCase

func KebabCase[T ~string]() func(destination ro.Observable[T]) ro.Observable[T]

KebabCase converts the string to kebab case. Play: https://go.dev/play/p/Ndj3Gy2lztd

Example
// Convert strings to kebab-case format
observable := ro.Pipe1(
	ro.Just(
		"hello world",
		"userName",
		"API_KEY",
		"camelCase",
	),
	KebabCase[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: hello-world
Next: user-name
Next: api-key
Next: camel-case
Completed

func PascalCase

func PascalCase[T ~string]() func(destination ro.Observable[T]) ro.Observable[T]

PascalCase converts the string to pascal case. Play: https://go.dev/play/p/107SvPGvHAK

Example
// Convert strings to PascalCase format
observable := ro.Pipe1(
	ro.Just(
		"hello world",
		"user_name",
		"api_key",
		"camel case",
	),
	PascalCase[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: HelloWorld
Next: UserName
Next: ApiKey
Next: CamelCase
Completed

func Random

func Random[T any](size int, charset []rune) func(destination ro.Observable[T]) ro.Observable[string]

Random generates a random string of the specified size using the specified charset. Play: https://go.dev/play/p/7oDIGRxvrGt

Example
// Generate random strings
observable := ro.Pipe1(
	ro.Just(1, 2, 3),
	Random[int](10, AlphanumericCharset),
)

subscription := observable.Subscribe(ro.NoopObserver[string]())
defer subscription.Unsubscribe()

func SnakeCase

func SnakeCase[T ~string]() func(destination ro.Observable[T]) ro.Observable[T]

SnakeCase converts the string to snake case. Play: https://go.dev/play/p/hlGVKI-dR4y

Example
// Convert strings to snake_case format
observable := ro.Pipe1(
	ro.Just(
		"hello world",
		"userName",
		"API_KEY",
		"camelCase",
	),
	SnakeCase[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: hello_world
Next: user_name
Next: api_key
Next: camel_case
Completed

func Words

func Words[T ~string]() func(destination ro.Observable[T]) ro.Observable[[]T]

Words splits the string into words. Play: https://go.dev/play/p/fVW5bSK7ltj

Example
// Split strings into words
observable := ro.Pipe1(
	ro.Just(
		"hello world",
		"user_name",
		"camelCase",
		"PascalCase",
	),
	Words[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[[]string]())
defer subscription.Unsubscribe()
Output:

Next: [hello world]
Next: [user name]
Next: [camel Case]
Next: [Pascal Case]
Completed

Types

This section is empty.

Jump to

Keyboard shortcuts

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