Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update preserveCamelCase method #102

Merged
merged 1 commit into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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