sindresorhus / camelcase

Convert a dash/dot/underscore/space separated string to camelCase: foo-bar → fooBar
MIT License
682 stars 95 forks source link

Subsequent uppercase letters are not converted #17

Closed sorenlouv closed 8 years ago

sorenlouv commented 8 years ago

Subsequent uppercase letters in a string are not converted to lowercase.

Example: "XMLHttpRequest" is converted to "xMLHttpRequest" rather than "xmlHttpRequest".

Compared with Lodash

const camelCase = require('camelcase');
const _ = require('lodash');

camelCase('XMLHttpRequest'); // xMLHttpRequest
_.camelCase('XMLHttpRequest'); // xmlHttpRequest

Is this the intended behaviour?

sindresorhus commented 8 years ago

Yeah, makes sense. I just never considered that use-case. PR welcome for a fix :)

abhishekc-sharma commented 8 years ago

If this is fixed, what would be the expected output for camelCase('FBBazzy') ?

SamVerschueren commented 8 years ago

fbBazzy probably

abhishekc-sharma commented 8 years ago

The changes I've made pass the following test cases including the previous ones without non basic plane characters.

t.is(fn('XMLHttpRequest'), 'xmlHttpRequest');
t.is(fn('AjaxXMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(fn('Ajax-XMLHttpRequest'), 'ajaxXmlHttpRequest');

It fails for this test case :

t.is(fn('FBBÈzzy'), 'fbbÈzzy');

This test case is t.is(fn('FBBÈzzy'), 'fBBÈzzy'); in the current version and passes because only the first character is compared. The current version produces the wrong answer when a character such as È occurs after a - before the final step is run.

camelCase('Ab-Èb') == 'ab-èb'

This is down to the fact that Regexes before ES6 are not Unicode aware I presume ?

jdalton commented 8 years ago

camelCase('Ab-Èb') == 'ab-èb'

For reference lodash.camelcase deburrs Latin-1 Supplement and Latin Extended-A block characters:

camelcase('Ab-Èb')
// => "abEb"