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

Fix camelcase for values with numbers sindresorhus#77 #106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions index.js
Expand Up @@ -6,7 +6,6 @@ const SEPARATORS = /[_.\- ]+/;

const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');

const preserveCamelCase = (string, toLowerCase, toUpperCase, preserveConsecutiveUppercase) => {
let isLastCharLower = false;
Expand Down Expand Up @@ -47,10 +46,8 @@ const preserveConsecutiveUppercase = (input, toLowerCase) => {

const postProcess = (input, toUpperCase) => {
SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
NUMBERS_AND_IDENTIFIER.lastIndex = 0;

return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
.replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier));
};

export default function camelCase(input, options) {
Expand Down
33 changes: 19 additions & 14 deletions test.js
Expand Up @@ -55,18 +55,19 @@ test('camelCase', t => {
t.is(camelCase('XMLHttpRequest'), 'xmlHttpRequest');
t.is(camelCase('AjaxXMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(camelCase('Ajax-XMLHttpRequest'), 'ajaxXmlHttpRequest');
t.is(camelCase('a1b_cd_ef'), 'a1bCdEf');
t.is(camelCase([]), '');
t.is(camelCase('mGridCol6@md'), 'mGridCol6@md');
t.is(camelCase('A::a'), 'a::a');
t.is(camelCase('Hello1World'), 'hello1World');
t.is(camelCase('Hello11World'), 'hello11World');
t.is(camelCase('hello1world'), 'hello1World');
t.is(camelCase('Hello1World11foo'), 'hello1World11Foo');
t.is(camelCase('Hello1World'), 'hello1world');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a correct change. It should stay hello1World.

t.is(camelCase('Hello11World'), 'hello11world');
t.is(camelCase('hello1world'), 'hello1world');
t.is(camelCase('Hello1World11foo'), 'hello1world11foo');
t.is(camelCase('Hello1'), 'hello1');
t.is(camelCase('hello1'), 'hello1');
t.is(camelCase('1Hello'), '1Hello');
t.is(camelCase('1hello'), '1Hello');
t.is(camelCase('h2w'), 'h2W');
t.is(camelCase('1Hello'), '1hello');
t.is(camelCase('1hello'), '1hello');
t.is(camelCase('h2w'), 'h2w');
t.is(camelCase('розовый_пушистый-единороги'), 'розовыйПушистыйЕдинороги');
t.is(camelCase('розовый_пушистый-единороги'), 'розовыйПушистыйЕдинороги');
t.is(camelCase('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ'), 'розовыйПушистыйЕдинороги');
Expand Down Expand Up @@ -121,18 +122,20 @@ test('camelCase with pascalCase option', t => {
t.is(camelCase('XMLHttpRequest', {pascalCase: true}), 'XmlHttpRequest');
t.is(camelCase('AjaxXMLHttpRequest', {pascalCase: true}), 'AjaxXmlHttpRequest');
t.is(camelCase('Ajax-XMLHttpRequest', {pascalCase: true}), 'AjaxXmlHttpRequest');
t.is(camelCase('a1b_cd_ef', {pascalCase: true}), 'A1bCdEf');
t.is(camelCase([], {pascalCase: true}), '');
t.is(camelCase('mGridCol6@md', {pascalCase: true}), 'MGridCol6@md');
t.is(camelCase('A::a', {pascalCase: true}), 'A::a');
t.is(camelCase('Hello1World', {pascalCase: true}), 'Hello1World');
t.is(camelCase('Hello11World', {pascalCase: true}), 'Hello11World');
t.is(camelCase('hello1world', {pascalCase: true}), 'Hello1World');
t.is(camelCase('hello1World', {pascalCase: true}), 'Hello1World');
t.is(camelCase('Hello1World', {pascalCase: true}), 'Hello1world');
t.is(camelCase('Hello11World', {pascalCase: true}), 'Hello11world');
t.is(camelCase('hello1world', {pascalCase: true}), 'Hello1world');
t.is(camelCase('hello1World', {pascalCase: true}), 'Hello1world');
t.is(camelCase('hello1', {pascalCase: true}), 'Hello1');
t.is(camelCase('Hello1', {pascalCase: true}), 'Hello1');
t.is(camelCase('1hello', {pascalCase: true}), '1Hello');
t.is(camelCase('1Hello', {pascalCase: true}), '1Hello');
t.is(camelCase('h1W', {pascalCase: true}), 'H1W');
t.is(camelCase('1hello', {pascalCase: true}), '1hello');
t.is(camelCase('1Hello', {pascalCase: true}), '1hello');
t.is(camelCase('h1w', {pascalCase: true}), 'H1w');
t.is(camelCase('h1W', {pascalCase: true}), 'H1w');
t.is(camelCase('РозовыйПушистыйЕдинороги', {pascalCase: true}), 'РозовыйПушистыйЕдинороги');
t.is(camelCase('розовый_пушистый-единороги', {pascalCase: true}), 'РозовыйПушистыйЕдинороги');
t.is(camelCase('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ', {pascalCase: true}), 'РозовыйПушистыйЕдинороги');
Expand Down Expand Up @@ -161,6 +164,7 @@ test('camelCase with preserveConsecutiveUppercase option', t => {
t.is(camelCase('XMLHttpRequest', {preserveConsecutiveUppercase: true}), 'XMLHttpRequest');
t.is(camelCase('AjaxXMLHttpRequest', {preserveConsecutiveUppercase: true}), 'ajaxXMLHttpRequest');
t.is(camelCase('Ajax-XMLHttpRequest', {preserveConsecutiveUppercase: true}), 'ajaxXMLHttpRequest');
t.is(camelCase('a1b_cd_ef', {preserveConsecutiveUppercase: true}), 'a1bCdEf');
t.is(camelCase([], {preserveConsecutiveUppercase: true}), '');
t.is(camelCase('mGridCOl6@md', {preserveConsecutiveUppercase: true}), 'mGridCOl6@md');
t.is(camelCase('A::a', {preserveConsecutiveUppercase: true}), 'a::a');
Expand Down Expand Up @@ -194,6 +198,7 @@ test('camelCase with both pascalCase and preserveConsecutiveUppercase option', t
t.is(camelCase('xMLHttpRequest', {pascalCase: true, preserveConsecutiveUppercase: true}), 'XMLHttpRequest');
t.is(camelCase('ajaxXMLHttpRequest', {pascalCase: true, preserveConsecutiveUppercase: true}), 'AjaxXMLHttpRequest');
t.is(camelCase('Ajax-XMLHttpRequest', {pascalCase: true, preserveConsecutiveUppercase: true}), 'AjaxXMLHttpRequest');
t.is(camelCase('a1b_cd_ef', {pascalCase: true, preserveConsecutiveUppercase: true}), 'A1bCdEf');
t.is(camelCase([], {pascalCase: true, preserveConsecutiveUppercase: true}), '');
t.is(camelCase('mGridCOl6@md', {pascalCase: true, preserveConsecutiveUppercase: true}), 'MGridCOl6@md');
t.is(camelCase('A::a', {pascalCase: true, preserveConsecutiveUppercase: true}), 'A::a');
Expand Down