-
Notifications
You must be signed in to change notification settings - Fork 621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat request: expose publicly capitalizeWord
(or similar)
#5424
Comments
Implementing I would like to have |
Maybe I misdirected the issue, I wanted to have an alias for The Also just noticed that the function linked in the original post is actually not exactly what I wanted, the Sorry if the issue wasn't clear |
I think that the addition of |
Sounds like |
I think Even with
Implementation could look something like this: type CapitalizeWordOptions = {
locale: string | Intl.Locale
force: boolean
}
const defaults: CapitalizeWordOptions = {
locale: 'en-US',
force: false,
}
function capitalizeWord(word: string, options?: Partial<CapitalizeWordOptions>): string {
const { locale, force } = { ...defaults, ...options }
for (const { segment: grapheme, index } of new Intl.Segmenter(locale, { granularity: 'grapheme' }).segment(word)) {
if (/\p{L}/u.test(grapheme)) {
const before = word.slice(0, index)
const after = word.slice(index + grapheme.length)
const afterModified = force ? after.toLocaleLowerCase(locale) : after
return before + grapheme.toLocaleUpperCase(locale) + afterModified
}
}
return word
} |
I got some reference in the https://www.php.net/manual/en/function.ucfirst.php I think that what @lionel-rowe said falls more on the description of the 'toTitleCase()' function, which I agree with @timreichen that it would be a great effort to do so by now. With the |
I'd suggest title casing is something that should permanently fall outside the scope of The reason I think capitalizing a single word could reasonably fall within the scope of With that said, I think it's worth distinguishing between a "dev-first" and a "user-first" approach to capitalization:
Generally speaking, dev-first capitalization is only for dev-centric use cases and should be avoided for user-facing text. On the other hand, the user-first approach can be used for both user-facing and dev-facing purposes, but in dev-facing scenarios you wouldn't get the type inference. |
Further to that, looking at the usage of the |
I suspect subtle/context-sensitive grammatical operations like title casing will be mostly done via cheap-end llms where possible. |
Is your feature request related to a problem? Please describe.
Expose publicly
capitalizeWord
or something similar (maybecapitalize
/ucfirst
/titleCase
/ ...):https://github.com/denoland/deno_std/blob/22d3bda488f145b725fc1eaeee16922a97d88add/text/_util.ts#L9-L13
A lot of languages provides a
ucfirst
helper (e.g. php, perl, etc.) that capitalize the first letter of a string.While this is trivial enough, it's often tedious to have to redefine this function in every project when needed
Describe the solution you'd like
Feature offered by std lib
Describe alternatives you've considered
Redefining this the
capitalizeWord
in own projectThe text was updated successfully, but these errors were encountered: