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

Apply casing to chars except the ones after a number (issue #77) #109

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ 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 applyCasingExpectAfterNumber = (string, applyCase) => {
let result = '';
for (let i = 0; i < string.length; i++) {
const currentChar = string[i];
const previousChar = string[i - 1];

const previousCharIsNumber = !Number.isNaN(Number(previousChar));
result += i > 0 && previousCharIsNumber ? currentChar : applyCase(currentChar);
}

return result;
};

const preserveCamelCase = (string, toLowerCase, toUpperCase, preserveConsecutiveUppercase) => {
let isLastCharLower = false;
let isLastCharUpper = false;
Expand Down Expand Up @@ -77,12 +90,12 @@ export default function camelCase(input, options) {
}

const toLowerCase = options.locale === false
? string => string.toLowerCase()
: string => string.toLocaleLowerCase(options.locale);
? string => applyCasingExpectAfterNumber(string, char => char.toLowerCase())
: string => applyCasingExpectAfterNumber(string, char => char.toLocaleLowerCase(options.locale));

const toUpperCase = options.locale === false
? string => string.toUpperCase()
: string => string.toLocaleUpperCase(options.locale);
? string => applyCasingExpectAfterNumber(string, char => char.toUpperCase())
: string => applyCasingExpectAfterNumber(string, char => char.toLocaleUpperCase(options.locale));

if (input.length === 1) {
if (SEPARATORS.test(input)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@
"devDependencies": {
"ava": "^4.3.0",
"tsd": "^0.20.0",
"xo": "^0.49.0"
"xo": "^0.54.2"
}
}
12 changes: 6 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ test('camelCase', t => {
t.is(camelCase('A::a'), 'a::a');
t.is(camelCase('Hello1World'), 'hello1World');
t.is(camelCase('Hello11World'), 'hello11World');
t.is(camelCase('hello1world'), 'hello1World');
t.is(camelCase('Hello1World11foo'), 'hello1World11Foo');
t.is(camelCase('hello1world'), 'hello1world');
t.is(camelCase('Hello1World11foo'), 'hello1World11foo');
t.is(camelCase('Hello1'), 'hello1');
t.is(camelCase('hello1'), 'hello1');
t.is(camelCase('1Hello'), '1Hello');
t.is(camelCase('1hello'), '1Hello');
t.is(camelCase('h2w'), 'h2W');
t.is(camelCase('1hello'), '1hello');
t.is(camelCase('h2w'), 'h2w');
t.is(camelCase('розовый_пушистый-единороги'), 'розовыйПушистыйЕдинороги');
t.is(camelCase('розовый_пушистый-единороги'), 'розовыйПушистыйЕдинороги');
t.is(camelCase('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ'), 'розовыйПушистыйЕдинороги');
Expand Down Expand Up @@ -126,11 +126,11 @@ test('camelCase with pascalCase option', t => {
t.is(camelCase('A::a', {pascalCase: true}), 'A::a');
t.is(camelCase('Hello1World', {pascalCase: true}), 'Hello1World');
t.is(camelCase('Hello11World', {pascalCase: true}), 'Hello11World');
t.is(camelCase('hello1world', {pascalCase: true}), 'Hello1World');
t.is(camelCase('hello1world', {pascalCase: true}), 'Hello1world');
t.is(camelCase('hello1World', {pascalCase: true}), 'Hello1World');
t.is(camelCase('hello1', {pascalCase: true}), 'Hello1');
t.is(camelCase('Hello1', {pascalCase: true}), 'Hello1');
t.is(camelCase('1hello', {pascalCase: true}), '1Hello');
t.is(camelCase('1hello', {pascalCase: true}), '1hello');
t.is(camelCase('1Hello', {pascalCase: true}), '1Hello');
t.is(camelCase('h1W', {pascalCase: true}), 'H1W');
t.is(camelCase('РозовыйПушистыйЕдинороги', {pascalCase: true}), 'РозовыйПушистыйЕдинороги');
Expand Down