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

Move regexp to outer scope #87

Merged
merged 2 commits into from Nov 15, 2021
Merged
Changes from 1 commit
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: 16 additions & 6 deletions index.js
@@ -1,5 +1,15 @@
'use strict';

const UPPER_CASE = /[\p{Lu}]/u;
nam-hle marked this conversation as resolved.
Show resolved Hide resolved
const LOWER_CASE = /[\p{Ll}]/u;
const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
const SEPARATORS = /[_.\- ]+/;

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, locale) => {
let isLastCharLower = false;
let isLastCharUpper = false;
Expand All @@ -8,13 +18,13 @@ const preserveCamelCase = (string, locale) => {
for (let i = 0; i < string.length; i++) {
const character = string[i];

if (isLastCharLower && /[\p{Lu}]/u.test(character)) {
if (isLastCharLower && UPPER_CASE.test(character)) {
string = string.slice(0, i) + '-' + string.slice(i);
isLastCharLower = false;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true;
i++;
} else if (isLastCharUpper && isLastLastCharUpper && /[\p{Ll}]/u.test(character)) {
} else if (isLastCharUpper && isLastLastCharUpper && LOWER_CASE.test(character)) {
string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = false;
Expand All @@ -30,12 +40,12 @@ const preserveCamelCase = (string, locale) => {
};

const preserveConsecutiveUppercase = input => {
return input.replace(/^[\p{Lu}](?![\p{Lu}])/gu, m1 => m1.toLowerCase());
return input.replace(LEADING_CAPITAL, m1 => m1.toLowerCase());
};

const postProcess = (input, options) => {
return input.replace(/[_.\- ]+([\p{Alpha}\p{N}_]|$)/gu, (_, p1) => p1.toLocaleUpperCase(options.locale))
.replace(/\d+([\p{Alpha}\p{N}_]|$)/gu, m => m.toLocaleUpperCase(options.locale));
return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => identifier.toLocaleUpperCase(options.locale))
.replace(NUMBERS_AND_IDENTIFIER, m => m.toLocaleUpperCase(options.locale));
};

const camelCase = (input, options) => {
Expand Down Expand Up @@ -71,7 +81,7 @@ const camelCase = (input, options) => {
input = preserveCamelCase(input, options.locale);
}

input = input.replace(/^[_.\- ]+/, '');
input = input.replace(LEADING_SEPARATORS, '');

if (options.preserveConsecutiveUppercase) {
input = preserveConsecutiveUppercase(input);
Expand Down