Skip to content

Support for more String transformation actions #1324

Description

@Eronmmer

I've been using Valibot for a project where I'm constantly converting between different naming conventions (API uses snake_case, frontend uses camelCase), and I noticed that while we have toLowerCase() and toUpperCase(), we're missing the more common case transformations.

What I need

I keep writing custom transforms to convert to/from snake-case, kebab-case, etc. But it would be much cleaner/nice to have built-in actions like so:

toCamelCase() - "hello_world""helloWorld"
toSnakeCase() - "helloWorld""hello_world"
toKebabCase() - "helloWorld""hello-world"
toPascalCase() - "hello_world""HelloWorld"

This comes up constantly when:

  • Converting API responses (backend uses snake_case, frontend uses camelCase)
  • Processing form inputs for URL slugs
  • Normalising data between different systems

Right now I either write custom transforms or pull in libraries like lodash just for these functions.

Proposal implementation

I looked at how toLowerCase is implemented and it seems pretty straightforward to follow the same pattern. Would be happy to take a shot at implementing this if you think it fits with Valibot's direction.

The logic isn't too complex:

// rough idea for camelCase
const toCamelCase = (str: string) => 
  str.replace(/[-_\s]+(.)?/g, (_, char) => char ? char.toUpperCase() : '');

Possible performance considerations

If you're concerned about the use of regex for transformation here slowing things down, we could implement this using either character iteration or a split/join approach, using regex only when it's absolutely necessary.

What do you think? Happy to work on a PR if this sounds good!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions