From e7dccc901ce645138a52dd5945e245773b4684c5 Mon Sep 17 00:00:00 2001 From: JCrandall101 <31408084+JCrandall101@users.noreply.github.com> Date: Wed, 9 Aug 2023 12:30:07 -0400 Subject: [PATCH] Fix incorrect camelization (#112) --- index.js | 4 ++-- test.js | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6d80316..2be1bd1 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, (match, pattern, offset) => ['_', '-'].includes(input.charAt(offset + match.length)) ? match : toUpperCase(match)) + .replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier)); }; export default function camelCase(input, options) { diff --git a/test.js b/test.js index 8c3f82b..4a013f8 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');