From 5bb178599ccd673260860b1c8b93eab80f0e4196 Mon Sep 17 00:00:00 2001 From: Leonidas Mavrotas Date: Thu, 22 Jun 2023 18:47:58 +0300 Subject: [PATCH 1/2] feat: Add an applyCasingExceptAfterNumber function and update tests --- index.js | 21 +++++++++++++++++---- test.js | 12 ++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 6d80316..7ee3d33 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,19 @@ 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 applyCasingExpectAfterNumber = (string, applyCase) => { + let result = ''; + for (let i = 0; i < string.length; i++) { + const currentChar = string[i]; + const previousChar = string[i - 1]; + + const previousCharIsNumber = !Number.isNaN(Number(previousChar)); + result += i > 0 && previousCharIsNumber ? currentChar : applyCase(currentChar); + } + + return result; +}; + const preserveCamelCase = (string, toLowerCase, toUpperCase, preserveConsecutiveUppercase) => { let isLastCharLower = false; let isLastCharUpper = false; @@ -77,12 +90,12 @@ export default function camelCase(input, options) { } const toLowerCase = options.locale === false - ? string => string.toLowerCase() - : string => string.toLocaleLowerCase(options.locale); + ? string => applyCasingExpectAfterNumber(string, char => char.toLowerCase()) + : string => applyCasingExpectAfterNumber(string, char => char.toLocaleLowerCase(options.locale)); const toUpperCase = options.locale === false - ? string => string.toUpperCase() - : string => string.toLocaleUpperCase(options.locale); + ? string => applyCasingExpectAfterNumber(string, char => char.toUpperCase()) + : string => applyCasingExpectAfterNumber(string, char => char.toLocaleUpperCase(options.locale)); if (input.length === 1) { if (SEPARATORS.test(input)) { diff --git a/test.js b/test.js index 306b437..2c7e73c 100644 --- a/test.js +++ b/test.js @@ -60,13 +60,13 @@ test('camelCase', t => { 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('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('h2w'), 'h2w'); t.is(camelCase('розовый_пушистый-единороги'), 'розовыйПушистыйЕдинороги'); t.is(camelCase('розовый_пушистый-единороги'), 'розовыйПушистыйЕдинороги'); t.is(camelCase('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ'), 'розовыйПушистыйЕдинороги'); @@ -126,11 +126,11 @@ test('camelCase with pascalCase option', t => { 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('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('1Hello', {pascalCase: true}), '1Hello'); t.is(camelCase('h1W', {pascalCase: true}), 'H1W'); t.is(camelCase('РозовыйПушистыйЕдинороги', {pascalCase: true}), 'РозовыйПушистыйЕдинороги'); From 5b51c0fe81ce789357921477cdfcaccaf47a9752 Mon Sep 17 00:00:00 2001 From: Leonidas Mavrotas Date: Thu, 22 Jun 2023 19:06:14 +0300 Subject: [PATCH 2/2] fix: Updated xo dep version for tests to run --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b62c0a6..094ee13 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,6 @@ "devDependencies": { "ava": "^4.3.0", "tsd": "^0.20.0", - "xo": "^0.49.0" + "xo": "^0.54.2" } }