robytes

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

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

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

README

Bytes Plugin

The byte plugin provides operators for manipulating byte slices and strings in reactive streams.

Installation

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

Operators

CamelCase

Converts strings to camelCase format.

import (
    "github.com/samber/ro"
    robytes "github.com/samber/ro/plugins/bytes"
)

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

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

Capitalizes the first letter of each string.

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

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

Converts strings to kebab-case format.

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

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

Converts strings to PascalCase format.

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

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

Converts strings to snake_case format.

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

// 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(
        []byte("This is a very long string that needs to be truncated"),
        []byte("Short"),
        []byte("Another long string for demonstration"),
    ),
    robytes.Ellipsis[[]byte](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(
        []byte("hello world"),
        []byte("user_name"),
        []byte("camelCase"),
        []byte("PascalCase"),
    ),
    robytes.Words[[]byte](),
)

// 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(
        []byte("prefix"),
        []byte("suffix"),
        []byte("base"),
    ),
    robytes.Random[[]byte](10, robytes.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"
    robytes "github.com/samber/ro/plugins/bytes"
)

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

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

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

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 ~[]byte]() func(destination ro.Observable[T]) ro.Observable[T]

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

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data []byte) {
			fmt.Printf("Next: %s\n", string(data))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
defer subscription.Unsubscribe()
Output:

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

func Capitalize

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

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

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data []byte) {
			fmt.Printf("Next: %s\n", string(data))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
defer subscription.Unsubscribe()
Output:

Next: Hello
Next: World
Next: Golang
Completed

func Ellipsis

func Ellipsis[T ~[]byte](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/5HBKJcWTNrG

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data []byte) {
			fmt.Printf("Next: %s\n", string(data))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
defer subscription.Unsubscribe()
Output:

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

func KebabCase

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

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

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data []byte) {
			fmt.Printf("Next: %s\n", string(data))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
defer subscription.Unsubscribe()
Output:

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

func PascalCase

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

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

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data []byte) {
			fmt.Printf("Next: %s\n", string(data))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
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[[]byte]

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

Example
// Generate random strings
observable := ro.Pipe1(
	ro.Just(
		[]byte("prefix"),
		[]byte("suffix"),
		[]byte("base"),
	),
	Random[[]byte](10, AlphanumericCharset),
)

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

func SnakeCase

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

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

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data []byte) {
			fmt.Printf("Next: %s\n", string(data))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
defer subscription.Unsubscribe()
Output:

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

func Words

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

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

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

subscription := observable.Subscribe(
	ro.NewObserver(
		func(data [][]byte) {
			words := make([]string, len(data))
			for i, word := range data {
				words[i] = string(word)
			}
			fmt.Printf("Next: [%s]\n", strings.Join(words, " "))
		},
		func(err error) {
			fmt.Printf("Error: %s\n", err.Error())
		},
		func() {
			fmt.Printf("Completed\n")
		},
	),
)
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