-
Notifications
You must be signed in to change notification settings - Fork 635
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. |
MediaWiki uses a fairly simple algorithm to capitalize page titles, which is to change the first character C to Titlecase_Mapping(C) and leave the rest of the string as is. Titlecase_Mapping(C) is locale sensitive. Would this be a reasonable addition to std? |
IMO locale-sensitive by default is a bad idea for the reasons I outlined in #6016. Opt-in locale sensitivity may be useful though. If so, a uniform API should probably be provided for it across the relevant As for Edit: ok it's 31 total exceptions. But still only a short list, probably not too onerous to special-case them all: const allCodePoints = Array.from({ length: 0x10ffff + 1 }, (_, i) => String.fromCodePoint(i))
const titleCaseRe = /\p{Lt}/u
allCodePoints.filter((x) => titleCaseRe.test(x))
// ['Dž', 'Lj', 'Nj', 'Dz', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'ᾘ', 'ᾙ', 'ᾚ', 'ᾛ', 'ᾜ', 'ᾝ', 'ᾞ', 'ᾟ', 'ᾨ', 'ᾩ', 'ᾪ', 'ᾫ', 'ᾬ', 'ᾭ', 'ᾮ', 'ᾯ', 'ᾼ', 'ῌ', 'ῼ'] |
I put together an implementation of the algorithm. Turns out there are 135 characters whose titlecase mapping is different from its uppercase mapping. In total there are 1479 characters that change when titlecased. Looking at the list of exceptions I realized a shortcoming of this algorithm – when the input string starts with an uppercase digraph, it lowercases the second letter in the digraph. |
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: