From 8c58cb47bdba5ebd5f87e0718108a2a9069f358f Mon Sep 17 00:00:00 2001 From: Jon Crandall Date: Thu, 20 Jul 2023 15:41:51 -0400 Subject: [PATCH 1/2] fix: reordered replace and check for underscore and dash after num char combos --- index.js | 4 ++-- package.json | 2 +- test.js | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6d80316..dd6381e 100644 --- a/index.js +++ b/index.js @@ -49,8 +49,8 @@ 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(NUMBERS_AND_IDENTIFIER, (m, n, i) => ['_', '-'].includes(input.charAt(i + m.length)) ? m : toUpperCase(m)) + .replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier)); }; export default function camelCase(input, options) { 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" } } diff --git a/test.js b/test.js index 306b437..27c2a17 100644 --- a/test.js +++ b/test.js @@ -2,6 +2,9 @@ import test from 'ava'; import camelCase from './index.js'; test('camelCase', t => { + t.is(camelCase('b2b_registration_request'), 'b2bRegistrationRequest'); + t.is(camelCase('b2b-registration-request'), 'b2bRegistrationRequest'); + t.is(camelCase('b2b_registration_b2b_request'), 'b2bRegistrationB2bRequest'); t.is(camelCase('foo'), 'foo'); t.is(camelCase('IDs'), 'ids'); t.is(camelCase('FooIDs'), 'fooIds'); @@ -76,6 +79,7 @@ test('camelCase', t => { }); test('camelCase with pascalCase option', t => { + t.is(camelCase('b2b_registration_request', {pascalCase: true}), 'B2bRegistrationRequest'); t.is(camelCase('foo', {pascalCase: true}), 'Foo'); t.is(camelCase('foo-bar', {pascalCase: true}), 'FooBar'); t.is(camelCase('foo-bar-baz', {pascalCase: true}), 'FooBarBaz'); From d468180b759d36983b4e4295de0149fed1c1efc1 Mon Sep 17 00:00:00 2001 From: Jon Crandall Date: Wed, 9 Aug 2023 09:14:02 -0400 Subject: [PATCH 2/2] chore: updating single letter variables --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index dd6381e..2be1bd1 100644 --- a/index.js +++ b/index.js @@ -49,7 +49,7 @@ const postProcess = (input, toUpperCase) => { SEPARATORS_AND_IDENTIFIER.lastIndex = 0; NUMBERS_AND_IDENTIFIER.lastIndex = 0; - return input.replace(NUMBERS_AND_IDENTIFIER, (m, n, i) => ['_', '-'].includes(input.charAt(i + m.length)) ? m : toUpperCase(m)) + return input.replace(NUMBERS_AND_IDENTIFIER, (match, pattern, offset) => ['_', '-'].includes(input.charAt(offset + match.length)) ? match : toUpperCase(match)) .replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier)); };