Skip to content
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

Closed
microbial opened this issue Sep 5, 2014 · 9 comments · Fixed by #209
Closed

camelCase expectations #201

microbial opened this issue Sep 5, 2014 · 9 comments · Fixed by #209

Comments

@microbial
Copy link

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

@jdalton
Copy link

jdalton commented Sep 5, 2014

I think this gets tricky. I can see SOME and TEXT being seen as separate words but the casing isn't so clear. For example it could be XML_HTTP_Request -> xmlHTTPRequest.

@jeffrose
Copy link

jeffrose commented Sep 7, 2014

I ran into the same issue, i.e. being surprised by the output of camelCase in this scenario. I would expect SOME_TEXT to covert to someText and XML_HTTP_Request to covert to xmlHttpRequest. Wanting the latter to convert to xmlHTTPRequest seems like an exception scenario that should take extra code.

@microbial
Copy link
Author

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.

@jdalton
Copy link

jdalton commented Sep 8, 2014

cool.

@millermedeiros
Copy link
Member

this camelCase implementation came from my really old Eclipse Monkey scripts (back from 2009) - I agree that acronyms are an edge case and would rather keep it intuitive.

another edge case is when the input is already in camelCase (should it keep it as is?).

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)

@millermedeiros
Copy link
Member

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..

@millermedeiros
Copy link
Member

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..

@jeffrose
Copy link

I think the results of the new code are more in line my expectations at least.

@satazor
Copy link
Contributor

satazor commented Jan 3, 2015

I also agree, this change looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants