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

Added optional locale option. #74

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 11 additions & 11 deletions index.js
@@ -1,6 +1,6 @@
'use strict';

const preserveCamelCase = string => {
const preserveCamelCase = (string, locale) => {
let isLastCharLower = false;
let isLastCharUpper = false;
let isLastLastCharUpper = false;
Expand All @@ -20,9 +20,9 @@ const preserveCamelCase = string => {
isLastCharUpper = false;
isLastCharLower = true;
} else {
isLastCharLower = character.toLocaleLowerCase() === character && character.toLocaleUpperCase() !== character;
isLastCharLower = character.toLocaleLowerCase(locale) === character && character.toLocaleUpperCase(locale) !== character;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = character.toLocaleUpperCase() === character && character.toLocaleLowerCase() !== character;
isLastCharUpper = character.toLocaleUpperCase(locale) === character && character.toLocaleLowerCase(locale) !== character;
}
}

Expand All @@ -35,11 +35,11 @@ const camelCase = (input, options) => {
}

options = {
...{pascalCase: false},
...{pascalCase: false, locale: null},
...options
};

const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase() + x.slice(1) : x;
const locale = (typeof options.locale === 'string') ? options.locale : undefined;
const postProcess = x => options.pascalCase ? x.charAt(0).toLocaleUpperCase(locale) + x.slice(1) : x;

if (Array.isArray(input)) {
input = input.map(x => x.trim())
Expand All @@ -54,20 +54,20 @@ const camelCase = (input, options) => {
}

if (input.length === 1) {
return options.pascalCase ? input.toLocaleUpperCase() : input.toLocaleLowerCase();
return options.pascalCase ? input.toLocaleUpperCase(locale) : input.toLocaleLowerCase(locale);
}

const hasUpperCase = input !== input.toLocaleLowerCase();
const hasUpperCase = input !== input.toLocaleLowerCase(locale);

if (hasUpperCase) {
input = preserveCamelCase(input);
}

input = input
.replace(/^[_.\- ]+/, '')
.toLocaleLowerCase()
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase())
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase());
.toLocaleLowerCase(locale)
.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(locale))
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(locale));

return postProcess(input);
};
Expand Down
2 changes: 2 additions & 0 deletions test.js
Expand Up @@ -128,6 +128,8 @@ test('camelCase with pascalCase option', t => {
t.is(camelCase('РОЗОВЫЙ_ПУШИСТЫЙ-ЕДИНОРОГИ', {pascalCase: true}), 'РозовыйПушистыйЕдинороги');
t.is(camelCase('桑德在这里。', {pascalCase: true}), '桑德在这里。');
t.is(camelCase('桑德_在这里。', {pascalCase: true}), '桑德在这里。');
t.is(camelCase('run-in-band', {locale: 'tr-TR'}), 'runİnBand');
t.is(camelCase('run-in-band', {locale: 'en-US'}), 'runInBand');
});

test('invalid input', t => {
Expand Down