zed icon indicating copy to clipboard operation
zed copied to clipboard

Function to title/capitalize strings

Open philrz opened this issue 2 years ago • 0 comments

A user asked in a community Slack thread:

curious if there's a way to title case a string, not seeing this in the stdlib/slack history/github issues.

Indeed this does not currently exist as a "first class" function in Zed, but a user-defined function can be created and we could add something formal in the future.

Details

Repro is with Zed commit 87deeeb.

The user clarified that they were referring to the kind of functionality shown in Python's str.title() and/or str.capwords(). Since this doesn't exist as a built-in Zed function right now, I proposed the following user-defined function:

$ cat title.zed 
func upper_first_char(str): (
  upper(str[0:1]) + str[1:]
)

func title(str): (
  join(map(split(str, ' '), upper_first_char), ' ')
)

$ zq -version
Version: v1.14.0-6-g87deeebe

$ echo '"hello world, this is a sentence."' | zq -I title.zed 'yield title(this)' -
"Hello World, This Is A Sentence."

The user confirmed this did indeed meet their needs for now. However, I'm opening this issue so that users that go searching for it in the future (like this user did before asking on Slack) might find this issue and use the user-defined function and/or chime in with their take on if this UDF meets their needs or if they need something more sophisticated. We also could eventually plan to implement this as a first-class function or make the UDF above part of some kind of "standard library".

philrz avatar Feb 29 '24 20:02 philrz