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
camelCase expectations #201
Comments
I think this gets tricky. I can see |
I ran into the same issue, i.e. being surprised by the output of |
I agree. I think acronyms just might be an exception to camel-casing. For the mentioned example maybe using removeNonWord() or some utility that simple removes delimiters would work better. I like to think of camelCase as a function that mutates a string with the help of delimiters and not in lieu of delimiters. I would hope that Shipping_Address, SHIPPING-ADDRESS, shipping_address, and even "Shipping address" would all become shippingAddress after camel-casing. |
cool. |
this another edge case is when the input is already in for now I would say that we could do something that uppercases the first char of each word and lowercases the rest would be good enough... but maybe we could add some smart logic that detects acronyms? (eg. if string contains any other words with lowercase chars and current word is all caps consider as acronym) |
just spent the last 10min implementing something that is smarter about acronyms: define(['../lang/toString', './replaceAccents', './removeNonWord', './upperCase', './lowerCase'], function(toString, replaceAccents, removeNonWord, upperCase, lowerCase){
/**
* Convert string to camelCase text.
*/
function camelCase(str){
str = toString(str);
str = replaceAccents(str);
str = removeNonWord(str)
.replace(/[\-_]/g, ' '); // convert all hyphens and underscores to spaces
// handle acronyms
// matches lowercase chars && uppercase words
if (/[a-z]/.test(str) && /^|\s[A-Z]+\s|$/.test(str)) {
// we convert any word that isn't all caps into lowercase
str = str.replace(/\s(\w+)/g, function(word, m) {
return /^[A-Z]+$/.test(m) ? word : lowerCase(word);
});
} else if (/\s/.test(str)) {
// if it doesn't contain an acronym and it has spaces we should
// convert every word to lowercase
str = lowerCase(str);
}
return str
.replace(/\s[a-z]/g, upperCase) // convert first char of each word to UPPERCASE
.replace(/^\s*[A-Z]+/g, lowerCase) // convert first word to lowercase
.replace(/\s+/g, ''); //remove spaces
}
return camelCase;
}); basically so: // keeps camelCase
camelCase('loremIpsum') === 'loremIpsum';
// all caps is converted into regular camelCase
camelCase('XML HTTP REQUEST') === 'xmlHttpRequest';
// mixed case preserves acronym (unless it's first word)
camelCase('XML_HTTP_Request') === 'xmlHTTPRequest but as you guys can see implementation is way more complex.. |
there are probably similar issues with other string methods.. so I'm going to wait for feedback before merging it and maybe we do another release covering all the string methods at once. sorry for the huge delay, been favoring other things during my free time.. |
I think the results of the new code are more in line my expectations at least. |
I also agree, this change looks good. |
When camelCasing a delimited uppercase string the following happens:
camelCase('SOME_TEXT'); //--> 'sOMETEXT'
I assumed we'd want the function to result in:
'someText'
Is this something you'd be interested in adding?
https://github.com/mout/mout/blob/master/src/string/camelCase.js
The text was updated successfully, but these errors were encountered: