Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix the preserveConsecutiveUppercase option (#102)
  • Loading branch information
ambujsahu81 committed Dec 13, 2022
1 parent 6f5439a commit 3a9f821
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 5 additions & 3 deletions index.js
Expand Up @@ -8,21 +8,23 @@ 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) => {
const preserveCamelCase = (string, toLowerCase, toUpperCase, preserveConsecutiveUppercase) => {
let isLastCharLower = false;
let isLastCharUpper = false;
let isLastLastCharUpper = false;
let isLastLastCharPreserved = false;

for (let index = 0; index < string.length; index++) {
const character = string[index];
isLastLastCharPreserved = index > 2 ? string[index - 3] === '-' : true;

if (isLastCharLower && UPPERCASE.test(character)) {
string = string.slice(0, index) + '-' + string.slice(index);
isLastCharLower = false;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true;
index++;
} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character) && (!isLastLastCharPreserved || preserveConsecutiveUppercase)) {
string = string.slice(0, index - 1) + '-' + string.slice(index - 1);
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = false;
Expand Down Expand Up @@ -93,7 +95,7 @@ export default function camelCase(input, options) {
const hasUpperCase = input !== toLowerCase(input);

if (hasUpperCase) {
input = preserveCamelCase(input, toLowerCase, toUpperCase);
input = preserveCamelCase(input, toLowerCase, toUpperCase, options.preserveConsecutiveUppercase);
}

input = input.replace(LEADING_SEPARATORS, '');
Expand Down
8 changes: 3 additions & 5 deletions test.js
Expand Up @@ -3,9 +3,8 @@ import camelCase from './index.js';

test('camelCase', t => {
t.is(camelCase('foo'), 'foo');
/// https://github.com/sindresorhus/camelcase/issues/95
/// t.is(camelCase('IDs'), 'ids');
/// t.is(camelCase('FooIDs'), 'fooIds');
t.is(camelCase('IDs'), 'ids');
t.is(camelCase('FooIDs'), 'fooIds');
t.is(camelCase('foo-bar'), 'fooBar');
t.is(camelCase('foo-bar-baz'), 'fooBarBaz');
t.is(camelCase('foo--bar'), 'fooBar');
Expand Down Expand Up @@ -170,8 +169,7 @@ test('camelCase with preserveConsecutiveUppercase option', t => {
t.is(camelCase('РозовыйПушистыйFOOдинорогиf', {preserveConsecutiveUppercase: true}), 'розовыйПушистыйFOOдинорогиf');
t.is(camelCase('桑德在这里。', {preserveConsecutiveUppercase: true}), '桑德在这里。');
t.is(camelCase('桑德_在这里。', {preserveConsecutiveUppercase: true}), '桑德在这里。');
/// https://github.com/sindresorhus/camelcase/issues/95
/// t.is(camelCase('IDs', {preserveConsecutiveUppercase: true}), 'ids');
t.is(camelCase('IDs', {preserveConsecutiveUppercase: true}), 'iDs');
t.is(camelCase('FooIDs', {preserveConsecutiveUppercase: true}), 'fooIDs');
});

Expand Down

0 comments on commit 3a9f821

Please sign in to comment.