Skip to content

Commit 3a9f821

Browse files
authoredDec 13, 2022
Fix the preserveConsecutiveUppercase option (#102)
1 parent 6f5439a commit 3a9f821

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed
 

‎index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
88
const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
99
const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
1010

11-
const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
11+
const preserveCamelCase = (string, toLowerCase, toUpperCase, preserveConsecutiveUppercase) => {
1212
let isLastCharLower = false;
1313
let isLastCharUpper = false;
1414
let isLastLastCharUpper = false;
15+
let isLastLastCharPreserved = false;
1516

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

1921
if (isLastCharLower && UPPERCASE.test(character)) {
2022
string = string.slice(0, index) + '-' + string.slice(index);
2123
isLastCharLower = false;
2224
isLastLastCharUpper = isLastCharUpper;
2325
isLastCharUpper = true;
2426
index++;
25-
} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
27+
} else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character) && (!isLastLastCharPreserved || preserveConsecutiveUppercase)) {
2628
string = string.slice(0, index - 1) + '-' + string.slice(index - 1);
2729
isLastLastCharUpper = isLastCharUpper;
2830
isLastCharUpper = false;
@@ -93,7 +95,7 @@ export default function camelCase(input, options) {
9395
const hasUpperCase = input !== toLowerCase(input);
9496

9597
if (hasUpperCase) {
96-
input = preserveCamelCase(input, toLowerCase, toUpperCase);
98+
input = preserveCamelCase(input, toLowerCase, toUpperCase, options.preserveConsecutiveUppercase);
9799
}
98100

99101
input = input.replace(LEADING_SEPARATORS, '');

‎test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import camelCase from './index.js';
33

44
test('camelCase', t => {
55
t.is(camelCase('foo'), 'foo');
6-
/// https://github.com/sindresorhus/camelcase/issues/95
7-
/// t.is(camelCase('IDs'), 'ids');
8-
/// t.is(camelCase('FooIDs'), 'fooIds');
6+
t.is(camelCase('IDs'), 'ids');
7+
t.is(camelCase('FooIDs'), 'fooIds');
98
t.is(camelCase('foo-bar'), 'fooBar');
109
t.is(camelCase('foo-bar-baz'), 'fooBarBaz');
1110
t.is(camelCase('foo--bar'), 'fooBar');
@@ -170,8 +169,7 @@ test('camelCase with preserveConsecutiveUppercase option', t => {
170169
t.is(camelCase('РозовыйПушистыйFOOдинорогиf', {preserveConsecutiveUppercase: true}), 'розовыйПушистыйFOOдинорогиf');
171170
t.is(camelCase('桑德在这里。', {preserveConsecutiveUppercase: true}), '桑德在这里。');
172171
t.is(camelCase('桑德_在这里。', {preserveConsecutiveUppercase: true}), '桑德在这里。');
173-
/// https://github.com/sindresorhus/camelcase/issues/95
174-
/// t.is(camelCase('IDs', {preserveConsecutiveUppercase: true}), 'ids');
172+
t.is(camelCase('IDs', {preserveConsecutiveUppercase: true}), 'iDs');
175173
t.is(camelCase('FooIDs', {preserveConsecutiveUppercase: true}), 'fooIDs');
176174
});
177175

0 commit comments

Comments
 (0)
Please sign in to comment.