gouch / to-title-case Goto Github PK
View Code? Open in Web Editor NEWA JavaScript method for intelligently converting strings to title case.
Home Page: https://individed.com/code/to-title-case/
License: MIT License
A JavaScript method for intelligently converting strings to title case.
Home Page: https://individed.com/code/to-title-case/
License: MIT License
See title -- All uppercased words, "THIS", for example, are not title-cased. They stay uppercase.
The script obviously has no way of "knowing" the grammatical role of a word, which sometimes produces erroneous output.
For instance: "Review: The book On Again Off Again is horrible".toTitleCase()
will produce Review: The Book on Again Off Again Is Horrible
, which is wrong, as On Again Off Again
is a proper noun in this case.
Similarly, the output of "The on button, a history".toTitleCase()
will be The on Button, a History
, which again, is erroneous as on button
is a noun here, and should therefore be capitalized.
I can think of no programmatic way of solving such edge case, but think that it should at least be possible to manually override lower-casing, maybe by providing an argument controlling the forced lower-casing:
"Review: The book On Again Off Again is horrible".toTitleCase()
==> Review: The Book on Again Off Again Is Horrible
"Review: The book On Again Off Again is horrible".toTitleCase(false)
==> Review: The Book On Again Off Again Is Horrible
"TEST".toTitleCase()
-> "TEST"
"test".toTitleCase()
-> "Test"
So stumbled across your JS and found it to be very useful. There are several edge cases that I caught, related to Irish Surnames.
https://english.stackexchange.com/questions/5915/etymology-for-mc-and-o-prefix-in-surnames
It would be nice to have a list of exclusion words like TV, M.A.C., TMZ, etc.
Example, where 'on' and 'out' are part of the small words list already included on the script, and 'TMZ', 'TV' and 'M.A.S.H.' are part of a (yet to be included) list of exclusion words:
"Check out this TMZ article on TV right after M.A.S.H." => "Check out This TMZ Article on TV Right After M.A.S.H."
Reason for this? Suppose I receive a string from some ancient database where everything's stored in CAPS LOCK, like "BARNES AND NOBLE". If I use toLowerCase() before toTitleCase() it works ok, but it won't work if the string is like "TMZ AND AT&T PARTNERSHIP ROCKS THE WORLD".
If the value you are trying to convert is all caps, it is not converted to title case. Is this intentional? You can call .toLowerCase() first and then it works just fine.
var myTitle = 'MY TITLE'
console.log( myTitle.toTitleCase() ) // MY TITLE
console.log( myTitle.toLowerCase().toTitleCase() ) //My Title
Thanks!
If you start with a string that is all UPPERCASE, then it doesn't get converted to Title Case, it stays all uppercase. The simple way to change this would be to change line 9.
From:
this.replace(...)
To:
this.toLowerCase().replace(...)
"HELLO world".toTitleCase()
returns "HELLO World"
and not "Hello World"
.
It's possible this is by design, but I think it would be better if the function always returned Title Case unless there was a clear reason not to (i.e. a small word in your list, a website, etc). If you like it ignoring capitalized words, maybe you could add an example to the tests showing that it's intentional? Then the choice would be more explicit.
Thanks for publishing this btw!
Words containing apostrophes aren't handled correctly.
Don'T --> Don'T
Related: converting all caps to title case in Excel has the same limitation
Current implementation seems to assume that the argument value is already in lower case.
A quick (though not optimized) fix for this is to insert toLowerCase() in the return line:
return this.toLowerCase().replace( ...
With this included, you wouldn't need to separately convert the short words to lower case. One lost feature with this approach is the ability to pass in a string with words (such as acronyms, etc.) in upper case, that the user wants to keep in upper case. To me, that's a reasonable trade-off (in favor of what, at least for my set of use cases, is much more common).
Thanks,
D
"this is a test." gets title cased to "This Is a test." instead of "This Is a Test."
A fix is to update the following code from:
// Ignore intentional capitalization
if (current.substr(1).search(/[A-Z]|\../) > -1) {
return current;
}
to:
// Ignore intentional capitalization
if (current.substr(1).search(/[A-Z]|\.[a-z0-9\u00C0-\u00FF]/) > -1) {
return current;
}
or possibly better using test() instead of search():
// Ignore intentional capitalization
if (/[A-Z]|\.[a-z0-9\u00C0-\u00FF]/.test(current.substr(1))) {
return current;
}
This string: On a Mission From... The Gods
Gets converted to: On a Mission From... the Gods
The
should have stayed unchanged.
I really like this function! It is tremendously helpful in insuring that all user-entered titles have a uniform format. However, there are sometimes exceptions, particularly in technical terms
For example, for the title "Degradation of mRNAs for Centromere and Telomere-Related Genes in Saccharomyces Cerevisiae"
the standard latin format for naming would have cerevisiae in lower case.
Would it be possible for you to incorporate a "tag" for your javascript function where it would pass the case through unchanged. In other words, if the tag was %%, the entry
"Genes In %%Saccharomyces cerevisiae%%"".toTitleCase()
would return the string "Genes in Saccharomyces cerevisiae" instead of Genes in Saccharomyces Cerevisiae?
This may be helpful for other users who want specific capitalization formats for specific words.
Thanks again!
Tom
Well it is an edge case, but
input: "CODIPHY: composing on-demand intelligent physical layers"
expects: "CODIPHY: Composing On-Demand Intelligent Physical Layers"
output: "CODIPHY: Composing on-Demand Intelligent Physical Layers"
There is an issue with the Dutch digraph “ij”. If a word starts with “ij”, for example “ijs” (Dutch for “ice”) it must be converted to “IJ” in title case.
Obviously the fault of RegEx's \W
incorrectly matching 'æ', but it is an issue unfortunately.
If I input "THIS IS ALL IN CAPS" at http://individed.com/code/to-title-case/ I get "THIS IS ALL in CAPS", when it should clearly be "This Is All in Caps".
I note that your script is not packaged for npm.
Any interest in merging a pull request if it adds meteor's package.js
file?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.