From f5b65be4b27ff2ce6027e02711f4d8b26951923f Mon Sep 17 00:00:00 2001 From: Tinydig Date: Fri, 17 Feb 2023 21:50:00 +0200 Subject: [PATCH] Fix camelcase for values with numbers sindresorhus#77 --- index.js | 5 +---- test.js | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 6d80316..79b77f9 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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) { diff --git a/test.js b/test.js index 306b437..39f535d 100644 --- a/test.js +++ b/test.js @@ -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'); + 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('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ'), 'розовыйПушистыйЕдинороги'); @@ -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}), 'РозовыйПушистыйЕдинороги'); @@ -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'); @@ -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');